1- import {
2- FC ,
3- ReactNode ,
4- createContext ,
5- useContext ,
6- useEffect ,
7- useState ,
8- } from "react" ;
1+ import { FC , ReactNode , createContext , useContext , useState } from "react" ;
92import { useLocation , useNavigate } from "react-router-dom" ;
103import { useSnackbar } from "notistack" ;
114
@@ -34,7 +27,6 @@ interface AuthContextType {
3427 resetPassword : ( email : string ) => Promise < void > ;
3528 setNewPassword : ( newPassword : string ) => Promise < void > ;
3629 confirmToken : ( token : string ) => Promise < void > ;
37- isAuth : boolean ;
3830}
3931
4032const AuthContext = createContext < AuthContextType > ( {
@@ -45,7 +37,6 @@ const AuthContext = createContext<AuthContextType>({
4537 resetPassword : async ( ) => { } ,
4638 setNewPassword : async ( ) => { } ,
4739 confirmToken : async ( ) => { } ,
48- isAuth : false ,
4940} ) ;
5041
5142export const useAuth = ( ) => {
@@ -58,7 +49,6 @@ export const useAuth = () => {
5849
5950export const AuthProvider : FC < IAuthProvider > = ( { children } ) => {
6051 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
61- const [ isAuth , setIsAuth ] = useState < boolean > ( false ) ;
6252 const navigate = useNavigate ( ) ;
6353 const { enqueueSnackbar } = useSnackbar ( ) ;
6454 const location = useLocation ( ) ;
@@ -69,41 +59,20 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
6959 const [ checkToken ] = useCheckResetPasswordTokenLazyQuery ( ) ;
7060
7161 const { loading : rolesLoading } = useUserRolesQuery ( {
72- skip : ! isAuth ,
62+ skip : ! ( localStorage . getItem ( " isAuth" ) === "true" ) ,
7363 onCompleted : ( data ) => {
7464 userRolesVar ( data ?. user ?. roles ) ;
7565 } ,
7666 } ) ;
7767
78- useEffect ( ( ) => {
79- const checkAuth = ( ) => {
80- const auth = localStorage . getItem ( "isAuth" ) ;
81-
82- if ( auth ) {
83- setIsAuth ( true ) ;
84- } else {
85- setIsAuth ( false ) ;
86- }
87-
88- setIsLoading ( false ) ;
89- } ;
90-
91- checkAuth ( ) ;
92- } , [ ] ) ;
93-
94- useEffect ( ( ) => {
95- if ( ! rolesLoading && ! isLoading ) {
96- setIsLoading ( false ) ;
97- }
98- } , [ rolesLoading , isLoading ] ) ;
68+ const loading = isLoading || rolesLoading ;
9969
10070 const login = async ( username : string , password : string ) => {
10171 setIsLoading ( true ) ;
10272 await AuthService . login ( username , password )
10373 . then ( ( response ) => {
10474 if ( response . status === RESPONSE_STATUS . SUCCESSFUL ) {
10575 localStorage . setItem ( "isAuth" , "true" ) ;
106- setIsAuth ( true ) ;
10776 setIsLoading ( false ) ;
10877 navigate ( ROUTES . HOME ) ;
10978 } else {
@@ -130,7 +99,6 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
13099 . then ( ( response ) => {
131100 if ( response . status === RESPONSE_STATUS . SUCCESSFUL ) {
132101 localStorage . removeItem ( "isAuth" ) ;
133- setIsAuth ( false ) ;
134102 setIsLoading ( false ) ;
135103 navigate ( ROUTES . AUTHORIZATION ) ;
136104 } else {
@@ -240,8 +208,7 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
240208 return (
241209 < AuthContext . Provider
242210 value = { {
243- isAuth,
244- isLoading,
211+ isLoading : loading ,
245212 login,
246213 logout,
247214 signup,
0 commit comments