Home > Enterprise >  ajax JWT Token not working even though it is header authorization is present
ajax JWT Token not working even though it is header authorization is present

Time:09-21

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>

CodePudding user response:

In

$.ajax({
  //Incluir no cabeçalho HTTP a forma de autenticação passando o token
  headers: {
    "Authorization" : "Bearer"   sessionStorage.getItem("token")
  },
  ...

There must be a space between the Bearer keyword and the JWT token.

2.1. Authorization Request Header Field

When sending the access token in the "Authorization" request header field defined by HTTP/1.1 [RFC2617], the client uses the "Bearer"
authentication scheme to transmit the access token.

For example:

 GET /resource HTTP/1.1
 Host: server.example.com
 Authorization: Bearer mF_9.B5f-4.1JqM

The syntax of the "Authorization" header field (...) is as follows:

 b64token    = 1*( ALPHA / DIGIT /
                   "-" / "." / "_" / "~" / " " / "/" ) *"="
 credentials = "Bearer" 1*SP b64token

In other words

  • Related