Skip to content

Commit 68d422c

Browse files
committed
[auth] add useVerifyBeforeUpdateEmail hook
1 parent 54f4c0a commit 68d422c

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

auth/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ List of Auth hooks:
2323
- [useUpdateEmail](#useupdateemail)
2424
- [useUpdatePassword](#useupdatepassword)
2525
- [useUpdateProfile](#useupdateprofile)
26+
- [useVerifyBeforeUpdateEmail](#useverifybeforeupdateemail)
2627
- [useSendPasswordResetEmail](#usesendpasswordresetemail)
2728
- [useSendEmailVerification](#usesendemailverification)
2829
- [useSignOut](#usesignout)
@@ -469,7 +470,7 @@ The `useUpdateEmail` hook takes the following parameters:
469470

470471
Returns:
471472

472-
- `updateEmail(email: string)`: a function you can call to update the current user's email addres
473+
- `updateEmail(email: string)`: a function you can call to update the current user's email address
473474
- `updating`: A `boolean` to indicate whether the user update is processing
474475
- `error`: Any `Error` returned by Firebase when trying to update the user, or `undefined` if there is no error
475476

@@ -632,6 +633,63 @@ const UpdateProfile = () => {
632633
};
633634
```
634635

636+
### useVerifyBeforeUpdateEmail
637+
638+
```js
639+
const [verifyBeforeUpdateEmail, updating, error] = useVerifyBeforeUpdateEmail(auth);
640+
```
641+
642+
Verify and update the current user's email address. Wraps the underlying `auth.verifyBeforeUpdateEmail` method and provides additional `updating` and `error` information.
643+
644+
The `useVerifyBeforeUpdateEmail` hook takes the following parameters:
645+
646+
- `auth`: `Auth` instance for the app you would like to monitor
647+
648+
Returns:
649+
650+
- `verifyBeforeUpdateEmail(email: string, actionCodeSettings: ActionCodeSettings | null)`: a function you can call to verify and update the current user's email address
651+
- `updating`: A `boolean` to indicate whether the user update is processing
652+
- `error`: Any `Error` returned by Firebase when trying to update the user, or `undefined` if there is no error
653+
654+
#### Full Example
655+
656+
```jsx
657+
import { useVerifyBeforeUpdateEmail } from 'react-firebase-hooks/auth';
658+
659+
const UpdateEmail = () => {
660+
const [email, setEmail] = useState('');
661+
const [verifyBeforeUpdateEmail, updating, error] = useVerifyBeforeUpdateEmail(auth);
662+
663+
if (error) {
664+
return (
665+
<div>
666+
<p>Error: {error.message}</p>
667+
</div>
668+
);
669+
}
670+
if (updating) {
671+
return <p>Updating...</p>;
672+
}
673+
return (
674+
<div className="App">
675+
<input
676+
type="email"
677+
value={email}
678+
onChange={(e) => setEmail(e.target.value)}
679+
/>
680+
<button
681+
onClick={async () => {
682+
await verifyBeforeUpdateEmail(email);
683+
alert('Please check your email to verify your updated email address');
684+
}}
685+
>
686+
Update email
687+
</button>
688+
</div>
689+
);
690+
};
691+
```
692+
635693
### useSendPasswordResetEmail
636694

637695
```js

auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ export {
2424
useUpdateEmail,
2525
useUpdatePassword,
2626
useUpdateProfile,
27+
useVerifyBeforeUpdateEmail,
2728
UpdateEmailHook,
2829
UpdatePasswordHook,
2930
UpdateProfileHook,
31+
VerifyBeforeUpdateEmailHook,
3032
} from './useUpdateUser';
3133

3234
export { EmailAndPasswordActionHook, SignInWithPopupHook } from './types';

auth/useUpdateUser.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
2+
ActionCodeSettings,
23
Auth,
34
AuthError,
45
updateEmail as fbUpdateEmail,
56
updatePassword as fbUpdatePassword,
67
updateProfile as fbUpdateProfile,
8+
verifyBeforeUpdateEmail as fbVerifyBeforeUpdateEmail,
79
} from 'firebase/auth';
810
import { useMemo, useState } from 'react';
911

@@ -21,6 +23,12 @@ export type UpdatePasswordHook = UpdateUserHook<
2123
export type UpdateProfileHook = UpdateUserHook<
2224
(profile: Profile) => Promise<void>
2325
>;
26+
export type VerifyBeforeUpdateEmailHook = UpdateUserHook<
27+
(
28+
email: string,
29+
actionCodeSettings: ActionCodeSettings | null
30+
) => Promise<void>
31+
>;
2432

2533
export const useUpdateEmail = (auth: Auth): UpdateEmailHook => {
2634
const [error, setError] = useState<AuthError>();
@@ -93,3 +101,36 @@ export const useUpdateProfile = (auth: Auth): UpdateProfileHook => {
93101
const resArray: UpdateProfileHook = [updateProfile, loading, error];
94102
return useMemo<UpdateProfileHook>(() => resArray, resArray);
95103
};
104+
105+
export const useVerifyBeforeUpdateEmail = (
106+
auth: Auth
107+
): VerifyBeforeUpdateEmailHook => {
108+
const [error, setError] = useState<AuthError>();
109+
const [loading, setLoading] = useState<boolean>(false);
110+
111+
const verifyBeforeUpdateEmail = async (
112+
email: string,
113+
actionCodeSettings: ActionCodeSettings | null
114+
) => {
115+
setLoading(true);
116+
setError(undefined);
117+
try {
118+
if (auth.currentUser) {
119+
await fbVerifyBeforeUpdateEmail(auth.currentUser, email, actionCodeSettings);
120+
} else {
121+
setError(new Error('No user is logged in') as AuthError);
122+
}
123+
} catch (err) {
124+
setError(err as AuthError);
125+
} finally {
126+
setLoading(false);
127+
}
128+
};
129+
130+
const resArray: VerifyBeforeUpdateEmailHook = [
131+
verifyBeforeUpdateEmail,
132+
loading,
133+
error,
134+
];
135+
return useMemo<VerifyBeforeUpdateEmailHook>(() => resArray, resArray);
136+
};

0 commit comments

Comments
 (0)