mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 18:13:48 -04:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import {sendEmailVerification, User} from 'firebase/auth'
|
|
import toast from 'react-hot-toast'
|
|
|
|
export const sendVerificationEmail = async (user: User | null | undefined, t: any) => {
|
|
// if (!privateUser?.email) {
|
|
// toast.error(t('settings.email.no_email', 'No email found on your account.'))
|
|
// return
|
|
// }
|
|
if (!user) {
|
|
toast.error(
|
|
t('settings.email.must_sign_in', 'You must be signed in to send a verification email.'),
|
|
)
|
|
return
|
|
}
|
|
if (user?.emailVerified) {
|
|
toast.success(t('settings.email.verified', 'Email Verified ✔️'))
|
|
return true
|
|
}
|
|
toast
|
|
.promise(sendEmailVerification(user), {
|
|
loading: t('settings.email.sending', 'Sending verification email...'),
|
|
success: t(
|
|
'settings.email.verification_sent',
|
|
'Verification email sent — check your inbox and spam.',
|
|
),
|
|
error: t('settings.email.verification_failed', 'Failed to send verification email.'),
|
|
})
|
|
.catch((e) => {
|
|
console.error('Failed to send verification email', e)
|
|
if (e?.code === 'auth/too-many-requests') {
|
|
toast.error(
|
|
t(
|
|
'settings.email.too_many_requests',
|
|
"You can't request more than one email per minute. Please wait before sending another request.",
|
|
),
|
|
)
|
|
}
|
|
return
|
|
})
|
|
|
|
async function waitForEmailVerification(intervalMs = 2000, timeoutMs = 5 * 60 * 1000) {
|
|
const start = Date.now()
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
if (!user) return false
|
|
|
|
// Refresh user record from Firebase
|
|
await user.reload()
|
|
|
|
if (user.emailVerified) {
|
|
// IMPORTANT: force a new ID token with updated claims
|
|
await user.getIdToken(true)
|
|
toast.success(t('settings.email.verified', 'Email Verified ✔️'))
|
|
return true
|
|
}
|
|
|
|
await new Promise((r) => setTimeout(r, intervalMs))
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
waitForEmailVerification()
|
|
}
|