diff --git a/app/hooks/auth.tsx b/app/hooks/auth.tsx index e8ed6d9..4c225f2 100644 --- a/app/hooks/auth.tsx +++ b/app/hooks/auth.tsx @@ -20,6 +20,7 @@ type AuthContextType = { loading: boolean; login: () => void; logout: () => Promise; + update: () => Promise; }; const AuthContext = createContext({ @@ -27,6 +28,9 @@ const AuthContext = createContext({ loading: false, login: () => {}, logout: async () => {}, + update: async () => { + return false; + }, }); export function AuthProvider({ children }: PropsWithChildren) { @@ -70,25 +74,8 @@ export function AuthProvider({ children }: PropsWithChildren) { }); } - let data = await fetch(`${import.meta.env.VITE_BASE_API_URL}/me`, { - credentials: "include", - }) - .then((r) => r.json()) - .catch((e) => { - notify( - { - type: "danger", - loading: false, - title: "An error occured.", - message: `An error occured: ${e}`, - }, - 10000, - ); - }); - - if (data !== undefined) { - setUser(data); - notify( + if (await update()) { + notify( { type: "success", loading: false, @@ -98,7 +85,6 @@ export function AuthProvider({ children }: PropsWithChildren) { 2000, ); } - setLoading(false); }; @@ -140,8 +126,32 @@ export function AuthProvider({ children }: PropsWithChildren) { }); }; + const update = async () => { + let data = await fetch(`${import.meta.env.VITE_BASE_API_URL}/me`, { + credentials: "include", + }) + .then((r) => r.json()) + .catch((e) => { + notify( + { + type: "danger", + loading: false, + title: "An error occured.", + message: `An error occured: ${e}`, + }, + 10000, + ); + }); + + if (data !== undefined) { + setUser(data); + return true; + } + return false; + }; + return ( - + {children} );