-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauthProvider.js
More file actions
39 lines (39 loc) · 1.35 KB
/
authProvider.js
File metadata and controls
39 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_CHECK } from 'react-admin';
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { email, password } = params;
const request = new Request('/apiv1/users/login', {
method: 'POST',
body: JSON.stringify({ email, password }),
headers: new Headers({ 'Content-Type': 'application/json' }),
})
return fetch(request)
.then(response => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
return response.json();
})
.then((response) => {
if (!response.isAdmin) {
return Promise.reject();
}
localStorage.setItem('token', response.token);
});
}
if (type === AUTH_LOGOUT) {
localStorage.removeItem('token');
}
if (type === AUTH_ERROR) {
const status = params.status;
if (status === 401 || status === 403) {
localStorage.removeItem('token');
return Promise.reject();
}
return Promise.resolve();
}
if (type === AUTH_CHECK) {
return localStorage.getItem('token') ? Promise.resolve() : Promise.reject();
}
return Promise.resolve();
}