applied sign in changes

This commit was merged in pull request #1.
This commit is contained in:
Ebube
2024-02-12 18:04:45 +01:00
parent 501937728b
commit c81c00d5a6
3 changed files with 192 additions and 195 deletions
+55 -45
View File
@@ -1,18 +1,26 @@
/* eslint-disable react-refresh/only-export-components */
import {FC, useState, useEffect, createContext, useContext, Dispatch, SetStateAction} from 'react'
import {LayoutSplashScreen} from '../../../../_metronic/layout/core'
import {AuthModel, UserModel} from './_models'
import * as authHelper from './AuthHelpers'
import {getUserByToken} from './_requests'
import {WithChildren} from '../../../../_metronic/helpers'
import {
FC,
useState,
useEffect,
createContext,
useContext,
Dispatch,
SetStateAction,
} from "react";
import { LayoutSplashScreen } from "../../../../_metronic/layout/core";
import { AuthModel, UserModel } from "./_models";
import * as authHelper from "./AuthHelpers";
import { getUserByToken } from "./_requests";
import { WithChildren } from "../../../../_metronic/helpers";
type AuthContextProps = {
auth: AuthModel | undefined
saveAuth: (auth: AuthModel | undefined) => void
currentUser: UserModel | undefined
setCurrentUser: Dispatch<SetStateAction<UserModel | undefined>>
logout: () => void
}
auth: AuthModel | undefined;
saveAuth: (auth: AuthModel | undefined) => void;
currentUser: UserModel | undefined;
setCurrentUser: Dispatch<SetStateAction<UserModel | undefined>>;
logout: () => void;
};
const initAuthContextPropsState = {
auth: authHelper.getAuth(),
@@ -20,72 +28,74 @@ const initAuthContextPropsState = {
currentUser: undefined,
setCurrentUser: () => {},
logout: () => {},
}
};
const AuthContext = createContext<AuthContextProps>(initAuthContextPropsState)
const AuthContext = createContext<AuthContextProps>(initAuthContextPropsState);
const useAuth = () => {
return useContext(AuthContext)
}
return useContext(AuthContext);
};
const AuthProvider: FC<WithChildren> = ({children}) => {
const [auth, setAuth] = useState<AuthModel | undefined>(authHelper.getAuth())
const [currentUser, setCurrentUser] = useState<UserModel | undefined>()
const AuthProvider: FC<WithChildren> = ({ children }) => {
const [auth, setAuth] = useState<AuthModel | undefined>(authHelper.getAuth());
const [currentUser, setCurrentUser] = useState<UserModel | undefined>();
const saveAuth = (auth: AuthModel | undefined) => {
setAuth(auth)
setAuth(auth);
if (auth) {
authHelper.setAuth(auth)
authHelper.setAuth(auth);
} else {
authHelper.removeAuth()
authHelper.removeAuth();
}
}
};
const logout = () => {
saveAuth(undefined)
setCurrentUser(undefined)
}
saveAuth(undefined);
setCurrentUser(undefined);
};
return (
<AuthContext.Provider value={{auth, saveAuth, currentUser, setCurrentUser, logout}}>
<AuthContext.Provider
value={{ auth, saveAuth, currentUser, setCurrentUser, logout }}
>
{children}
</AuthContext.Provider>
)
}
);
};
const AuthInit: FC<WithChildren> = ({children}) => {
const {auth, currentUser, logout, setCurrentUser} = useAuth()
const [showSplashScreen, setShowSplashScreen] = useState(true)
const AuthInit: FC<WithChildren> = ({ children }) => {
const { auth, currentUser, logout, setCurrentUser } = useAuth();
const [showSplashScreen, setShowSplashScreen] = useState(true);
// We should request user by authToken (IN OUR EXAMPLE IT'S API_TOKEN) before rendering the application
useEffect(() => {
const requestUser = async (apiToken: string) => {
try {
if (!currentUser) {
const {data} = await getUserByToken(apiToken)
const { data } = await getUserByToken(apiToken);
if (data) {
setCurrentUser(data)
setCurrentUser(data);
}
}
} catch (error) {
console.error(error)
console.error(error);
if (currentUser) {
logout()
logout();
}
} finally {
setShowSplashScreen(false)
setShowSplashScreen(false);
}
}
};
if (auth && auth.api_token) {
requestUser(auth.api_token)
requestUser(auth.api_token);
} else {
logout()
setShowSplashScreen(false)
logout();
setShowSplashScreen(false);
}
// eslint-disable-next-line
}, [])
}, []);
return showSplashScreen ? <LayoutSplashScreen /> : <>{children}</>
}
return showSplashScreen ? <LayoutSplashScreen /> : <>{children}</>;
};
export {AuthProvider, AuthInit, useAuth}
export { AuthProvider, AuthInit, useAuth };