function isLoggedIn() {
return !!localStorage.getItem('token');
}
This function will return true if the 'token' key exists in the localStorage.
load
function of the pages that require authentication, check if the user is authenticated:export async function load({ session }) {
if (!isLoggedIn()) {
return {
status: 401,
redirect: '/login'
};
}
// Return the data for the page
}
This will redirect the user to the login page if they are not authenticated.
localStorage.setItem('token', token);
localStorage.removeItem('token');
This will log the user out and remove the authentication token from the localStorage.
By following these steps, your routes will be protected and only accessible to authenticated users.
Asked: 2021-05-22 11:00:00 +0000
Seen: 22 times
Last updated: Aug 24 '21