-
Notifications
You must be signed in to change notification settings - Fork 4
/
ResetPassword.vue
243 lines (224 loc) · 5.25 KB
/
ResetPassword.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<template>
<main v-if="success" class="pdap-flex-container">
<h1>Success</h1>
<p data-test="success-subheading">
{{
token
? 'Your password has been successfully updated'
: 'We sent you an email with a link to reset your password. Please follow the link in the email to proceed'
}}
</p>
<RouterLink v-if="token" to="/login">Click here to log in </RouterLink>
</main>
<main
v-else-if="!success && token"
class="pdap-flex-container mx-auto max-w-2xl"
>
<h1>Change your password</h1>
<p v-if="!hasValidatedToken" class="flex flex-col items-start sm:gap-4">
Loading...
</p>
<p
v-else-if="hasValidatedToken && isExpiredToken"
data-test="token-expired"
class="flex flex-col items-start sm:gap-4"
>
Sorry, that token has expired.
<RouterLink
data-test="re-request-link"
to="/reset-password"
@click="
isExpiredToken = false;
error = undefined;
token = undefined;
"
>
Click here to request another
</RouterLink>
</p>
<Form
v-else
id="reset-password"
data-test="reset-password-form"
class="flex flex-col"
name="reset-password"
:error="error"
:schema="FORM_SCHEMA_CHANGE_PASSWORD"
@change="handleChangeOnError"
@submit="onSubmitChangePassword"
>
<ul class="text-med mb-8">
Passwords must be at least 8 characters and include:
<li>1 uppercase letter</li>
<li>1 lowercase letter</li>
<li>1 number</li>
<li>1 special character</li>
</ul>
<Button class="max-w-full" type="submit">
{{ loading ? 'Loading...' : 'Change password' }}
</Button>
</Form>
</main>
<main v-else class="pdap-flex-container mx-auto max-w-2xl">
<h1>Request a link to reset your password</h1>
<Form
id="reset-password"
data-test="reset-password-form"
class="flex flex-col"
name="reset-password"
:error="error"
:schema="FORM_SCHEMA_REQUEST_PASSWORD"
@submit="onSubmitRequestReset"
>
<Button class="max-w-full" type="submit">
{{ loading ? 'Loading...' : 'Request password reset' }}
</Button>
</Form>
</main>
</template>
<script setup>
import { Button, Form } from 'pdap-design-system';
import { useUserStore } from '../stores/user';
import { onMounted, ref, watchEffect } from 'vue';
import { RouterLink, useRoute } from 'vue-router';
// Constants
const FORM_SCHEMA_CHANGE_PASSWORD = [
{
'data-test': 'password',
id: 'password',
name: 'password',
label: 'Password',
type: 'password',
placeholder: 'Your updated password',
value: '',
validators: {
password: {
message: 'Please provide your password',
value: true,
},
},
},
{
'data-test': 'confirm-password',
id: 'confirmPassword',
name: 'confirmPassword',
label: 'Confirm Password',
type: 'password',
placeholder: 'Confirm your updated password',
value: '',
validators: {
password: {
message: 'Please confirm your password',
value: true,
},
},
},
];
const FORM_SCHEMA_REQUEST_PASSWORD = [
{
'data-test': 'email',
id: 'email',
name: 'email',
label: 'Email',
type: 'text',
placeholder: 'Your email address',
value: '',
validators: {
email: {
message: 'Please provide your email address',
value: true,
},
},
},
];
const {
params: { token },
} = useRoute();
// Stores
const user = useUserStore();
// Reactive vars
const error = ref(undefined);
const isExpiredToken = ref(false);
const hasValidatedToken = ref(false);
const loading = ref(false);
const success = ref(false);
// Effects
// Clear error on success
watchEffect(() => {
if (success.value) error.value = undefined;
});
// Functions
// Lifecycle methods
onMounted(validateToken);
// Handlers
async function validateToken() {
if (!token) return;
try {
const response = await user.validateResetPasswordToken(token);
if (300 < response.status >= 200) {
isExpiredToken.value = false;
}
} catch (error) {
isExpiredToken.value = true;
} finally {
hasValidatedToken.value = true;
}
}
/**
* Handles clearing pw-match form errors on change if they exist
*/
function handleChangeOnError(formValues) {
if (error.value && formValues.password !== formValues.confirmPassword) {
handlePasswordValidation(formValues);
}
}
/**
* Validates that passwords match
* @returns {boolean} `false` if passwords do not match, `true` if they do
*/
function handlePasswordValidation(formValues) {
if (formValues.password !== formValues.confirmPassword) {
if (!error.value) {
error.value = 'Passwords do not match, please try again.';
}
return false;
} else {
error.value = undefined;
return true;
}
}
/**
* Updates user's password
*/
async function onSubmitChangePassword(formValues) {
if (!handlePasswordValidation(formValues)) return;
try {
loading.value = true;
const { password } = formValues;
await user.resetPassword(password, token);
success.value = true;
} catch (err) {
if (err.message === 'The submitted token is invalid') {
isExpiredToken.value = true;
}
error.value = err.message;
} finally {
loading.value = false;
}
}
/**
* Updates user's password
*/
async function onSubmitRequestReset(formValues) {
try {
loading.value = true;
const { email } = formValues;
await user.requestPasswordReset(email);
success.value = true;
} catch (err) {
error.value = err.message;
} finally {
loading.value = false;
}
}
</script>