feat: require email verification before login
This commit is contained in:
1 parent
acdc219ad3
commit
964e7c8aff
2 files changed
+12
-3
No files matched your search
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext, useEffect, useState, ReactNode } from 'react';
|
||||
import { User, signInWithEmailAndPassword, signOut, onAuthStateChanged, createUserWithEmailAndPassword, updateProfile, deleteUser, sendEmailVerification, sendPasswordResetEmail } from 'firebase/auth';
|
||||
import { User, signInWithEmailAndPassword, signOut, onAuthStateChanged, createUserWithEmailAndPassword, updateProfile, deleteUser, sendEmailVerification, sendPasswordResetEmail, reload } from 'firebase/auth';
|
||||
import { auth, db, app } from '@/lib/firebase';
|
||||
import { doc, setDoc, query, where, collection, getDocs, deleteDoc } from 'firebase/firestore';
|
||||
|
||||
@@ -73,7 +73,16 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const isAdmin = user ? adminEmails.includes(user.email || '') : false;
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
await signInWithEmailAndPassword(auth, email, password);
|
||||
const userCredential = await signInWithEmailAndPassword(auth, email, password);
|
||||
|
||||
// Reload user to get latest emailVerified status
|
||||
await reload(userCredential.user);
|
||||
|
||||
// Check if email is verified
|
||||
if (!userCredential.user.emailVerified) {
|
||||
await signOut(auth);
|
||||
throw new Error('Please verify your email address before logging in. Check your inbox for the verification link.');
|
||||
}
|
||||
};
|
||||
|
||||
const signup = async (email: string, password: string, firstName?: string) => {
|
||||
|
||||
Reference in new issue
Block a user