Skip to content

Commit adebfb9

Browse files
committed
MInor fix auth
1 parent fa3ec9b commit adebfb9

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/@types/user.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
interface ReqLogin {
22
username: string;
33
password: string;
4-
}
5-
interface ReqRegister {
6-
username: string;
7-
email: string;
8-
password: string;
4+
email?: string;
95
}
106
interface ResLoginApi extends Res {
117
data: {

src/components/Auth/Auth.reducers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import * as types from './Auth.constants';
22
import produce from 'immer';
33

4+
let userType: IUser = {
5+
id: '',
6+
username: '',
7+
email: undefined,
8+
password: '',
9+
};
410
const initialState = {
511
loading: true,
612
isAuthenticated: false,
713
token: localStorage.getItem('token'),
8-
user: null,
14+
user: userType,
915
};
1016

1117
export const loginReducer = (state = initialState, action) =>
@@ -16,13 +22,9 @@ export const loginReducer = (state = initialState, action) =>
1622
draft.loading = false;
1723
draft.user = action.payload;
1824
break;
19-
case types.LOGIN_REQUESTED:
20-
draft.loading = true;
21-
draft.isAuthenticated = false;
22-
draft.user = action.payload;
23-
break;
2425
case types.LOGIN_SUCCESS:
2526
case types.REGISTER_SUCCESS:
27+
localStorage.setItem('token', draft.user.id);
2628
draft.loading = false;
2729
draft.isAuthenticated = true;
2830
break;

src/components/Auth/Auth.thunks.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const login = (payload: ReqLogin) => async dispatch => {
2626
allUsers = await axios.get(`${URL.baseAPIUrl}/api/users`);
2727
let user = allUsers.filter(x => x.username === username)[0];
2828
if (user && user.password === password) {
29-
localStorage.setItem('token', user.id);
3029
dispatch(actions.loginSuccess(user));
3130
loadUser();
3231
}
@@ -36,12 +35,11 @@ export const login = (payload: ReqLogin) => async dispatch => {
3635
}
3736
};
3837

39-
export const register = (payload: ReqRegister) => async dispatch => {
38+
export const register = (payload: ReqLogin) => async dispatch => {
4039
try {
4140
const id = uuid();
4241
const newUser = { id, ...payload };
4342
await axios.post(`${URL.baseAPIUrl}/api/users`, newUser);
44-
localStorage.setItem('token', id);
4543
dispatch(actions.registerSuccess(newUser));
4644
console.log('Resgister success', newUser);
4745
const all = await axios.get(`${URL.baseAPIUrl}/api/users`);

0 commit comments

Comments
 (0)