Most of the web applications require a user management system: registration, login, reset password, etc.
To avoid you to reinvent the wheel, Strapi embedded a full featured user management system powered by Grant and JSON Web Token (JWT).
Route used to register a user to your application: POST /auth/local/register
.
Request payload:
{
"username": "John DOE",
"email": "[email protected]",
"password": "123456"
}
Response payload:
{
"user": {},
"jwt": ""
}
Route used to login a user to your application: POST /auth/local
.
Request payload:
{
"identifier": "[email protected]",
"password": "123456"
}
Response payload:
{
"user": {},
"jwt": ""
}
JWT does not use session. Once you get the token, it has to be stored in front (for
example in the localstorage
), and sent within each request. The token can be sent:
- in the header (
Bearer
) - in the body (
token
field) - in the querystring (
token
field)
Thanks to Grant and Purest, you can easily use OAuth and OAuth2 providers to enable authentication in your application. By default, Strapi comes with four providers:
- Github
- Linkedin2 (Oauth2 Provider for Linkedin)
To use the providers authentication, set your credentials in
./api/user/config/environments/development/grant.json
.
Redirect your user to: GET /connect/:provider
.
After his approval, he will be redirected to /auth/:provider/callback
. The jwt and user will be available in the querystring.
Response payload:
{
"user": {},
"jwt": ""
}
Strapi comes with 5 providers. If you want to add another one, it can be easily done thanks to Purest, by adding it in the Grant service.
Send an email to the user with an activation code: POST /auth/forgot-password
.
Request payload:
{
"email": "[email protected]"
}
Route used to update the password of a user after he asked for a
"forgot-password" email: POST /auth/change-password
.
Request payload:
{
"code": "",
"password": "123456",
"passwordConfirmation": "123456"
}
Response payload:
{
"user": {},
"jwt": ""
}
If you want to access attributes of the logged in user, you can use this.user
inside of your controller action.