Authentication on Strapi

hi Team,

How to implement authentication on strapi .

We would like to token based authentication . For example oauth.

please advice.

Thanks!

1 Like

Authentication

Hi ,

do you have sample to implement authentication? i am new to strapi. i am not able to follow the user guide.

if possible please provide a sample file .

please do the needful.

Thanks!

It is specific to your frontend, what do you use? React, vue, ajax, vanilla?

we are going to use react. thanks!

Login

import axios from 'axios';

axios
  .post('http://localhost:1337/auth/local', {
    identifier: 'user@strapi.io',
    password: 'strapiPassword',
  })
  .then(response => {
    //Store user token in cookie or localstorage, then use it in all next requests.
    console.log('Your user token', response.data.jwt);
  })
  .catch(error => {
    // Something wrong with auth. Wrong credentials maybe.
    console.log('An error occurred:', error.response);
  });

Get data as authenticated user

axios
  .get('http://localhost:1337/posts', {
    headers: {
      //Use token that you stored from previous request
      Authorization: `Bearer ${token}`,
    },
  })
  .then(response => {
    // Received all posts as authenticated user
    console.log('Posts: ', response.data);
  })

thanks for your information.

how to validate JWT in strapi project?

I have followed Authenticated request - Strapi Developer Documentation for strapi.

Where to find the details about auth/local Api in strapi?

Can you clarify, if you are wanting to customize this: JWT validation - Strapi Developer Documentation

Not sure what you mean, what information are you looking for, can you give an example?

Hi ,

I would like to implement JWT Validation. I have copied user-permissions module from github and paste it into my project. i tried to test http://localhost:1337/auth/local (post method). i am getting

{“statusCode”:400,“error”:“Bad Request”,“message”:[{“messages”:[{“id”:“Auth.form.error.email.provide”,“message”:“Please provide your username or your e-mail.”}]}],“data”:[{“messages”:[{“id”:“Auth.form.error.email.provide”,“message”:“Please provide your username or your e-mail.”}]}]}

plesae advice.

Thanks

1 Like

Can you provide an example of your code?

did you also provided a json with:

{
  "identifier": "registered.name@email.com",
  "password": "same-as-the-one-set",
}

… or a form having same fields?

That api endpoint is for authentication, the response will be a json containg the autorization jwt, once you get it you can use it to authorize access to protected api routes, like: /users/me

Hi

I am not able to attached the zip file . its expecting image.

i have followed the below page

Hi

if possible please provide a sample for JWT authentication. Not able to follow login information .

if you provide a sample , it will be well and good. Sorry!

We planned to deploy it in azure and we will pass JWT to Strapi to validate.

Thanks!

Hello,

I’ve shared already a working example above. It should be very simple to make an auth request.
Please attach your code here(as text) or screenshot, otherwise we can’t identify why you get 404 error.

Thanks

Maybe it will be easier for you to watch a video

Hi,

I have corrected Auth.js.Now i can able to login . i have tested it with postman. API (http://localhost:1337/Auth/local) post method is returning

{“jwt”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjAzODUyMjA5LCJleHAiOjE2MDY0NDQyMDl9.Y6f-uN2cJcW_KhPw-xP6lxIDnva-G8wXihiUEq9DBco”,“user”:{“username":“UserName1”,“email”:"username1@email.com”,“provider”:“local”,“confirmed”:true,“blocked”:false,“role”:{“name”:“Authenticated”,“description”:“Default role given to authenticated user.”,“type”:“authenticated”},“user_groups”:[{“groupname”:“group1”}]}}

But i tried to display ctx.state.user field. its not returning “user_groups” details. Why?

In my user model , i have added “user_groups” as relation field. API is returning properly,.

But ctx.state.user object is not having my custom field. the below code is not returning my custom field.

// fetch authenticated user

  ctx.state.user = await strapi.plugins[

    'users-permissions'

  ].services.user.fetchAuthenticatedUser(id);

Hi team,

do you have any idea about this issue?

Please update me

Thanks!