feat(auth): implement forgot-password flow with Firebase Auth
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
setPersistence,
|
||||
browserLocalPersistence,
|
||||
sendPasswordResetEmail,
|
||||
confirmPasswordReset,
|
||||
} from "firebase/auth";
|
||||
import type { User, AuthError } from "firebase/auth";
|
||||
import { app} from "../features/auth/firebase"
|
||||
@@ -70,6 +71,20 @@ export const sendPasswordReset = async (email: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reset password with code and new password
|
||||
* Used after user clicks the link in the reset email
|
||||
*/
|
||||
export const resetPassword = async (code: string, newPassword: string) => {
|
||||
try {
|
||||
await confirmPasswordReset(auth, code, newPassword);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
const authError = error as AuthError;
|
||||
return { success: false, error: getAuthErrorMessage(authError.code) };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Subscribe to auth state changes
|
||||
* Returns unsubscribe function
|
||||
@@ -92,15 +107,18 @@ const getAuthErrorMessage = (errorCode: string): string => {
|
||||
const errorMessages: Record<string, string> = {
|
||||
"auth/invalid-email": "Invalid email address format.",
|
||||
"auth/user-disabled": "This user account has been disabled.",
|
||||
"auth/user-not-found": "Invalid email or password.",
|
||||
"auth/user-not-found": "No account found with this email address.",
|
||||
"auth/wrong-password": "Invalid email or password.",
|
||||
"auth/invalid-credential": "Invalid email or password.",
|
||||
"auth/too-many-requests": "Too many login attempts. Please try again later.",
|
||||
"auth/operation-not-allowed": "Login is currently disabled. Please try again later.",
|
||||
"auth/network-request-failed": "Network error. Please check your connection.",
|
||||
"auth/invalid-action-code": "This password reset link is invalid or has expired.",
|
||||
"auth/expired-action-code": "This password reset link has expired. Please request a new one.",
|
||||
"auth/weak-password": "Password is too weak. Please choose a stronger password.",
|
||||
};
|
||||
|
||||
return errorMessages[errorCode] || "An error occurred during login. Please try again.";
|
||||
return errorMessages[errorCode] || "An error occurred. Please try again.";
|
||||
};
|
||||
export { app };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user