Skip to content

Commit 398fa61

Browse files
committed
Modified AuthContext
1 parent 1e0f7a4 commit 398fa61

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/context/AuthContext.jsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
/* eslint-disable no-unused-vars */
22
import { createContext, useState , useEffect} from 'react';
3-
import axiosInstance from "../Components/utils/Axios"
43
// eslint-disable-next-line react-refresh/only-export-components
54
export const AuthContext = createContext();
65

76
// eslint-disable-next-line react/prop-types
87
export const AuthProvider = ({ children }) => {
98
const [isAuthenticated, setIsAuthenticated] = useState(false);
109
const [user, setUser] = useState(null);
10+
const [isLoading, setIsLoading] = useState(true);
1111

1212
const 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 (

0 commit comments

Comments
 (0)