Skip to content
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

[auth]: Add useConfirmPasswordReset hook for Firebase password reset #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions auth/useConfirmPasswordReset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
ActionCodeSettings,
Auth,
AuthError,
confirmPasswordReset as fbConfirmPasswordReset,
} from 'firebase/auth';
import { useCallback, useState } from 'react';

export type ConfirmPasswordResetHook = [
(email: string, newPassword: string) => Promise<boolean>,
boolean,
AuthError | Error | undefined
];

export default (auth: Auth): ConfirmPasswordResetHook => {
const [error, setError] = useState<AuthError>();
const [loading, setLoading] = useState<boolean>(false);

const confirmPasswordReset = useCallback(
async (oobCode: string, newPassword:string) => {
setLoading(true);
setError(undefined);
try {
await fbConfirmPasswordReset(auth, oobCode, newPassword);
return true;
} catch (err) {
setError(err as AuthError);
return false;
} finally {
setLoading(false);
}
},
[auth]
);

return [confirmPasswordReset, loading, error];
};