Skip to content

Commit cb11c65

Browse files
committed
add forgot password in login
1 parent 0cf56d3 commit cb11c65

File tree

13 files changed

+326
-85
lines changed

13 files changed

+326
-85
lines changed

__tests__/app/containers/Authentication/__snapshots__/Login.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`Login Container Should render 1`] = `
44
<Login
55
errorMessage=""
6+
forgotPassword={[Function]}
67
handleSelectPanel={[Function]}
78
isLoggedIn={false}
89
isLoginLoading={false}

src/app/actions/User.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export namespace UserActions {
1010
EDIT_USER_PROFILE = 'EDIT_USER_PROFILE',
1111
EDIT_USER_PASSWORD = 'EDIT_USER_PASSWORD',
1212
CHANGE_USER_PASSWORD = 'CHANGE_USER_PASSWORD',
13+
FORGOT_PASSWORD = 'FORGOT_PASSWORD',
1314
UPDATE_ERROR_MESSAGE = 'UPDATE_ERROR_MESSAGE',
1415
UPDATE_USER_DETAILS = 'UPDATE_USER_DETAILS',
1516
CHECK_EMAIL_EXISTS = 'CHECK_EMAIL_EXISTS',
@@ -64,10 +65,15 @@ export namespace UserActions {
6465
export const checkUsernameExists = (username: string) =>
6566
action(Type.CHECK_USERNAME_EXISTS, { username });
6667

67-
export const changeUserPassword = (newPassword: string, passwordResetToken: string) =>
68+
export const changeUserPassword = (
69+
newPassword: string,
70+
passwordResetToken: string,
71+
userId: number,
72+
) =>
6873
action(Type.CHANGE_USER_PASSWORD, {
6974
newPassword,
7075
passwordResetToken,
76+
userId,
7177
});
7278

7379
export const toggleUserProfileModal = (isUserProfileModalOpen: boolean) =>
@@ -82,4 +88,6 @@ export namespace UserActions {
8288

8389
export const setIsLoginLoading = (isLoginLoading: boolean) =>
8490
action(Type.SET_IS_LOGIN_LOADING, { isLoginLoading });
91+
92+
export const forgotPassword = (email: string) => action(Type.FORGOT_PASSWORD, { email });
8593
}

src/app/apiFetch/User.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,32 @@ export const changeUserPassword = (body: UserInterfaces.ChangeUserPassword) => {
116116
return fetch(`${API_BASE_URL}user/password`, {
117117
body: JSON.stringify(body),
118118
headers: {
119-
Accept: 'application/json',
120119
'Content-Type': 'application/json',
121120
},
122121
method: 'POST',
123122
})
124123
.then((response) => {
125-
console.log('api fetch response');
126-
console.log(response);
127-
return response.json();
124+
return headResponseWrapper(response, HeadReqType.USERNAME);
125+
})
126+
.then((data) => {
127+
return data;
128+
})
129+
.catch((error) => {
130+
console.error(error);
131+
});
132+
};
133+
export const userForgotPassword = (email: string) => {
134+
return fetch(`${API_BASE_URL}user/forgot-password`, {
135+
body: email,
136+
headers: {
137+
'Content-Type': 'application/json',
138+
},
139+
method: 'POST',
140+
})
141+
.then((response) => {
142+
return response.text();
128143
})
129144
.then((data) => {
130-
console.log('api fetch data');
131-
console.log(data);
132145
return data;
133146
})
134147
.catch((error) => {

src/app/components/Authentication/ChangePassword.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as React from 'react';
77
export class ChangePassword extends React.Component<changePasswordProps, ChangePasswordState> {
88
private credentialsFormRef = React.createRef<HTMLFormElement>();
99
private passwordResetToken = '';
10+
private userId = 0;
1011
constructor(props: changePasswordProps) {
1112
super(props);
1213
this.state = {
@@ -19,7 +20,9 @@ export class ChangePassword extends React.Component<changePasswordProps, ChangeP
1920
public componentDidMount() {
2021
// get string from url
2122
const search = this.props.location.search;
22-
this.passwordResetToken = search.split('=')[1];
23+
const urlParams = search.split('&');
24+
this.passwordResetToken = urlParams[0].split('=')[1];
25+
this.userId = parseInt(urlParams[1].split('=')[1], 0);
2326
}
2427

2528
public render() {
@@ -83,7 +86,7 @@ export class ChangePassword extends React.Component<changePasswordProps, ChangeP
8386

8487
<div
8588
className={
86-
this.state.passwordError !== ''
89+
this.state.passwordError !== '' || this.props.errorMessage !== ''
8790
? classnames('form-row', authStyles['register-error-active'])
8891
: classnames('form-row', authStyles['register-error-inactive'])
8992
}
@@ -130,7 +133,7 @@ export class ChangePassword extends React.Component<changePasswordProps, ChangeP
130133
...this.state,
131134
passwordError: '',
132135
});
133-
this.props.changePassword(this.state.password, this.passwordResetToken);
136+
this.props.changePassword(this.state.password, this.passwordResetToken, this.userId);
134137
}
135138
}
136139
} else {

0 commit comments

Comments
 (0)