File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change 11/* eslint-disable no-unused-vars */
22import { createContext , useState , useEffect } from 'react' ;
3- import axiosInstance from "../Components/utils/Axios"
43// eslint-disable-next-line react-refresh/only-export-components
54export const AuthContext = createContext ( ) ;
65
76// eslint-disable-next-line react/prop-types
87export const AuthProvider = ( { children } ) => {
98 const [ isAuthenticated , setIsAuthenticated ] = useState ( false ) ;
109 const [ user , setUser ] = useState ( null ) ;
10+ const [ isLoading , setIsLoading ] = useState ( true ) ;
1111
1212const checkAuthStatus = async ( ) => {
1313 const apiResponse = await axiosInstance . get ( `${ import . meta. env . VITE_BACKEND_URL } /` )
14- if ( apiResponse . status === 401 ) {
15- setIsAuthenticated ( false ) ;
16- setUser ( null ) ;
17- } else if ( apiResponse . status === 200 ) {
14+ if ( apiResponse . status === 200 && apiResponse . data . userInfo ) {
1815 setIsAuthenticated ( true ) ;
1916 setUser ( apiResponse . data . userInfo ) ;
17+ } else {
18+ setIsAuthenticated ( false )
19+ setUser ( null )
2020 }
21+ setIsLoading ( false )
2122}
2223 useEffect ( ( ) => {
23- if ( user == null ) {
2424 checkAuthStatus ( ) ;
25- }
2625 } , [ user ] )
2726
2827 const authContextValue = {
2928 isAuthenticated,
3029 user,
3130 setUser,
32- setIsAuthenticated
31+ setIsAuthenticated, isLoading , setIsLoading
3332 } ;
3433
3534 return (
You can’t perform that action at this time.
0 commit comments