@Nazar Medeiros - Your solution uses passport with Express. I am not using passport, just express-jwt. I might be doing something wrong, but when a user logs in, the token needs to return to the client side. From what I have found so far, this means we have to return a json with the token and therefor cannot call redirect. Is there something I am missing there?
To get around this, I simply return the token, store it in my cookies and then make a ajax GET request (with the valid token). When that ajax call returns I replace the body's html with the returned HTML. This is probably not the right way to do it, but I can't find a better way. Here is my JQuery JavaScript code.
function loginUser(){
$.post("/users/login", {
username: $( '#login_input_username' ).val(),
password: $( '#login_input_password' ).val()
}).done(function(res){
document.cookie = "token = " + res.token;
redirectToHome();
})
}
function redirectToHome(){
var settings = {
"async": true,
"crossDomain": true,
"url": "/home",
"type": "GET",
"headers": {
"authorization": "Bearer " + getCookie('token'),
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
$('body').replaceWith(response);
});
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i