Skip to content

Commit 8208eb9

Browse files
committed
[auth] Make all functions return a success indicator
1 parent ac8bb13 commit 8208eb9

7 files changed

+113
-67
lines changed

Diff for: auth/README.md

+78-52
Large diffs are not rendered by default.

Diff for: auth/useDeleteUser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Auth, AuthError } from 'firebase/auth';
22
import { useCallback, useState } from 'react';
33

44
export type DeleteUserHook = [
5-
() => Promise<void>,
5+
() => Promise<boolean>,
66
boolean,
77
AuthError | Error | undefined
88
];
@@ -17,11 +17,13 @@ export default (auth: Auth): DeleteUserHook => {
1717
try {
1818
if (auth.currentUser) {
1919
await auth.currentUser.delete();
20+
return true;
2021
} else {
21-
setError(new Error('No user is logged in') as AuthError);
22+
throw new Error('No user is logged in');
2223
}
2324
} catch (err) {
2425
setError(err as AuthError);
26+
return false;
2527
} finally {
2628
setLoading(false);
2729
}

Diff for: auth/useSendEmailVerification.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { useCallback, useState } from 'react';
77

88
export type SendEmailVerificationHook = [
9-
() => Promise<void>,
9+
() => Promise<boolean>,
1010
boolean,
1111
AuthError | Error | undefined
1212
];
@@ -21,11 +21,13 @@ export default (auth: Auth): SendEmailVerificationHook => {
2121
try {
2222
if (auth.currentUser) {
2323
await fbSendEmailVerification(auth.currentUser);
24+
return true;
2425
} else {
25-
setError(new Error('No user is logged in') as AuthError);
26+
throw new Error('No user is logged in');
2627
}
2728
} catch (err) {
2829
setError(err as AuthError);
30+
return false;
2931
} finally {
3032
setLoading(false);
3133
}

Diff for: auth/useSendPasswordResetEmail.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { useCallback, useState } from 'react';
88

99
export type SendPasswordResetEmailHook = [
10-
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<void>,
10+
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<boolean>,
1111
boolean,
1212
AuthError | Error | undefined
1313
];
@@ -22,8 +22,10 @@ export default (auth: Auth): SendPasswordResetEmailHook => {
2222
setError(undefined);
2323
try {
2424
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
25+
return true;
2526
} catch (err) {
2627
setError(err as AuthError);
28+
return false;
2729
} finally {
2830
setLoading(false);
2931
}

Diff for: auth/useSendSignInLinkToEmail.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { useCallback, useState } from 'react';
88

99
export type SendSignInLinkToEmailHook = [
10-
(email: string, actionCodeSettings: ActionCodeSettings) => Promise<void>,
10+
(email: string, actionCodeSettings: ActionCodeSettings) => Promise<boolean>,
1111
boolean,
1212
AuthError | Error | undefined
1313
];
@@ -22,8 +22,10 @@ export default (auth: Auth): SendSignInLinkToEmailHook => {
2222
setError(undefined);
2323
try {
2424
await fbSendSignInLinkToEmail(auth, email, actionCodeSettings);
25+
return true;
2526
} catch (err) {
2627
setError(err as AuthError);
28+
return false;
2729
} finally {
2830
setLoading(false);
2931
}

Diff for: auth/useSignOut.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Auth, AuthError } from 'firebase/auth';
22
import { useCallback, useState } from 'react';
33

44
export type SignOutHook = [
5-
() => Promise<void>,
5+
() => Promise<boolean>,
66
boolean,
77
AuthError | Error | undefined
88
];
@@ -16,8 +16,10 @@ export default (auth: Auth): SignOutHook => {
1616
setError(undefined);
1717
try {
1818
await auth.signOut();
19+
return true;
1920
} catch (err) {
2021
setError(err as AuthError);
22+
return false;
2123
} finally {
2224
setLoading(false);
2325
}

Diff for: auth/useUpdateUser.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ type Profile = {
1616

1717
export type UpdateUserHook<M> = [M, boolean, AuthError | Error | undefined];
1818

19-
export type UpdateEmailHook = UpdateUserHook<(email: string) => Promise<void>>;
19+
export type UpdateEmailHook = UpdateUserHook<
20+
(email: string) => Promise<boolean>
21+
>;
2022
export type UpdatePasswordHook = UpdateUserHook<
21-
(password: string) => Promise<void>
23+
(password: string) => Promise<boolean>
2224
>;
2325
export type UpdateProfileHook = UpdateUserHook<
24-
(profile: Profile) => Promise<void>
26+
(profile: Profile) => Promise<boolean>
2527
>;
2628
export type VerifyBeforeUpdateEmailHook = UpdateUserHook<
2729
(
2830
email: string,
2931
actionCodeSettings: ActionCodeSettings | null
30-
) => Promise<void>
32+
) => Promise<boolean>
3133
>;
3234

3335
export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
@@ -41,11 +43,13 @@ export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
4143
try {
4244
if (auth.currentUser) {
4345
await fbUpdateEmail(auth.currentUser, email);
46+
return true;
4447
} else {
45-
setError(new Error('No user is logged in') as AuthError);
48+
throw new Error('No user is logged in');
4649
}
4750
} catch (err) {
4851
setError(err as AuthError);
52+
return false;
4953
} finally {
5054
setLoading(false);
5155
}
@@ -67,11 +71,13 @@ export const useUpdatePassword = (auth: Auth): UpdatePasswordHook => {
6771
try {
6872
if (auth.currentUser) {
6973
await fbUpdatePassword(auth.currentUser, password);
74+
return true;
7075
} else {
71-
setError(new Error('No user is logged in') as AuthError);
76+
throw new Error('No user is logged in');
7277
}
7378
} catch (err) {
7479
setError(err as AuthError);
80+
return false;
7581
} finally {
7682
setLoading(false);
7783
}
@@ -93,11 +99,13 @@ export const useUpdateProfile = (auth: Auth): UpdateProfileHook => {
9399
try {
94100
if (auth.currentUser) {
95101
await fbUpdateProfile(auth.currentUser, profile);
102+
return true;
96103
} else {
97-
setError(new Error('No user is logged in') as AuthError);
104+
throw new Error('No user is logged in');
98105
}
99106
} catch (err) {
100107
setError(err as AuthError);
108+
return false;
101109
} finally {
102110
setLoading(false);
103111
}
@@ -125,11 +133,13 @@ export const useVerifyBeforeUpdateEmail = (
125133
email,
126134
actionCodeSettings
127135
);
136+
return true;
128137
} else {
129-
setError(new Error('No user is logged in') as AuthError);
138+
throw new Error('No user is logged in');
130139
}
131140
} catch (err) {
132141
setError(err as AuthError);
142+
return false;
133143
} finally {
134144
setLoading(false);
135145
}

0 commit comments

Comments
 (0)