add a function to reload the user data
This commit is contained in:
@@ -16,28 +16,33 @@ const InitialAuthState: IAuthState = {
|
|||||||
const AuthContext = createContext<{
|
const AuthContext = createContext<{
|
||||||
auth: IAuthState;
|
auth: IAuthState;
|
||||||
setAuth: Dispatch<SetStateAction<IAuthState>>;
|
setAuth: Dispatch<SetStateAction<IAuthState>>;
|
||||||
|
reloadAuth: () => void;
|
||||||
resetAuth: () => void;
|
resetAuth: () => void;
|
||||||
}>({ auth: InitialAuthState, setAuth: () => {}, resetAuth: () => {} });
|
}>({ auth: InitialAuthState, setAuth: () => {}, reloadAuth: () => {}, resetAuth: () => {} });
|
||||||
|
|
||||||
export const AuthProvider = ({ children }: { children: any }) => {
|
export const AuthProvider = ({ children }: { children: any }) => {
|
||||||
const [auth, setAuth] = useState<IAuthState>(InitialAuthState);
|
const [auth, setAuth] = useState<IAuthState>(InitialAuthState);
|
||||||
|
|
||||||
const resetAuth = () => setAuth(InitialAuthState);
|
const reloadAuth = () => {
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
Auth.user().then((response) => {
|
Auth.user().then((response) => {
|
||||||
if (response.status === BackendReturnStatus.Ok) {
|
if (response.status === BackendReturnStatus.Ok) {
|
||||||
setAuth({ valid: true, user: response.user });
|
setAuth({ valid: true, user: response.user });
|
||||||
} else {
|
} else {
|
||||||
setAuth(InitialAuthState);
|
setAuth(InitialAuthState);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetAuth = () => setAuth(InitialAuthState);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
reloadAuth();
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AuthContext.Provider value={{ auth, setAuth, resetAuth }}>
|
<AuthContext.Provider value={{ auth, setAuth, reloadAuth, resetAuth }}>
|
||||||
{children}
|
{children}
|
||||||
</AuthContext.Provider>
|
</AuthContext.Provider>
|
||||||
</>
|
</>
|
||||||
|
|||||||
Reference in New Issue
Block a user