From 9c7a34f9302de6abb6a567a2646d994a412c0598 Mon Sep 17 00:00:00 2001 From: oura-hideyoshi Date: Mon, 22 Apr 2024 22:29:06 +0900 Subject: [PATCH] update: Add useConfirmPasswordReset hook for Firebase password reset --- auth/useConfirmPasswordReset.ts | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 auth/useConfirmPasswordReset.ts diff --git a/auth/useConfirmPasswordReset.ts b/auth/useConfirmPasswordReset.ts new file mode 100644 index 0000000..551744e --- /dev/null +++ b/auth/useConfirmPasswordReset.ts @@ -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, + AuthError | Error | undefined + ]; + + export default (auth: Auth): ConfirmPasswordResetHook => { + const [error, setError] = useState(); + const [loading, setLoading] = useState(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]; + }; + \ No newline at end of file