I'm building a react app where users get a subdomain on signup and when users log in or sign up on the main domain, I save their profile data to local storage and redirect them to their subdomain along with the token. I encode user profile data in this token like name, and username. When users get redirected to their subdomain, I decode the token and save the user data to the local storage for the subdomain so that I can access the logged-in user data from the subdomain. I am redirecting the user like this:
window.location.href = `http://${data?.result?.username}.localhost:3000?token=${data?.token}`;
But right now, what happens is when a user logs out from the subdomain but it doesn't log out from the main domain. I would like to know how I can log out a user from the main as well as the subdomain. I delete the user data from the local storage when a user clicks on the log out.
sign in code:
export const signin = (formData) => async (dispatch) => {
try {
// login the user
const { data } = await api.signIn(formData);
await dispatch({ type: "AUTH", data });
window.location.href = `http://${data?.result?.username}.localhost:3000?token=${data?.token}`;
} catch (error) {
dispatch({ type: "ERROR", data: error?.response?.data });
}
};
Reducer code:
const authReducer = (state = { authData: user }, action) => {
switch (action.type) {
case "AUTH":
localStorage.setItem("profile", JSON.stringify({ ...action?.data }));
return { ...state, authData: action?.data };
case "LOGOUT":
localStorage.clear();
return { ...state, authData: null };
default:
return state;
}
};
log out code:
const logout = () => {
dispatch({ type: "LOGOUT" });
history.push("/");
};