Skip to content

Commit 33882ac

Browse files
committed
Adding a Login Page
Based on a custom authProvider that accepts anyone via storage of username and password in local storage
1 parent 65b75bf commit 33882ac

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { Admin, Resource } from 'react-admin';
33
import Dashboard from './Dashboard';
4+
import authProvider from './authProvider';
45
import { PostList, PostEdit, PostCreate } from './posts';
56
import { UserList } from './users';
67
import jsonServerProvider from 'ra-data-json-server';
@@ -9,7 +10,7 @@ import UserIcon from '@material-ui/icons/Group';
910

1011
const dataProvider = jsonServerProvider('http://jsonplaceholder.typicode.com');
1112
const App = () => (
12-
<Admin dashboard={Dashboard} dataProvider={dataProvider}>
13+
<Admin dashboard={Dashboard} authProvider={authProvider} dataProvider={dataProvider}>
1314
<Resource name="users" list={UserList} icon={UserIcon} />
1415
<Resource name="posts" list={PostList} edit={PostEdit} create={PostCreate} icon={PostIcon} />
1516
</Admin>

src/authProvider.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default {
2+
// called when the user attempts to log in
3+
login: ({ username }) => {
4+
localStorage.setItem('username', username);
5+
// accept all username/password combinations
6+
return Promise.resolve();
7+
},
8+
// called when the user clicks on the logout button
9+
logout: () => {
10+
localStorage.removeItem('username');
11+
return Promise.resolve();
12+
},
13+
// called when the API returns an error
14+
checkError: ({ status }) => {
15+
if (status === 401 || status === 403) {
16+
localStorage.removeItem('username');
17+
return Promise.reject();
18+
}
19+
return Promise.resolve();
20+
},
21+
// called when the user navigates to a new location, to check for authentication
22+
checkAuth: () => {
23+
return localStorage.getItem('username')
24+
? Promise.resolve()
25+
: Promise.reject();
26+
},
27+
// called when the user navigates to a new location, to check for permissions / roles
28+
getPermissions: () => Promise.resolve(),
29+
};

0 commit comments

Comments
 (0)