Skip to content

pr request for useSendPasswordResetEmail hook + docs #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ The `useSendPasswordResetEmail` hook takes the following parameters:

Returns:

- `sendPasswordResetEmail(email: string)`: a function you can call to send a password reset emaail
- `sendPasswordResetEmail(email: string, actionCodeSettings?:ActionCodeSettings)`: a function you can call to send a password reset email. Optionally accepts an [actionCodeSettings](https://firebase.google.com/docs/reference/js/auth.actioncodesettings.md#actioncodesettings_interface) object as well.
- `sending`: A `boolean` to indicate whether the email is being sent
- `error`: Any `Error` returned by Firebase when trying to send the email, or `undefined` if there is no error

Expand All @@ -660,6 +660,10 @@ const SendPasswordReset = () => {
const [sendPasswordResetEmail, sending, error] = useSendPasswordResetEmail(
auth
);

const actionCodeSettings = {
url: 'https://www.example.com/login'
}

if (error) {
return (
Expand All @@ -680,7 +684,7 @@ const SendPasswordReset = () => {
/>
<button
onClick={async () => {
await sendPasswordResetEmail(email);
await sendPasswordResetEmail(email, actionCodeSettings);
alert('Sent email');
}}
>
Expand Down Expand Up @@ -743,4 +747,4 @@ const SendEmailVerification = () => {
</div>
);
};
```
```
10 changes: 7 additions & 3 deletions auth/useSendPasswordResetEmail.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
Auth,
AuthError,
ActionCodeSettings,
sendPasswordResetEmail as fbSendPasswordResetEmail,
} from 'firebase/auth';
import { useMemo, useState } from 'react';

export type SendPasswordResetEmailHook = [
(email: string) => Promise<void>,
(email: string, actionCodeSettings?: ActionCodeSettings) => Promise<void>,
boolean,
AuthError | Error | undefined
];
Expand All @@ -15,11 +16,14 @@ export default (auth: Auth): SendPasswordResetEmailHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);

const sendPasswordResetEmail = async (email: string) => {
const sendPasswordResetEmail = async (
email: string,
actionCodeSettings?: ActionCodeSettings
) => {
setLoading(true);
setError(undefined);
try {
await fbSendPasswordResetEmail(auth, email);
await fbSendPasswordResetEmail(auth, email, actionCodeSettings);
} catch (err) {
setError(err as AuthError);
} finally {
Expand Down