-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathuseUpdateUser.ts
151 lines (136 loc) · 3.77 KB
/
useUpdateUser.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
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
import {
ActionCodeSettings,
Auth,
AuthError,
updateEmail as fbUpdateEmail,
updatePassword as fbUpdatePassword,
updateProfile as fbUpdateProfile,
verifyBeforeUpdateEmail as fbVerifyBeforeUpdateEmail,
} from 'firebase/auth';
import { useCallback, useState } from 'react';
type Profile = {
displayName?: string | null;
photoURL?: string | null;
};
export type UpdateUserHook<M> = [M, boolean, AuthError | Error | undefined];
export type UpdateEmailHook = UpdateUserHook<
(email: string) => Promise<boolean>
>;
export type UpdatePasswordHook = UpdateUserHook<
(password: string) => Promise<boolean>
>;
export type UpdateProfileHook = UpdateUserHook<
(profile: Profile) => Promise<boolean>
>;
export type VerifyBeforeUpdateEmailHook = UpdateUserHook<
(
email: string,
actionCodeSettings: ActionCodeSettings | null
) => Promise<boolean>
>;
export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const updateEmail = useCallback(
async (email: string) => {
setLoading(true);
setError(undefined);
try {
if (auth.currentUser) {
await fbUpdateEmail(auth.currentUser, email);
return true;
} else {
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);
return [updateEmail, loading, error];
};
export const useUpdatePassword = (auth: Auth): UpdatePasswordHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const updatePassword = useCallback(
async (password: string) => {
setLoading(true);
setError(undefined);
try {
if (auth.currentUser) {
await fbUpdatePassword(auth.currentUser, password);
return true;
} else {
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);
return [updatePassword, loading, error];
};
export const useUpdateProfile = (auth: Auth): UpdateProfileHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const updateProfile = useCallback(
async (profile: Profile) => {
setLoading(true);
setError(undefined);
try {
if (auth.currentUser) {
await fbUpdateProfile(auth.currentUser, profile);
return true;
} else {
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);
return [updateProfile, loading, error];
};
export const useVerifyBeforeUpdateEmail = (
auth: Auth
): VerifyBeforeUpdateEmailHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);
const verifyBeforeUpdateEmail = useCallback(
async (email: string, actionCodeSettings: ActionCodeSettings | null) => {
setLoading(true);
setError(undefined);
try {
if (auth.currentUser) {
await fbVerifyBeforeUpdateEmail(
auth.currentUser,
email,
actionCodeSettings
);
return true;
} else {
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);
return [verifyBeforeUpdateEmail, loading, error];
};