Browse Source

add a function to reload the user data

Sven Czarnian 2 years ago
parent
commit
2cced49406
1 changed files with 11 additions and 6 deletions
  1. 11 6
      src/contexts/authcontext.tsx

+ 11 - 6
src/contexts/authcontext.tsx

@@ -16,28 +16,33 @@ const InitialAuthState: IAuthState = {
 const AuthContext = createContext<{
   auth: IAuthState;
   setAuth: Dispatch<SetStateAction<IAuthState>>;
+  reloadAuth: () => void;
   resetAuth: () => void;
-}>({ auth: InitialAuthState, setAuth: () => {}, resetAuth: () => {} });
+}>({ auth: InitialAuthState, setAuth: () => {}, reloadAuth: () => {}, resetAuth: () => {} });
 
 export const AuthProvider = ({ children }: { children: any }) => {
   const [auth, setAuth] = useState<IAuthState>(InitialAuthState);
 
-  const resetAuth = () => setAuth(InitialAuthState);
-
-  useEffect(() => {
+  const reloadAuth = () => {
     Auth.user().then((response) => {
       if (response.status === BackendReturnStatus.Ok) {
         setAuth({ valid: true, user: response.user });
       } else {
         setAuth(InitialAuthState);
       }
-    })
+    });
+  };
+
+  const resetAuth = () => setAuth(InitialAuthState);
+
+  useEffect(() => {
+    reloadAuth();
   // eslint-disable-next-line react-hooks/exhaustive-deps
   }, []);
 
   return (
     <>
-      <AuthContext.Provider value={{ auth, setAuth, resetAuth }}>
+      <AuthContext.Provider value={{ auth, setAuth, reloadAuth, resetAuth }}>
         {children}
       </AuthContext.Provider>
     </>