forked from CSFrequency/react-firebase-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useCreateUserWithEmailAndPassword.ts
41 lines (38 loc) · 1.17 KB
/
useCreateUserWithEmailAndPassword.ts
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
import { useState, useMemo } from 'react';
import firebase from 'firebase/app';
import { CreateUserOptions, EmailAndPasswordActionHook } from './types';
export default (
auth: firebase.auth.Auth,
options?: CreateUserOptions
): EmailAndPasswordActionHook => {
const [error, setError] = useState<firebase.FirebaseError>();
const [
registeredUser,
setRegisteredUser,
] = useState<firebase.auth.UserCredential>();
const [loading, setLoading] = useState<boolean>(false);
const createUserWithEmailAndPassword = async (
email: string,
password: string
) => {
setLoading(true);
try {
const user = await auth.createUserWithEmailAndPassword(email, password);
if (options && options.sendEmailVerification && user.user) {
await user.user.sendEmailVerification(options.emailVerificationOptions);
}
setRegisteredUser(user);
setLoading(false);
} catch (error) {
setError(error);
setLoading(false);
}
};
const resArray: EmailAndPasswordActionHook = [
createUserWithEmailAndPassword,
registeredUser,
loading,
error,
];
return useMemo<EmailAndPasswordActionHook>(() => resArray, resArray);
};