Skip to content

Commit

Permalink
fix: unwanted auth api call
Browse files Browse the repository at this point in the history
  • Loading branch information
gurjit03 committed Jul 30, 2023
1 parent fd8302b commit b0c1d51
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/js/hooks/use-get-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import { apiClient } from '@/components/FavouriteShabadButton/utils/api-client';
export const useGetUser = <D>() => {
const [user, setUser] = React.useState<D | null>(null);
const [isLoading, setIsLoading] = React.useState(true);

const isUserLoggedIn = !!window.localStorage.getItem(LOCAL_STORAGE_KEY_FOR_SESSION_TOKEN);
React.useEffect(() => {
apiClient('/auth/jwt', {
token: window.localStorage.getItem(LOCAL_STORAGE_KEY_FOR_SESSION_TOKEN),
})
.then((response) => {
setUser(response);
})
.catch((e) => {
// eslint-disable-next-line no-console
console.log('Error: ' + e.message);
localStorage.removeItem(LOCAL_STORAGE_KEY_FOR_SESSION_TOKEN);
if(isUserLoggedIn) {
apiClient('/auth/jwt', {
token: window.localStorage.getItem(LOCAL_STORAGE_KEY_FOR_SESSION_TOKEN),
})
.finally(() => {
setIsLoading(false);
});
.then((response) => {
setUser(response);
})
.catch((e) => {
// eslint-disable-next-line no-console
console.log('Error: ' + e.message);
localStorage.removeItem(LOCAL_STORAGE_KEY_FOR_SESSION_TOKEN);
})
.finally(() => {
setIsLoading(false);
});
}
}, []);

return { user, isLoading };
Expand Down

0 comments on commit b0c1d51

Please sign in to comment.