Skip to content

Commit

Permalink
refactor: update auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Nov 3, 2024
1 parent 3999c9c commit 034c147
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions client/features/auth/AuthLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAuthSession, getCurrentUser, signOut } from 'aws-amplify/auth';
import { fetchAuthSession, signOut } from 'aws-amplify/auth';
import { Hub } from 'aws-amplify/utils';
import { isAxiosError } from 'axios';
import { useLoading } from 'components/loading/useLoading';
Expand All @@ -13,23 +13,26 @@ export const AuthLoader = () => {
const { setLoading } = useLoading();
const { setAlert } = useAlert();
const updateCookie = useCallback(async () => {
const tokens = await fetchAuthSession().then((e) => e.tokens);
const tokens = await fetchAuthSession()
.then((e) => e.tokens)
.catch(catchApiErr);

if (tokens !== undefined && tokens.idToken !== undefined) {
await apiClient.session.$post({
if (!tokens?.idToken) return setUser(null);

const result = await apiClient.session
.$post({
body: { idToken: tokens.idToken.toString(), accessToken: tokens.accessToken.toString() },
});
await apiClient.private.me.$get().then(setUser);
} else {
setUser(null);
})
.catch(catchApiErr);

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

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

useEffect(() => {
// eslint-disable-next-line complexity
Expand Down Expand Up @@ -63,7 +66,7 @@ export const AuthLoader = () => {
setUser(null);
break;
case 'signedIn':
await updateCookie().catch(catchApiErr);
await updateCookie();
break;
case 'tokenRefresh_failure':
await signOut();
Expand Down

0 comments on commit 034c147

Please sign in to comment.