Skip to content

Commit aac722e

Browse files
committed
Update Auth
1 parent d3fc2d8 commit aac722e

File tree

18 files changed

+117
-63
lines changed

18 files changed

+117
-63
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"@ant-design/icons": "^4.3.0",
7+
"@types/axios": "^0.14.0",
78
"antd": "^4.8.6",
89
"concurrently": "^5.3.0",
910
"immer": "^8.0.0",
@@ -16,7 +17,8 @@
1617
"redux": "^4.0.5",
1718
"redux-thunk": "^2.3.0",
1819
"styled-components": "^5.2.1",
19-
"typescript": "~4.0.5"
20+
"typescript": "~4.0.5",
21+
"uuid": "^8.3.1"
2022
},
2123
"devDependencies": {
2224
"@testing-library/jest-dom": "^5.11.5",

server/db/db.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2-
"users": [],
2+
"users": [{
3+
"id": "dfdmsqjfmsd",
4+
"username": "usertest",
5+
"email": "emailusertest@gmail.com",
6+
"password": "123"
7+
}],
38
"products": []
49
}

src/@types/user.d.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@ interface ReqLogin {
22
username: string;
33
password: string;
44
}
5+
interface ReqRegister {
6+
username: string;
7+
email: string;
8+
password: string;
9+
}
510
interface ResLoginApi extends Res {
611
data: {
7-
access_token: string;
12+
id: string;
13+
username: string;
14+
email: string;
15+
password: string;
816
};
917
}
18+
19+
interface IUser {
20+
id: string;
21+
username: string;
22+
email?: string;
23+
password: string;
24+
}

src/App/App.reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as types from './App.constants';
2-
import { LOGIN_SUCCESS } from 'src/components/Auth/Login/Login.constants';
2+
import { LOGIN_SUCCESS } from 'src/components/Auth/Login/Auth.constants';
33
import produce from 'immer';
44

55
const initialState = {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as types from './Login.constants';
1+
import * as types from './Auth.constants';
22

33
export const loginRequested = () => ({
44
type: types.LOGIN_REQUESTED,
@@ -26,3 +26,13 @@ export const authError = () => ({
2626
export const logout = () => ({
2727
type: types.LOGOUT,
2828
});
29+
30+
export const registerSuccess = payload => ({
31+
type: types.REGISTER_SUCCESS,
32+
payload,
33+
});
34+
35+
export const registerFailed = payload => ({
36+
type: types.REGISTER_FAILED,
37+
payload,
38+
});

src/components/Auth/Login/Login.constants.ts renamed to src/components/Auth/Auth.constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export const LOGIN_REQUESTED = 'views/auth/LOGIN_REQUESTED';
44
export const USER_LOADED = 'views/auth/USER_LOADED';
55
export const AUTH_ERROR = 'views/auth/AUTH_ERROR';
66
export const LOGOUT = 'views/auth/LOGOUT';
7+
export const REGISTER_SUCCESS = 'views/auth/REGISTER_SUCCESS';
8+
export const REGISTER_FAILED = 'views/auth/REGISTER_FAIL';

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

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

44
const initialState = {
@@ -22,12 +22,14 @@ export const loginReducer = (state = initialState, action) =>
2222
draft.user = action.payload;
2323
break;
2424
case types.LOGIN_SUCCESS:
25+
case types.REGISTER_SUCCESS:
2526
draft.loading = false;
2627
draft.isAuthenticated = true;
2728
break;
2829
case types.LOGIN_FAILED:
2930
case types.AUTH_ERROR:
3031
case types.LOGOUT:
32+
case types.REGISTER_FAILED:
3133
localStorage.removeItem('token');
3234
draft.loading = false;
3335
draft.isAuthenticated = false;

src/components/Auth/Auth.thunks.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import axios from 'axios';
2+
import { URL } from 'src/constants/urls';
3+
import * as actions from './Auth.actions';
4+
import { v4 as uuid } from 'uuid';
5+
6+
export const loadUser = () => async dispatch => {
7+
const tokenId = localStorage.getItem('token');
8+
if (!tokenId) {
9+
return dispatch(actions.authError);
10+
}
11+
try {
12+
const res = await axios.get(`${URL.baseAPIUrl}/api/users/${tokenId}`);
13+
if (res) {
14+
return dispatch(actions.userLoaded(res));
15+
}
16+
dispatch(actions.authError);
17+
} catch (error) {
18+
return dispatch(actions.authError);
19+
}
20+
};
21+
22+
export const login = (payload: ReqLogin) => async dispatch => {
23+
const { username, password } = payload;
24+
try {
25+
let allUsers: IUser[] = [];
26+
allUsers = await axios.get(`${URL.baseAPIUrl}/api/users`);
27+
let user = allUsers.filter(x => x.username === username)[0];
28+
if (user && user.password === password) {
29+
localStorage.setItem('token', user.id);
30+
dispatch(actions.loginSuccess(user));
31+
loadUser();
32+
}
33+
dispatch(actions.loginFailed);
34+
} catch (error) {
35+
return dispatch(actions.loginFailed);
36+
}
37+
};
38+
39+
export const register = (payload: ReqRegister) => async dispatch => {
40+
try {
41+
const id = uuid();
42+
const newUser = { id, ...payload };
43+
await axios.post(`${URL.baseAPIUrl}/api/users`, newUser);
44+
localStorage.setItem('token', id);
45+
dispatch(actions.registerSuccess(newUser));
46+
loadUser();
47+
} catch (error) {
48+
return dispatch(actions.registerFailed);
49+
}
50+
};
51+
export const logout = () => async dispatch => {
52+
return dispatch(actions.logout);
53+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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';
6-
import { login } from './Login.thunks';
6+
import { login } from './Auth.thunks';
77
import { PATH } from 'src/constants/paths';
88

99
const mapStateToProps = state => ({

src/components/Auth/Login/Login.thunks.ts

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

0 commit comments

Comments
 (0)