This is a commercial project, so I can't show everything. The login is made and when I log in, I get a token and user data, but as soon as I refresh the page or go back to the profile, the data disappears, this is due to the refresh of the token, which works fine on the main page in the console, but it does not work in the profile , the data must be overwritten via redux. I will show parts of the code associated with the token.
action accessToken
import actionTypes from "./actionTypes";
export const setToken = (token) => {
return (dispatch) => {
dispatch({ type: actionTypes.setToken, payload: token });
};
};
export const resetToken = () => {
return (dispatch) => {
dispatch({ type: actionTypes.resetToken});
};
};
export default { setToken, resetToken };
reducersAccessToken
import actionTypes from "../actions/actionTypes";
const initialState = null;
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.setToken:
return action.payload;
case actionTypes.resetToken:
return action.payload;
default:
return state;
}
};
export default reducer;
mainPage when going on get refreshToken
const dispatch = useDispatch();
const { setToken } = bindActionCreators(accessTokenActions, dispatch);
const { setCurrentUser } = bindActionCreators(currentUserActions, dispatch);
const axiosPrivate = useAxiosPrivate();
useEffect(() => {
const checkAuth = async () => {
const response = await axiosPrivate.post(`${baseUrl}/api/auth/refresh`);
if (response.data.status === 200) {
setToken(response.data.body.token);
setCurrentUser(response.data.body.user);
}
console.log(response)
};
checkAuth();
}, []);