0

Hello I'm trying to figure out how to use JWT and axios. I was wondering if you could explain me what's the point to add "bearer" and if I should use "Authorization" or "x-access-token" name please ?

Axios.get(`http://localhost:3001/posts`, {
      headers: { 'Authorization': `bearer ${token}` }
    }).then((response) => {
      console.log(response)
    });
User
  • 45
  • 5

1 Answers1

0

If I need to explain JWT Token in pretty simple words then I'll say something like this:

Access tokens are used in token-based authentication to allow access to an API. Access tokens are received after users successfully authenticate and authorize themselves.

And about the Authorization Token:

To set the authorization header, call it like this:

headers: { 'Authorization': `bearer ${token}`

Now the bearer token basically refers to the token type which in this case is a bearer type, read more here

The abstract of the provided link is something like this:

This specification describes how to use bearer tokens in HTTP requests to access OAuth 2.0 protected resources. Any party in possession of a bearer token (a "bearer") can use it to get access to the associated resources (without demonstrating possession of a cryptographic key). To prevent misuse, bearer tokens need to be protected from disclosure in storage and in transport.

Now let's get to the Authorization vs x-access-token

Authorization:

The HTTP Authorization request header contains the credentials to authenticate a user agent with a server, usually, but not necessarily, after the server has responded with a 401 Unauthorized status and the WWW-Authenticate header.

Read more about Authorization here

X-Access-Token:

In case of 'x-auth-token' user has to supply username/password for the first time and server returns a access-token in header field 'x-auth-token'. For further sessions this token is exchanged, not the username/password.

Now the conclusion. You'll use whatever your project really requires example if your working on an application which has a lot of users might use you'll have to implement as much security as possible which means using JWT Token and other security steps.

On the other hand if your application only requires one admin login to input some data and you have a pretty small window then you might want to go with session based login.

Community
  • 1
  • 1