How can I set JWT token in headers?
I'm sending the username and password through AJAX call, and receiving the response 200 with JWT token. I'm setting that token in the session storage. How can I set it in headers to check that token exists until the end of the session?
Was my approach correct? Send credentials in body > receive response (success) >login successful> store the token in session storage and set token it in headers> logout when session get expired
function validate() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var data = {
username: username,
password: password
}
console.log(data);
$.ajax({
type:"post",
cache:false,
url:"xyz",
data:data, /
success: function (result) {
console.log(result); //will have the status and jwt if it's success
}
});
};
storing the jwt
let key="token";
sessionStorage.setItem(key, jwt);// will get the jwt from the result.