How to store auth session?
How do I store the auth session so it stops asking to me log in on every page?
login method that sets the user as authenticated and stores the necessary information (e.g., token) in the local storage. Here's an example of how an auth provider is set up in Refine with Auth0:handlelogin function to use the login method from your authProvider. This way, refine will handle the authentication state and redirection for you. npm run dev to start it again.handleloginconst App = () => {
const { isLoading, isAuthenticated, user, logout, getIdTokenClaims } = useAuth0();
if (isLoading) {
return <span>loading...</span>;
}
const authProvider: AuthBindings = {
login: async () => {
return {
success: true,
};
},
logout: async () => {
logout({ returnTo: window.location.origin });
return {
success: true,
};
},
onError: async (error) => {
console.error(error);
return { error };
},
check: async () => {
try {
const token = await getIdTokenClaims();
if (token) {
axios.defaults.headers.common = {
Authorization: `Bearer ${token.__raw}`,
};
return {
authenticated: true,
};
} else {
return {
authenticated: false,
error: {
message: "Check failed",
name: "Token not found",
},
redirectTo: "/login",
logout: true,
};
}
} catch (error: any) {
return {
authenticated: false,
error: new Error(error),
redirectTo: "/login",
logout: true,
};
}
return {
authenticated: false,
error: {
message: "Check failed",
name: "Token not found",
},