forked from CSFrequency/react-firebase-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSendEmailVerification.ts
More file actions
37 lines (33 loc) · 923 Bytes
/
useSendEmailVerification.ts
File metadata and controls
37 lines (33 loc) · 923 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 {
Auth,
AuthError,
sendEmailVerification as fbSendEmailVerification,
} from 'firebase/auth';
import { useCallback, useState } from 'react';
export type SendEmailVerificationHook = [
() => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
export default (auth: Auth): SendEmailVerificationHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const sendEmailVerification = useCallback(async () => {
setLoading(true);
setError(undefined);
try {
if (auth.currentUser) {
await fbSendEmailVerification(auth.currentUser);
return true;
} else {
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
}, [auth]);
return [sendEmailVerification, loading, error];
};