Skip to content

Commit

Permalink
fix: fix withauth to always check user
Browse files Browse the repository at this point in the history
  • Loading branch information
chowjustin committed Nov 11, 2024
1 parent b91d80b commit 277c677
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/components/hoc/withAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,30 @@ export default function withAuth<T>(
stopLoading();
return;
}
if (!user) {
const loadUser = async () => {
try {
const res = await api.get<ApiResponse<User>>("/users/me");

if (!res.data.data) {
toast.error("Sesi login tidak valid");
throw new Error("Sesi login tidak valid");
}

login({
...res.data.data,
token,
});
} catch (err) {
await removeToken();
console.error("Failed to fetch user data", err);
} finally {
stopLoading();

const loadUser = async () => {
try {
const res = await api.get<ApiResponse<User>>("/users/me");

if (!res.data.data) {
toast.error("Sesi login tidak valid");
throw new Error("Sesi login tidak valid");
}
};

loadUser();
}
}, [isAuthenticated, login, logout, stopLoading, user]);
login({
...res.data.data,
token,
});
} catch (err) {
await removeToken();
console.error("Failed to fetch user data", err);
} finally {
stopLoading();
}
};

loadUser();
}, [isAuthenticated, login, logout, stopLoading]);

React.useEffect(() => {
checkAuth();
Expand Down

0 comments on commit 277c677

Please sign in to comment.