Jay
Jay

Reputation: 173

The server responded with a status of 401 (Unauthorized) [Strapi]

I am trying to build an app with Nuxt.js (not modified at all) connect it to a Strapi api and I am completely new to this. I followed the tutorial on the Strapi blog to deploy the Strapi api to Heroku and the Nuxt.js app runs locally. Both instances run fine on their own.

But now I when I try a get request (see below) I get the 401 (Unauthorized) response.

axios.get(
    'url',
    {headers: {
      Authorization: 'Bearer ${token}'}
    }
)

I tried creating different users and changing permissions. But I always get:

"statusCode": 401, "error": "Unauthorized", "message": "Invalid token."

I am not sure what I am doing wrong. I thought the password in the Strapi user panel is the JWT. Am I mistaken? I tried searching the documentation, stackoverflow, ...but couldn't solve my problem. Thank you for your help!

Upvotes: 1

Views: 19947

Answers (2)

Aurélien Georget
Aurélien Georget

Reputation: 266

Based on the documentation, you need to login (POST /auth/local) to retrieve the JWT and use it in the Authorization header for requesting a resource.

Upvotes: 2

Renderlife
Renderlife

Reputation: 60

You need to use identifier, not username or email

https://strapi.io/documentation/developer-docs/latest/guides/auth-request.html#setup

const { data } = await axios.post('http://localhost:1337/auth/local', {
  identifier: '[email protected]',
  password: 'strapi',
});

Upvotes: 1

Related Questions