Skip to content

Commit

Permalink
[auth] Make all functions return a success indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbianca committed Nov 9, 2022
1 parent ac8bb13 commit 8208eb9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 67 deletions.
130 changes: 78 additions & 52 deletions auth/README.md

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions auth/useDeleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Auth, AuthError } from 'firebase/auth';
import { useCallback, useState } from 'react';

export type DeleteUserHook = [
() => Promise<void>,
() => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -17,11 +17,13 @@ export default (auth: Auth): DeleteUserHook => {
try {
if (auth.currentUser) {
await auth.currentUser.delete();
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down
6 changes: 4 additions & 2 deletions auth/useSendEmailVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { useCallback, useState } from 'react';

export type SendEmailVerificationHook = [
() => Promise<void>,
() => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -21,11 +21,13 @@ export default (auth: Auth): SendEmailVerificationHook => {
try {
if (auth.currentUser) {
await fbSendEmailVerification(auth.currentUser);
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down
4 changes: 3 additions & 1 deletion auth/useSendPasswordResetEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { useCallback, useState } from 'react';

export type SendPasswordResetEmailHook = [
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<void>,
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -22,8 +22,10 @@ export default (auth: Auth): SendPasswordResetEmailHook => {
setError(undefined);
try {
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
return true;
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down
4 changes: 3 additions & 1 deletion auth/useSendSignInLinkToEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { useCallback, useState } from 'react';

export type SendSignInLinkToEmailHook = [
(email: string, actionCodeSettings: ActionCodeSettings) => Promise<void>,
(email: string, actionCodeSettings: ActionCodeSettings) => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -22,8 +22,10 @@ export default (auth: Auth): SendSignInLinkToEmailHook => {
setError(undefined);
try {
await fbSendSignInLinkToEmail(auth, email, actionCodeSettings);
return true;
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down
4 changes: 3 additions & 1 deletion auth/useSignOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Auth, AuthError } from 'firebase/auth';
import { useCallback, useState } from 'react';

export type SignOutHook = [
() => Promise<void>,
() => Promise<boolean>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -16,8 +16,10 @@ export default (auth: Auth): SignOutHook => {
setError(undefined);
try {
await auth.signOut();
return true;
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down
26 changes: 18 additions & 8 deletions auth/useUpdateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ type Profile = {

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

export type UpdateEmailHook = UpdateUserHook<(email: string) => Promise<void>>;
export type UpdateEmailHook = UpdateUserHook<
(email: string) => Promise<boolean>
>;
export type UpdatePasswordHook = UpdateUserHook<
(password: string) => Promise<void>
(password: string) => Promise<boolean>
>;
export type UpdateProfileHook = UpdateUserHook<
(profile: Profile) => Promise<void>
(profile: Profile) => Promise<boolean>
>;
export type VerifyBeforeUpdateEmailHook = UpdateUserHook<
(
email: string,
actionCodeSettings: ActionCodeSettings | null
) => Promise<void>
) => Promise<boolean>
>;

export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
Expand All @@ -41,11 +43,13 @@ export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
try {
if (auth.currentUser) {
await fbUpdateEmail(auth.currentUser, email);
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand All @@ -67,11 +71,13 @@ export const useUpdatePassword = (auth: Auth): UpdatePasswordHook => {
try {
if (auth.currentUser) {
await fbUpdatePassword(auth.currentUser, password);
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand All @@ -93,11 +99,13 @@ export const useUpdateProfile = (auth: Auth): UpdateProfileHook => {
try {
if (auth.currentUser) {
await fbUpdateProfile(auth.currentUser, profile);
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -125,11 +133,13 @@ export const useVerifyBeforeUpdateEmail = (
email,
actionCodeSettings
);
return true;
} else {
setError(new Error('No user is logged in') as AuthError);
throw new Error('No user is logged in');
}
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
Expand Down

0 comments on commit 8208eb9

Please sign in to comment.