mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 09:00:19 +00:00
133 lines
2.9 KiB
Markdown
133 lines
2.9 KiB
Markdown
# Authentification
|
|
|
|
## Register a new user.
|
|
|
|
This route lets you create new users.
|
|
|
|
#### Usage
|
|
|
|
```js
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'http://localhost:1337/auth/local/register',
|
|
data: {
|
|
username: 'Strapi user'
|
|
email: 'user@strapi.io',
|
|
password: 'strapiPassword'
|
|
},
|
|
done: function(auth) {
|
|
console.log('Well done!');
|
|
console.log('User profile', auth.user);
|
|
console.log('User token', auth.jwt);
|
|
},
|
|
fail: function(error) {
|
|
console.log('An error occurred:', error);
|
|
}
|
|
});
|
|
```
|
|
|
|
## Login.
|
|
|
|
This route lets you log your users in by getting an authentication token.
|
|
|
|
#### Usage
|
|
|
|
- The `identifier` param can either be an email or a username.
|
|
|
|
```js
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'http://localhost:1337/auth/local',
|
|
data: {
|
|
identifier: 'user@strapi.io',
|
|
password: 'strapiPassword'
|
|
},
|
|
done: function(auth) {
|
|
console.log('Well done!');
|
|
console.log('User profile', auth.user);
|
|
console.log('User token', auth.jwt);
|
|
},
|
|
fail: function(error) {
|
|
console.log('An error occurred:', error);
|
|
}
|
|
});
|
|
```
|
|
|
|
## Use your token to be identify as user.
|
|
|
|
By default, each API request is identified as `guest` role (see permissions of `guest`'s role in your admin dashboard). To make a request as a user, you have to set the `Authorization` token in your request headers. You receive a 401 error if you are not authorized to make this request or if your authorization header is not correct.
|
|
|
|
#### Usage
|
|
|
|
- The `token` variable is the `data.jwt` received when login in or registering.
|
|
|
|
```js
|
|
$.ajax({
|
|
type: 'GET',
|
|
url: 'http://localhost:1337/article',
|
|
headers: {
|
|
Authorization: `Bearer ${token}`
|
|
},
|
|
done: function(data) {
|
|
console.log('Your data', data);
|
|
},
|
|
fail: function(error) {
|
|
console.log('An error occurred:', error);
|
|
}
|
|
});
|
|
```
|
|
|
|
## Send forgot password request.
|
|
|
|
This action sends an email to a user with the link of you reset password page. This link contains an URL param `code` which is required to reset user password.
|
|
|
|
#### Usage
|
|
|
|
- `email` is your user email.
|
|
- `url` is the url link that user will receive.
|
|
|
|
```js
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'http://localhost:1337/auth/forgot-password',
|
|
data: {
|
|
email: 'user@strapi.io'
|
|
url: 'http://mon-site.com/rest-password'
|
|
},
|
|
done: function() {
|
|
console.log('Your user received an email');
|
|
},
|
|
fail: function(error) {
|
|
console.log('An error occurred:', error);
|
|
}
|
|
});
|
|
```
|
|
|
|
> Received link url format http://mon-site.com/rest-password?code=privateCode
|
|
|
|
## Reset user password.
|
|
|
|
This action will reset the user password.
|
|
|
|
#### Usage
|
|
|
|
- `code` is the url params received from the email link (see forgot password)
|
|
|
|
```js
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'http://localhost:1337/auth/reset-password',
|
|
data: {
|
|
code: 'privateCode'
|
|
password: 'myNewPassword'
|
|
passwordConfirmation: 'myNewPassword'
|
|
},
|
|
done: function() {
|
|
console.log('Your user password is reset');
|
|
},
|
|
fail: function(error) {
|
|
console.log('An error occurred:', error);
|
|
}
|
|
});
|
|
```
|