add a function to reload the user data
This commit is contained in:
@@ -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>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user