G

Weird ResetPasswrd

unlisted
Guest Nov 07, 2024 Never 25
Clone
TypeScript Loginpage
TypeScript auth
TypeScript appwrite
TypeScript Loginpage 32 lines (31 loc) | 1.04 KB
1
const forgotPasswordBtn = async () => {
2
const result = await inputConfirm({
3
title: 'Forgot Password',
4
text: 'Please enter your email address to reset your password',
5
inputLabel: 'Email',
6
inputPlaceholder: '[email protected]',
7
icon: 'question',
8
validationRule: (value: string) => {
9
if (!validationRules.isValidEmail(value)) {
10
return 'Please enter a valid email address';
11
}
12
return true;
13
},
14
});
15
16
if (result.isConfirmed) {
17
try {
18
const response = await resetPassword(result.value);
19
if (response) {
20
success('Password reset email sent!');
21
} else {
22
error('Failed to send password reset email. Please try again.');
23
}
24
} catch (err) {
25
if (err instanceof AppwriteException) {
26
error(err.message);
27
} else {
28
error('An unknown error occurred.');
29
}
30
}
31
}
32
};
1
export const resetPassword = async (email: string): Promise<boolean> => {
2
if (!email) return false;
3
const userResponse = await $listUsers([Query.equal('email', email)]);
4
if (!userResponse || userResponse.status !== 200) return false;
5
if (userResponse.data.total === 0) return false;
6
const user = userResponse.data.users[0];
7
if (!user) {
8
console.log('no user found');
9
return false;
10
};
11
console.log('Creating recovery');
12
try {
13
const response = await $account.createRecovery(
14
email,
15
`${SITE_URL}/verification/reset/password`,
16
);
17
console.log('response', response);
18
return response !== null && response !== undefined;
19
} catch (error) {
20
console.error('Error creating recovery', error);
21
return false;
22
}
23
};
1
export const $listUsers = async (
2
queries?: string[],
3
search?: string,
4
customHeaders?: HeadersInit,
5
) => {
6
let curUserId: string | undefined;
7
try {
8
const curUser = await $account.get();
9
curUserId = curUser?.$id;
10
} catch (error) {
11
curUserId = undefined;
12
}
13
try {
14
const response = await $functions.createExecution(
15
USER_MGMT_ID,
16
JSON.stringify(
17
UserRequestSchema.parse({
18
type: 'list',
19
dbId: APPWRITE_DB,
20
curUserId: curUserId,
21
queries: queries,
22
search: search,
23
}),
24
),
25
ExecutionMode.SYNC,
26
'/',
27
ExecutionMethod.POST,
28
{
29
'Content-Type': 'application/json',
30
...(customHeaders ?? {}),
31
},
32
);
33
if (response.status === 'completed') {
34
return JSON.parse(response.responseBody).data;
35
}
36
return 'Failed to list users';
37
} catch (error) {
38
console.error(error);
39
return null;
40
}
41
};