I have a JavaScript project that consumes an API which sends a specific responseCode and returns the role, according to that role, it is redirected to the page that the user should see, but I need that when placing the URL it does not allow him to see unless logged in, if not logged in, return to login.
if (code == 0) {
console.log('Usuario existe con rol: ' + rol, ' El mensaje del API fue: ' + message);
if (rol == 'Administrador') {
window.location.href = 'support/management/dashboard.html';
} else {
window.location.href = 'support/user/home.html?auth=true';
}
} else {
console.log('No permisos');
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Usuario y/o Contraseña Incorrecto, verifique.'
});
}
This would be basic JavaScript logic, and I'd like any page other than index.html not to be accessible unless logged in.
You can save the role in
localStorage
. Basically it saves data that persists ( here more infokeep it like this
Then in each
html
you can do get therol
from thelocalStorage
and validate according to what you need
either
NOTE
The variable will persist
localStorage
until it is deleted. For that you can create an actionlog out
and once you do it delete the variable.Also if you want to only keep the variable in which the tab is open and once closed it is deleted, you can use
sessionStorage
(more info about sessionStorage)