Skip to content

Commit b14999b

Browse files
Merge pull request #45 from Typext/feature/implementing-recovery-login-screen
feat(recovery login screen): finished recovery and reset login component
2 parents 988204b + 66a668e commit b14999b

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

src/DTOs/Auth.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { StringifyOptions } from 'node:querystring';
21
import { ReactNode } from 'react';
32

43
export interface AuthState {
@@ -43,8 +42,9 @@ export interface RecoveryCredentials {
4342
}
4443

4544
export interface ResetCredentials {
45+
email: string;
4646
password: string;
47-
confirmPassword: string;
47+
password_confirmation: string;
4848
}
4949

5050
interface InvitationData {
@@ -65,6 +65,7 @@ interface RecoveryPassowordData {
6565
interface ResetPasswordData {
6666
error: String;
6767
loader: boolean;
68+
success: boolean;
6869
}
6970

7071
export interface AuthContextData {

src/contexts/auth.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const AuthProvider = ({ children }: AuthProviderProps) => {
1919

2020
const [resetError, setResetError] = useState<string>('');
2121
const [resetLoader, setResetLoader] = useState<boolean>(false);
22+
const [resetSuccess, setResetSuccess] = useState<boolean>(false);
2223

2324
const [data, setData] = useState<AuthState>(() => {
2425
const token = localStorage.getItem('@Typext:token');
@@ -130,23 +131,28 @@ const AuthProvider = ({ children }: AuthProviderProps) => {
130131
setRecoveryLoader(false);
131132
}, []);
132133

133-
const resetPassword = useCallback(async ({ password, confirmPassword }) => {
134-
setResetLoader(true);
134+
const resetPassword = useCallback(
135+
async ({ email, password, password_confirmation }) => {
136+
setResetLoader(true);
135137

136-
try {
137-
setRecoveryError('');
138+
try {
139+
setResetError('');
138140

139-
await api.post('/password/reset', { password, confirmPassword });
140-
} catch (err) {
141-
const errorStatus = err.response?.status;
141+
await api.post('/password/reset', {
142+
email,
143+
password,
144+
password_confirmation,
145+
});
142146

143-
if (errorStatus === 401) {
144-
setResetError(err.response?.data.message);
147+
setResetLoader(false);
148+
setResetSuccess(true);
149+
} catch (err) {
150+
setResetLoader(false);
151+
setResetError(err.response?.data.validation.body.message);
145152
}
146-
147-
setResetLoader(false);
148-
}
149-
}, []);
153+
},
154+
[],
155+
);
150156

151157
return (
152158
<AuthContext.Provider
@@ -168,6 +174,7 @@ const AuthProvider = ({ children }: AuthProviderProps) => {
168174
reset: {
169175
error: resetError,
170176
loader: resetLoader,
177+
success: resetSuccess,
171178
},
172179
signIn,
173180
signUp,
@@ -193,6 +200,3 @@ function useAuth(): AuthContextData {
193200
}
194201

195202
export { AuthProvider, useAuth };
196-
function async(arg0: { email: any }): any {
197-
throw new Error('Function not implemented.');
198-
}

src/pages/ResetPassword/components/ResetPasswordModal/index.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
2+
import { useHistory } from 'react-router-dom';
23

34
import { useAuth } from 'contexts/auth';
45

@@ -17,10 +18,20 @@ interface IResetPasswordModalProps {
1718
const ResetPasswordModal: React.FC<IResetPasswordModalProps> = ({
1819
onClose,
1920
}: IResetPasswordModalProps) => {
21+
const history = useHistory();
22+
2023
const {
21-
reset: { error, loader },
24+
reset: { error, loader, success },
2225
} = useAuth();
2326

27+
useEffect(() => {
28+
if (!loader && !error && success) {
29+
setTimeout(() => {
30+
history.push('/login');
31+
}, 1500);
32+
}
33+
}, [success, error, loader, history]);
34+
2435
return (
2536
<StyledResetPasswordModal>
2637
<DefaultModal onClose={onClose}>
@@ -35,8 +46,8 @@ const ResetPasswordModal: React.FC<IResetPasswordModalProps> = ({
3546
) : (
3647
<StyledResetPasswordContent>
3748
<img src={WarnIcon} alt="" />
38-
<h1>SENHA INVÁLIDA!</h1>
39-
<h3>VERIFIQUE SE A DIGITOU CORRETAMENTE</h3>
49+
<h1>OCORREU UM PROBLEMA</h1>
50+
<h3>{error.toUpperCase()}</h3>
4051
</StyledResetPasswordContent>
4152
)}
4253
</DefaultModal>

src/pages/ResetPassword/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ import Input from 'components/Input/Input';
77
import Button from 'components/Button/Button';
88
import Logo from 'assets/logo.svg';
99

10+
import { useParams } from 'react-router-dom';
1011
import ResetPasswordModal from './components/ResetPasswordModal';
1112
import StyledNewPassword from './styles';
1213

14+
export interface ParamsData {
15+
email: string;
16+
}
17+
1318
const NewPassword = () => {
19+
const params: ParamsData = useParams();
20+
1421
const { resetPassword } = useAuth();
1522

1623
const [showResetModal, setShowResetModal] = useState<boolean>(false);
@@ -23,17 +30,20 @@ const NewPassword = () => {
2330
}, []);
2431

2532
const handleResetPassword = useCallback(() => {
33+
const userEmail = params.email;
34+
2635
if (!userPassword || !userConfirmPassword) {
2736
message.error('Todos os campos devem estar preenchidos');
2837
return;
2938
}
3039

3140
setShowResetModal(true);
3241
resetPassword({
42+
email: userEmail,
3343
password: userPassword,
34-
confirmPassword: userConfirmPassword,
44+
password_confirmation: userConfirmPassword,
3545
});
36-
}, [resetPassword, userPassword, userConfirmPassword]);
46+
}, [resetPassword, params, userPassword, userConfirmPassword]);
3747

3848
return (
3949
<>

src/routes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function Routes() {
3131
<Route path="/user/update" isPrivate component={UserUpdate} />
3232
<Route path="/home" exact isPrivate component={Home} />
3333
<Route path="/minute" isPrivate component={Minute} />
34-
<Route path="/reset" component={ResetPassword} />
34+
<Route path="/reset/:email" component={ResetPassword} />
3535
<Route path="/recovery" component={Recovery} />
3636
<Route path="/login" component={Login} />
3737
<Route path="/invite/:email" component={Register} />

0 commit comments

Comments
 (0)