Files
parcer/src/context/auth.tsx

94 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-09-24 12:07:32 +05:30
import { useReducer, createContext, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
2024-11-01 08:26:00 +05:30
import { apiUrl } from "../apiConfig";
2024-09-24 12:07:32 +05:30
2024-09-25 12:15:38 +05:30
interface AuthProviderProps {
2024-09-24 12:10:04 +05:30
children: React.ReactNode;
2024-09-24 12:07:32 +05:30
}
2024-09-24 12:08:19 +05:30
interface ActionType {
type: 'LOGIN' | 'LOGOUT';
payload?: any;
2024-09-24 12:10:04 +05:30
}
2024-09-24 12:08:19 +05:30
2024-09-24 12:07:32 +05:30
type InitialStateType = {
2024-09-24 12:10:04 +05:30
user: any;
2024-09-24 12:07:32 +05:30
};
const initialState = {
2024-09-24 12:10:04 +05:30
user: null,
2024-09-24 12:07:32 +05:30
};
2024-09-25 12:07:24 +05:30
const AuthContext = createContext<{
2024-09-24 12:10:04 +05:30
state: InitialStateType;
dispatch: React.Dispatch<ActionType>;
2024-09-24 12:07:32 +05:30
}>({
2024-09-24 12:10:04 +05:30
state: initialState,
dispatch: () => null,
2024-09-24 12:07:32 +05:30
});
2024-09-24 12:08:19 +05:30
const reducer = (state: InitialStateType, action: ActionType) => {
2024-09-24 12:10:04 +05:30
switch (action.type) {
case 'LOGIN':
return {
...state,
user: action.payload,
};
case 'LOGOUT':
return {
...state,
user: null,
};
default:
return state;
}
2024-09-24 12:07:32 +05:30
};
2024-09-25 12:15:38 +05:30
const AuthProvider = ({ children }: AuthProviderProps) => {
2024-09-24 12:10:04 +05:30
const [state, dispatch] = useReducer(reducer, initialState);
const navigate = useNavigate();
axios.defaults.withCredentials = true;
2024-09-24 12:07:32 +05:30
2024-09-24 12:10:04 +05:30
useEffect(() => {
const storedUser = window.localStorage.getItem('user');
if (storedUser) {
2024-11-18 21:26:23 +05:30
dispatch({ type: 'LOGIN', payload: JSON.parse(storedUser) });
}
2024-11-18 21:26:23 +05:30
}, []);
2024-09-24 12:10:04 +05:30
axios.interceptors.response.use(
2024-11-18 21:26:23 +05:30
function (response) {
return response;
},
function (error) {
const res = error.response;
2024-11-18 21:26:23 +05:30
if (res.status === 401 && res.config && !res.config.__isRetryRequest) {
2024-09-24 12:10:04 +05:30
return new Promise((resolve, reject) => {
axios
2024-11-01 08:26:00 +05:30
.get(`${apiUrl}/auth/logout`)
.then(() => {
2024-09-24 12:10:04 +05:30
console.log('/401 error > logout');
2024-11-18 21:26:23 +05:30
dispatch({ type: 'LOGOUT' });
window.localStorage.removeItem('user');
navigate('/login');
2024-09-24 12:10:04 +05:30
})
.catch((err) => {
console.error('AXIOS INTERCEPTORS ERROR:', err);
2024-09-24 12:10:04 +05:30
reject(error);
});
});
}
return Promise.reject(error);
}
);
2024-11-18 21:26:23 +05:30
2024-09-24 12:10:04 +05:30
return (
<AuthContext.Provider value={{ state, dispatch }}>
{children}
</AuthContext.Provider>
2024-09-24 12:10:04 +05:30
);
2024-09-24 12:07:32 +05:30
};
2024-11-11 19:59:51 +05:30
export { AuthContext, AuthProvider };