-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathuseSendPasswordResetEmail.ts
More file actions
37 lines (33 loc) · 1005 Bytes
/
useSendPasswordResetEmail.ts
File metadata and controls
37 lines (33 loc) · 1005 Bytes
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
import {
ActionCodeSettings,
Auth,
AuthError,
sendPasswordResetEmail as fbSendPasswordResetEmail,
} from 'firebase/auth';
import { useCallback, useState } from 'react';
export type SendPasswordResetEmailHook = [
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
export default (auth: Auth): SendPasswordResetEmailHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const sendPasswordResetEmail = useCallback(
async (email: string, actionCodeSettings?: ActionCodeSettings) => {
setLoading(true);
setError(undefined);
try {
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
return true;
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);
return [sendPasswordResetEmail, loading, error];
};