Skip to content

Commit d3fc2d8

Browse files
committed
Updata auth
1 parent 683286b commit d3fc2d8

File tree

12 files changed

+94
-15
lines changed

12 files changed

+94
-15
lines changed

json-server.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"prettier": "^2.1.2"
3838
},
3939
"scripts": {
40-
"json-server": "json-server --watch server/db.json",
40+
"json-server": "json-server --watch server/db/db.json --routes server/routes.json --port 5000",
4141
"dev": "react-scripts start",
4242
"start": "concurrently \"yarn json-server\" \"yarn dev\"",
4343
"build": "react-scripts build",
@@ -63,4 +63,4 @@
6363
"last 1 safari version"
6464
]
6565
}
66-
}
66+
}

src/components/Auth/Login/Login.actions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ export const loginFailed = payload => ({
1313
type: types.LOGIN_FAILED,
1414
payload,
1515
});
16+
17+
export const userLoaded = payload => ({
18+
type: types.USER_LOADED,
19+
payload,
20+
});
21+
22+
export const authError = () => ({
23+
type: types.AUTH_ERROR,
24+
});
25+
26+
export const logout = () => ({
27+
type: types.LOGOUT,
28+
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
export const LOGIN_REQUESTED = 'views/login/LOGIN_REQUESTED';
2-
export const LOGIN_SUCCESS = 'views/login/LOGIN_SUCCESS';
3-
export const LOGIN_FAILED = 'views/login/LOGIN_FAILED';
1+
export const LOGIN_SUCCESS = 'views/auth/LOGIN_SUCCESS';
2+
export const LOGIN_FAILED = 'views/auth/LOGIN_FAILED';
3+
export const LOGIN_REQUESTED = 'views/auth/LOGIN_REQUESTED';
4+
export const USER_LOADED = 'views/auth/USER_LOADED';
5+
export const AUTH_ERROR = 'views/auth/AUTH_ERROR';
6+
export const LOGOUT = 'views/auth/LOGOUT';

src/components/Auth/Login/Login.reducers.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@ import * as types from './Login.constants';
22
import produce from 'immer';
33

44
const initialState = {
5-
loading: false,
5+
loading: true,
66
isAuthenticated: false,
7+
token: localStorage.getItem('token'),
8+
user: null,
79
};
810

911
export const loginReducer = (state = initialState, action) =>
1012
produce(state, draft => {
1113
switch (action.type) {
14+
case types.USER_LOADED:
15+
draft.isAuthenticated = true;
16+
draft.loading = false;
17+
draft.user = action.payload;
18+
break;
1219
case types.LOGIN_REQUESTED:
1320
draft.loading = true;
21+
draft.isAuthenticated = false;
22+
draft.user = action.payload;
1423
break;
1524
case types.LOGIN_SUCCESS:
1625
draft.loading = false;
1726
draft.isAuthenticated = true;
1827
break;
1928
case types.LOGIN_FAILED:
29+
case types.AUTH_ERROR:
30+
case types.LOGOUT:
31+
localStorage.removeItem('token');
2032
draft.loading = false;
33+
draft.isAuthenticated = false;
34+
draft.token = null;
2135
break;
36+
2237
default:
2338
return state;
2439
}

src/components/Auth/Login/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react';
2-
import { Form, Input, Button, Checkbox } from 'antd';
2+
import { Form, Input, Button, Checkbox, message } from 'antd';
33
import { UserOutlined, LockOutlined } from '@ant-design/icons';
44
import { connect, ConnectedProps } from 'react-redux';
55
import { useHistory, Link } from 'react-router-dom';
@@ -28,11 +28,11 @@ const _Login = (props: Props) => {
2828
const payload = { username, password };
2929
try {
3030
const res = await login(payload);
31+
message.success('Login successfully');
3132
history.push(PATH.HOME);
32-
console.log('Success', res);
3333
} catch (error) {
34+
message.error(error.message);
3435
setError(error.payload.message);
35-
console.log('Error', error.message);
3636
}
3737
}
3838
};
@@ -86,6 +86,7 @@ const _Login = (props: Props) => {
8686
type="primary"
8787
htmlType="submit"
8888
className="login-form-button"
89+
loading={loading}
8990
>
9091
Log in
9192
</Button>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as types from './Register.constants';
2+
3+
export const registerSuccess = payload => ({
4+
type: types.REGISTER_SUCCESS,
5+
payload,
6+
});
7+
8+
export const registerFailed = payload => ({
9+
type: types.REGISTER_FAILED,
10+
payload,
11+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const REGISTER_SUCCESS = 'views/auth/REGISTER_SUCCESS';
2+
export const REGISTER_FAILED = 'views/auth/REGISTER_FAIL';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as types from './Register.constants';
2+
import produce from 'immer';
3+
4+
const initialState = {
5+
loading: true,
6+
isAuthenticated: false,
7+
token: localStorage.getItem('token'),
8+
};
9+
10+
export const loginReducer = (state = initialState, action) =>
11+
produce(state, draft => {
12+
switch (action.type) {
13+
case types.REGISTER_SUCCESS:
14+
draft.loading = false;
15+
draft.isAuthenticated = true;
16+
break;
17+
case types.REGISTER_FAILED:
18+
localStorage.removeItem('token');
19+
draft.token = null;
20+
draft.loading = false;
21+
draft.isAuthenticated = false;
22+
break;
23+
default:
24+
return state;
25+
}
26+
});

src/components/Auth/Register/Register.thunks.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)