Skip to content

Commit 809a4c2

Browse files
committed
migration
1 parent c014ebb commit 809a4c2

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

front/src/mock/userInfo.json

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

front/src/mock/userInfo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const userMock = Object.freeze({
2+
token:
3+
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkZW1vIiwiaWF0IjoxNTAyMzA3MzU0LCJleHAiOjE3MjMyMzIxNTQsImF1ZCI6ImRlbW8tZGVtbyIsInN1YiI6ImRlbW8iLCJHaXZlbk5hbWUiOiJKb2huIiwiU3VybmFtZSI6IkRvZSIsIkVtYWlsIjoiam9obi5kb2VAZXhhbXBsZS5jb20iLCJSb2xlIjpbIlN1cGVyIGNvb2wgZGV2IiwibWFnaWMgbWFrZXIiXX0.6FjgLCypaqmRp4tDjg_idVKIzQw16e-z_rjA3R94IqQ',
4+
user: {
5+
id: 111,
6+
login: 'john.doe@fake.mail',
7+
firstname: 'John',
8+
lastname: 'Doe',
9+
isAdmin: true,
10+
},
11+
});
12+
13+
export default userMock;

front/src/pages/login/Login.tsx

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Row from 'reactstrap/lib/Row';
66
import Col from 'reactstrap/lib/Col';
77
import { appConfig } from '../../config/appConfig';
88
import { getLocationOrigin } from '../../services/API/fetchTools';
9-
import userInfoMock from '../../mock/userInfo.json';
9+
import userInfoMock from '../../mock/userInfo.js';
1010
import { AuthContextProps } from '../../contexts/auth/consumerHOC';
1111
import FadeInEntrance from '../../components/fadeInEntrance';
1212

@@ -52,42 +52,39 @@ function Login({
5252
[],
5353
);
5454

55-
const handlesOnLogin = async (event: React.MouseEvent<HTMLButtonElement>) => {
56-
event && event.preventDefault();
55+
const handlesOnLogin = useCallback(
56+
async (event: React.MouseEvent<HTMLButtonElement>) => {
57+
event && event.preventDefault();
58+
59+
try {
60+
setIsLogging(true);
61+
const response = await logUser(email, password);
62+
const {
63+
data: { token, user },
64+
} = response;
65+
66+
setToken(token);
67+
setUserInfo(user);
5768

58-
const { history, setToken, setUserInfo } = this.props;
59-
const { email, password } = this.state;
60-
61-
const userLogin = {
62-
login: email,
63-
password: password,
64-
};
65-
66-
try {
67-
this.setState({ isLogging: true });
68-
const response = await logUser(userLogin);
69-
const {
70-
data: { token, user },
71-
} = response;
72-
73-
setToken(token);
74-
setUserInfo(user);
75-
76-
this.setState({ isLogging: false });
77-
78-
history.push({ pathname: '/' }); // back to Home
79-
} catch (error) {
80-
this.setState({ isLogging: false });
81-
/* eslint-disable no-console */
82-
console.log('login went wrong..., error: ', error);
83-
/* eslint-enable no-console */
84-
}
85-
};
69+
history.push({ pathname: '/' }); // back to Home
70+
} catch (error) {
71+
/* eslint-disable no-console */
72+
console.log('login went wrong..., error: ', error);
73+
/* eslint-enable no-console */
74+
} finally {
75+
setIsLogging(false);
76+
}
77+
},
78+
[history, setToken, setUserInfo, email, password],
79+
);
8680
// #endregion
8781

8882
// #region log user request
8983
const logUser = useCallback(
90-
async (login: string = '', password: string = '') => {
84+
async (
85+
login: string = '',
86+
password: string = '',
87+
): Promise<{ data: { user: Partial<User>; token: string } }> => {
9188
const __SOME_LOGIN_API__ = 'login';
9289
const url = `${getLocationOrigin()}/${__SOME_LOGIN_API__}`;
9390
const method = 'post';
@@ -107,7 +104,7 @@ function Login({
107104
}
108105

109106
try {
110-
const response = await axios.request({
107+
const response = await axios.request<{ token: string; user: any }>({
111108
method,
112109
url,
113110
withCredentials: true,
@@ -120,9 +117,9 @@ function Login({
120117
...options,
121118
});
122119

123-
return Promise.resolve(response);
120+
return response;
124121
} catch (error) {
125-
return Promise.reject(error);
122+
throw error;
126123
}
127124
},
128125
[],

front/src/types/user/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
declare type User = {
2-
id: string;
2+
id: string | number;
33
login: string;
44
firstname: string;
55
lastname: string;
66
token: string;
7-
isAuthenticated: boolean;
7+
isAuthenticated?: boolean;
88
};

0 commit comments

Comments
 (0)