You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/Authentication.md
+78-3
Original file line number
Diff line number
Diff line change
@@ -182,9 +182,11 @@ Note that the `authProvider.logout()` method can return the url to which the use
182
182
183
183
If the API requires authentication, and the user credentials are missing in the request or invalid, the API usually answers with an HTTP error code 401 or 403.
184
184
185
-
Fortunately, each time the API returns an error, react-admin calls the `authProvider.checkError()` method. Once again, it's up to you to decide which HTTP status codes should let the user continue (by returning a resolved promise) or log them out (by returning a rejected promise).
185
+
Fortunately, each time the API returns an error, react-admin calls the `authProvider.checkError()` method. When `checkError()` returns a rejected promise, react-admin calls the `authProvider.logout()` method.
186
186
187
-
For instance, to redirect the user to the login page for both 401 and 403 codes:
187
+
So it's up to you to decide which HTTP status codes should let the user continue (by returning a resolved promise) or log them out (by returning a rejected promise).
188
+
189
+
For instance, to log the user out for both 401 and 403 codes:
188
190
189
191
```js
190
192
// in src/authProvider.js
@@ -198,13 +200,54 @@ export default {
198
200
localStorage.removeItem('auth');
199
201
returnPromise.reject();
200
202
}
203
+
// other error code (404, 500, etc): no need to log out
204
+
returnPromise.resolve();
205
+
},
206
+
// ...
207
+
};
208
+
```
209
+
210
+
When `authProvider.checkError()` returns a rejected Promise, react-admin redirects to the `/login` page, or to the `error.redirectTo` url. That means you can override the default redirection as follows:
// other error code (404, 500, etc): no need to log out
201
225
returnPromise.resolve();
202
226
},
203
227
// ...
204
228
};
205
229
```
206
230
207
-
Note that when `checkError()` returns a rejected promise, react-admin calls the `authProvider.logout()` method before redirecting, and uses the url which may have been returned by the call to `logout()`.
231
+
When `authProvider.checkError()` returns a rejected Promise, react-admin displays a notification to the end user, unlsee the `error.message` is `false`. That means you can disable the notification on error as follows:
232
+
233
+
```js
234
+
// in src/authProvider.js
235
+
exportdefault {
236
+
login: ({ username, password }) => { /* ... */ },
237
+
getIdentity: () => { /* ... */ },
238
+
logout: () => { /* ... */ },
239
+
checkError: (error) => {
240
+
conststatus=error.status;
241
+
if (status ===401|| status ===403) {
242
+
localStorage.removeItem('auth');
243
+
returnPromise.reject({ message:false });
244
+
}
245
+
// other error code (404, 500, etc): no need to log out
246
+
returnPromise.resolve();
247
+
},
248
+
// ...
249
+
};
250
+
```
208
251
209
252
## Checking Credentials During Navigation
210
253
@@ -246,6 +289,38 @@ export default {
246
289
247
290
Note that react-admin will call the `authProvider.logout()` method before redirecting. If you specify the `redirectTo` here, it will override the url which may have been returned by the call to `logout()`.
248
291
292
+
If the promise is rejected, react-admin displays a notification to the end user. You can customize this message by rejecting an error with a `message` property:
293
+
294
+
```js
295
+
// in src/authProvider.js
296
+
exportdefault {
297
+
login: ({ username, password }) => { /* ... */ },
298
+
getIdentity: () => { /* ... */ },
299
+
logout: () => { /* ... */ },
300
+
checkError: (error) => { /* ... */ },
301
+
checkAuth: () =>localStorage.getItem('auth')
302
+
?Promise.resolve()
303
+
:Promise.reject({ message:'login.required' }), // react-admin passes the error message to the translation layer
304
+
// ...
305
+
}
306
+
```
307
+
308
+
You can also disable this notification completely by rejecting an error with a `message` with a `false` value:
309
+
310
+
```js
311
+
// in src/authProvider.js
312
+
exportdefault {
313
+
login: ({ username, password }) => { /* ... */ },
314
+
getIdentity: () => { /* ... */ },
315
+
logout: () => { /* ... */ },
316
+
checkError: (error) => { /* ... */ },
317
+
checkAuth: () =>localStorage.getItem('auth')
318
+
?Promise.resolve()
319
+
:Promise.reject({ message:false }),
320
+
// ...
321
+
}
322
+
```
323
+
249
324
**Tip**: In addition to `login()`, `logout()`, `checkError()`, and `checkAuth()`, react-admin calls the `authProvider.getPermissions()` method to check user permissions. It's useful to enable or disable features on a per user basis. Read the [Authorization Documentation](./Authorization.md) to learn how to implement that type.
0 commit comments