Authstate, Login, Logout. Store, dispatch etc

This commit is contained in:
2022-09-09 12:39:10 +02:00
parent d527500034
commit fc24ce1306
5 changed files with 77 additions and 50 deletions

View File

@@ -15,13 +15,12 @@ interface InitState {
}
const initialState: InitState = {
isLoading: false,
isLoading: true,
isAuthenticated: false,
currentUser: null,
};
const AuthProvider = (props: any) => {
const [store, setStore] = createStore(initialState);
const navigate = useNavigate();
@@ -35,16 +34,17 @@ const AuthProvider = (props: any) => {
onMount(async () => {
await loadCurrentUser();
setStore("isLoading", false);
});
const setCurrentUser = (user: UserType) => {
setStore("isAuthenticated", true);
setStore("currentUser", user);
setStore("isLoading", false);
};
const removeCurrentUser = () => {
setStore("isAuthenticated", false);
setStore("currentUser", null);
localStorage.removeItem("current_user");
};
return (