-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve email verification with timeout and fallback handling
- Loading branch information
1 parent
95e45d4
commit df7c4ef
Showing
2 changed files
with
36 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
// verifier is an email verification service | ||
|
||
import { getRequiredServerEnvVar } from './misc.tsx' | ||
import { getErrorMessage, getRequiredServerEnvVar } from './misc.tsx' | ||
|
||
const VERIFIER_API_KEY = getRequiredServerEnvVar('VERIFIER_API_KEY') | ||
|
||
type VerifierResult = | ||
| { status: true; email: string; domain: string } | ||
| { | ||
status: false | ||
error: { code: number; message: string } | ||
} | ||
|
||
export async function verifyEmailAddress(emailAddress: string) { | ||
const verifierUrl = new URL( | ||
`https://verifier.meetchopra.com/verify/${emailAddress}`, | ||
) | ||
verifierUrl.searchParams.append('token', VERIFIER_API_KEY) | ||
type VerifierResult = | ||
| { status: true; email: string; domain: string } | ||
| { | ||
status: false | ||
error: { code: number; message: string } | ||
} | ||
const response = await fetch(verifierUrl.toString()) | ||
const verifierResult: VerifierResult = await response.json() | ||
return verifierResult | ||
const controller = new AbortController() | ||
const timeoutId = setTimeout(() => controller.abort(), 2000) | ||
try { | ||
const response = await fetch(verifierUrl.toString(), { | ||
signal: controller.signal, | ||
}) | ||
clearTimeout(timeoutId) | ||
const verifierResult: VerifierResult = await response.json() | ||
return verifierResult | ||
} catch (error: unknown) { | ||
clearTimeout(timeoutId) | ||
if (error instanceof Error && error.name === 'AbortError') { | ||
console.error('Email verification timed out:', emailAddress) | ||
} else { | ||
console.error('Error verifying email:', getErrorMessage(error)) | ||
} | ||
// If the request times out, we'll return a default result | ||
// we don't I wanna block the user from logging in if we can't verify the email address | ||
// is invalid... | ||
return { | ||
status: true, | ||
email: emailAddress, | ||
domain: emailAddress.split('@')[1], | ||
} | ||
} | ||
} |