I want to acces my plateform that use an API with a route protected. I need to be logged to access the platform. I am using JWT token for it. The login worked perfectly. The only problem i have it is to fetch data from a protected route where i need to pass my token in the header.
I am having this: status: "Authorization Token not found"
seems like my token not was find. This is the code i made bellow:
LOGIN PAGE
<script>
function login() {
var email = $("#email").val();
var password = $("#password").val();
var dadosjson = JSON.stringify({
"email": email,
"password": password
});
$.ajax({
url: "https:/api/login",
dataType: 'json',
data: dadosjson,
type: 'POST',
contentType: 'application/json',
success: function(data) {
sessionStorage.setItem("token", data.token);
window.location.href = "home.php";
},
error: function(data) {
alert("Impossível recuperar dados");
}
});
}
</script>
Once the user passed to the login page, it will redirect him to the home.php (home page)
<script>
$(document).ready(function() {
$.ajax({
//Incluir no cabeçalho HTTP a forma de autenticação passando o token
headers: {
"Authorization": "Bearer" + sessionStorage.getItem("token")
},
//Informar a URL do recurso desejado
url: 'https:/api/balance',
contentType: 'application/json',
dataType: 'json',
//Informar o método da requisição, no caso, GET
type: 'GET',
//Ação, caso sucesso
success: function(data) {
// $("#usuario-nome_usual").html(data.name);
// $("#usuario-email").html(data.email);
console.log(data);
},
//Ação, caso erro
error: function(data) {
alert("Impossível recuperar dados. Você deve fazer login!");
// window.location.href = "login.html";
}
});
});
</script>