I have a login screen that gives access to a people viewing screen. I need to pass a cookie to the request that will call this list of people (which is an HTML file and inside it, has a JS file that gets these people from my API). Can anyone explain me how I can do this? Create two js files:
cookie-manager.js
var CookieManager = (function(){'
const set = function(token){
var date = new Date();
date.setTime(date.getTime() + 7*24*60*60*1000);
var expString = "expires=" + date.toGMTString();
document.cookie = 'token=${token};' + expString;
}
const get = function(cname){
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
return {
set : set,
get : get
}
auth-controller.js
var AuthController = (function(){
const loginAdmin = function(){
let formControl = new FormControl({
action : "http://localhost:8080/auth",
formElement : document.querySelector("#auth")
});
formControl.data = formControl.serialize(formControl.formElement);
formControl.data.roleForm = "ADMIN";
formControl.submit(function(response){
if(response.status && response.status != 200){
new Messages({
type : "danger",
messages : ["Credenciais Inválidas"]
}).build().show();
}else{
new Messages({
type : "info",
messages : ["Aguarde você está sendo redirecionado."]
}).build().show();
CookieManager.set(response.data.token);
setTimeout(function(){
CookieManager.get();
location.href = "tabela-participantes.html";
}, 3000);
}
});
}
return {
loginAdmin : loginAdmin
}
})();