Skip to content

Commit

Permalink
refactor: split fetching me function
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Nov 6, 2024
1 parent 034c147 commit 3b03560
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions client/features/auth/AuthLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAuthSession, signOut } from 'aws-amplify/auth';
import { fetchAuthSession, getCurrentUser, signOut } from 'aws-amplify/auth';
import { Hub } from 'aws-amplify/utils';
import { isAxiosError } from 'axios';
import { useLoading } from 'components/loading/useLoading';
Expand All @@ -12,6 +12,10 @@ export const AuthLoader = () => {
const { user, setUser } = useUser();
const { setLoading } = useLoading();
const { setAlert } = useAlert();
const fetchMe = useCallback(
() => apiClient.private.me.$get().catch(catchApiErr).then(setUser),
[setUser],
);
const updateCookie = useCallback(async () => {
const tokens = await fetchAuthSession()
.then((e) => e.tokens)
Expand All @@ -25,14 +29,14 @@ export const AuthLoader = () => {
})
.catch(catchApiErr);

if (result?.status === 'success') {
await apiClient.private.me.$get().catch(catchApiErr).then(setUser);
}
}, [setUser]);
if (result?.status === 'success') await fetchMe();
}, [setUser, fetchMe]);

useEffect(() => {
updateCookie();
}, [updateCookie]);
getCurrentUser()
.then(fetchMe)
.catch(() => setUser(null));
}, [fetchMe, setUser]);

useEffect(() => {
// eslint-disable-next-line complexity
Expand Down

0 comments on commit 3b03560

Please sign in to comment.