Skip to content

Commit fda7ab4

Browse files
committed
[auth] Make sure that all internal methods are wrapped with useCallback
1 parent 6b8d105 commit fda7ab4

9 files changed

+239
-265
lines changed

auth/useAuthState.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Auth, onAuthStateChanged, User } from 'firebase/auth';
2-
import { useEffect, useMemo } from 'react';
2+
import { useEffect } from 'react';
33
import { LoadingHook, useLoadingValue } from '../util';
44

55
export type AuthStateHook = LoadingHook<User | null, Error>;
@@ -36,6 +36,5 @@ export default (auth: Auth, options?: AuthStateOptions): AuthStateHook => {
3636
};
3737
}, [auth]);
3838

39-
const resArray: AuthStateHook = [value, loading, error];
40-
return useMemo<AuthStateHook>(() => resArray, resArray);
39+
return [value, loading, error];
4140
};

auth/useCreateUserWithEmailAndPassword.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
sendEmailVerification,
66
UserCredential,
77
} from 'firebase/auth';
8-
import { useMemo, useState } from 'react';
8+
import { useCallback, useState } from 'react';
99
import { CreateUserOptions, EmailAndPasswordActionHook } from './types';
1010

1111
export default (
@@ -16,37 +16,31 @@ export default (
1616
const [registeredUser, setRegisteredUser] = useState<UserCredential>();
1717
const [loading, setLoading] = useState<boolean>(false);
1818

19-
const createUserWithEmailAndPassword = async (
20-
email: string,
21-
password: string
22-
) => {
23-
setLoading(true);
24-
setError(undefined);
25-
try {
26-
const user = await firebaseCreateUserWithEmailAndPassword(
27-
auth,
28-
email,
29-
password
30-
);
31-
if (options && options.sendEmailVerification && user.user) {
32-
await sendEmailVerification(
33-
user.user,
34-
options.emailVerificationOptions
19+
const createUserWithEmailAndPassword = useCallback(
20+
async (email: string, password: string) => {
21+
setLoading(true);
22+
setError(undefined);
23+
try {
24+
const user = await firebaseCreateUserWithEmailAndPassword(
25+
auth,
26+
email,
27+
password
3528
);
29+
if (options && options.sendEmailVerification && user.user) {
30+
await sendEmailVerification(
31+
user.user,
32+
options.emailVerificationOptions
33+
);
34+
}
35+
setRegisteredUser(user);
36+
} catch (error) {
37+
setError(error as AuthError);
38+
} finally {
39+
setLoading(false);
3640
}
37-
setRegisteredUser(user);
38-
} catch (error) {
39-
setError(error as AuthError);
40-
} finally {
41-
setLoading(false);
42-
}
43-
};
41+
},
42+
[auth, options]
43+
);
4444

45-
const resArray: EmailAndPasswordActionHook = [
46-
createUserWithEmailAndPassword,
47-
registeredUser,
48-
loading,
49-
error,
50-
];
51-
return useMemo<EmailAndPasswordActionHook>(() => resArray, resArray);
45+
return [createUserWithEmailAndPassword, registeredUser, loading, error];
5246
};

auth/useDeleteUser.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Auth, AuthError } from 'firebase/auth';
2-
import { useMemo, useState } from 'react';
2+
import { useCallback, useState } from 'react';
33

44
export type DeleteUserHook = [
55
() => Promise<void>,
@@ -11,7 +11,7 @@ export default (auth: Auth): DeleteUserHook => {
1111
const [error, setError] = useState<AuthError>();
1212
const [loading, setLoading] = useState<boolean>(false);
1313

14-
const deleteUser = async () => {
14+
const deleteUser = useCallback(async () => {
1515
setLoading(true);
1616
setError(undefined);
1717
try {
@@ -25,8 +25,7 @@ export default (auth: Auth): DeleteUserHook => {
2525
} finally {
2626
setLoading(false);
2727
}
28-
};
28+
}, [auth]);
2929

30-
const resArray: DeleteUserHook = [deleteUser, loading, error];
31-
return useMemo<DeleteUserHook>(() => resArray, resArray);
30+
return [deleteUser, loading, error];
3231
};

auth/useSendEmailVerification.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
AuthError,
44
sendEmailVerification as fbSendEmailVerification,
55
} from 'firebase/auth';
6-
import { useMemo, useState } from 'react';
6+
import { useCallback, useState } from 'react';
77

88
export type SendEmailVerificationHook = [
99
() => Promise<void>,
@@ -15,7 +15,7 @@ export default (auth: Auth): SendEmailVerificationHook => {
1515
const [error, setError] = useState<AuthError>();
1616
const [loading, setLoading] = useState<boolean>(false);
1717

18-
const sendEmailVerification = async () => {
18+
const sendEmailVerification = useCallback(async () => {
1919
setLoading(true);
2020
setError(undefined);
2121
try {
@@ -29,12 +29,7 @@ export default (auth: Auth): SendEmailVerificationHook => {
2929
} finally {
3030
setLoading(false);
3131
}
32-
};
32+
}, [auth]);
3333

34-
const resArray: SendEmailVerificationHook = [
35-
sendEmailVerification,
36-
loading,
37-
error,
38-
];
39-
return useMemo<SendEmailVerificationHook>(() => resArray, resArray);
34+
return [sendEmailVerification, loading, error];
4035
};

auth/useSendPasswordResetEmail.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2+
ActionCodeSettings,
23
Auth,
34
AuthError,
4-
ActionCodeSettings,
55
sendPasswordResetEmail as fbSendPasswordResetEmail,
66
} from 'firebase/auth';
7-
import { useMemo, useState } from 'react';
7+
import { useCallback, useState } from 'react';
88

99
export type SendPasswordResetEmailHook = [
1010
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<void>,
@@ -16,25 +16,20 @@ export default (auth: Auth): SendPasswordResetEmailHook => {
1616
const [error, setError] = useState<AuthError>();
1717
const [loading, setLoading] = useState<boolean>(false);
1818

19-
const sendPasswordResetEmail = async (
20-
email: string,
21-
actionCodeSettings?: ActionCodeSettings
22-
) => {
23-
setLoading(true);
24-
setError(undefined);
25-
try {
26-
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
27-
} catch (err) {
28-
setError(err as AuthError);
29-
} finally {
30-
setLoading(false);
31-
}
32-
};
19+
const sendPasswordResetEmail = useCallback(
20+
async (email: string, actionCodeSettings?: ActionCodeSettings) => {
21+
setLoading(true);
22+
setError(undefined);
23+
try {
24+
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
25+
} catch (err) {
26+
setError(err as AuthError);
27+
} finally {
28+
setLoading(false);
29+
}
30+
},
31+
[auth]
32+
);
3333

34-
const resArray: SendPasswordResetEmailHook = [
35-
sendPasswordResetEmail,
36-
loading,
37-
error,
38-
];
39-
return useMemo<SendPasswordResetEmailHook>(() => resArray, resArray);
34+
return [sendPasswordResetEmail, loading, error];
4035
};
Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
import {
22
Auth,
3-
UserCredential,
4-
signInWithEmailAndPassword as firebaseSignInWithEmailAndPassword,
53
AuthError,
4+
signInWithEmailAndPassword as firebaseSignInWithEmailAndPassword,
5+
UserCredential,
66
} from 'firebase/auth';
7-
import { useState, useMemo } from 'react';
7+
import { useCallback, useState } from 'react';
88
import { EmailAndPasswordActionHook } from './types';
99

1010
export default (auth: Auth): EmailAndPasswordActionHook => {
1111
const [error, setError] = useState<AuthError>();
1212
const [loggedInUser, setLoggedInUser] = useState<UserCredential>();
1313
const [loading, setLoading] = useState<boolean>(false);
1414

15-
const signInWithEmailAndPassword = async (
16-
email: string,
17-
password: string
18-
) => {
19-
setLoading(true);
20-
setError(undefined);
21-
try {
22-
const user = await firebaseSignInWithEmailAndPassword(
23-
auth,
24-
email,
25-
password
26-
);
27-
setLoggedInUser(user);
28-
} catch (err) {
29-
setError(err as AuthError);
30-
} finally {
31-
setLoading(false);
32-
}
33-
};
15+
const signInWithEmailAndPassword = useCallback(
16+
async (email: string, password: string) => {
17+
setLoading(true);
18+
setError(undefined);
19+
try {
20+
const user = await firebaseSignInWithEmailAndPassword(
21+
auth,
22+
email,
23+
password
24+
);
25+
setLoggedInUser(user);
26+
} catch (err) {
27+
setError(err as AuthError);
28+
} finally {
29+
setLoading(false);
30+
}
31+
},
32+
[auth]
33+
);
3434

35-
const resArray: EmailAndPasswordActionHook = [
36-
signInWithEmailAndPassword,
37-
loggedInUser,
38-
loading,
39-
error,
40-
];
41-
return useMemo<EmailAndPasswordActionHook>(() => resArray, resArray);
35+
return [signInWithEmailAndPassword, loggedInUser, loading, error];
4236
};

0 commit comments

Comments
 (0)