Auth & backend
Verify signup emails in Auth.js (NextAuth v5)
Check new magic-link emails in the Auth.js v5 signIn callback and credentials signups in your own server action: refuse undeliverable and disposable addresses.
Updated 2026-11-26
This recipe adds an email check to the two ways people create an account with Auth.js v5 in Next.js: the email (magic link) provider, through the signIn callback, and a credentials sign-up, through your own server action. Both refuse addresses that cannot receive mail or belong to a disposable inbox service, and both let the person through when the API cannot answer. There is no native Qelka provider for Auth.js: you add one helper and a few lines.
Auth.js v5 is installed with npm install next-auth@beta; the Auth.js site now says the project is part of Better Auth. If you are starting from scratch, see the Better Auth integration.
What you need
- A Next.js App Router project with Auth.js v5 (
auth.tsat the root, the route handler inapp/api/auth/[...nextauth]/route.ts). - For magic links: an email provider such as Resend or Nodemailer, and a database adapter, which Auth.js requires to store verification tokens.
- A Qelka API key in the
QELKA_API_KEYenvironment variable, in.env.localfor development and in your host's secret settings in production. Ask_test_key returns simulated results and never spends credits: see Test mode. A restricted key limited toemailandipis enough.
Steps
1. The check helper
Save as lib/signup-check.ts. It is server-only code: never import it in a client component.
type EmailResult = {
status: string
reason: string
did_you_mean: string | null
checks: { disposable: boolean }
}
export type EmailVerdict = { allowed: boolean; reason: string; suggestion: string | null }
async function lookup<T>(path: string, body: Record<string, unknown>): Promise<T | null> {
try {
const response = await fetch(`https://api.qelka.com/v1${path}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.QELKA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(5000),
})
if (response.ok) return (await response.json()) as T
const failure = await response.json().catch(() => null)
console.warn(`${path} check skipped: ${failure?.error?.type ?? response.status} request ${response.headers.get('X-Request-Id')}`)
} catch {
console.warn(`${path} check skipped: unreachable`)
}
return null
}
export async function checkSignupEmail(email: string): Promise<EmailVerdict> {
const result = await lookup<EmailResult>('/email', { email, mode: 'fast' })
if (!result) return { allowed: true, reason: 'unchecked', suggestion: null }
if (result.status === 'undeliverable') {
return { allowed: false, reason: result.reason, suggestion: result.did_you_mean }
}
if (result.checks.disposable) return { allowed: false, reason: 'disposable', suggestion: null }
return { allowed: true, reason: result.reason, suggestion: result.did_you_mean }
}
export async function needsReview(ip: string): Promise<boolean> {
const result = await lookup<{ risk: { level: string }; flags: { tor: boolean } }>('/ip', { ip })
return result ? result.risk.level === 'high' || result.flags.tor : false
}
mode: 'fast' runs the syntax, DNS and list checks and skips the mailbox check, so the answer never waits on someone else's mail server. Fast mode catches typos, dead domains and disposable inboxes; for magic links, the link itself then proves the mailbox. See the email reference.
2. Magic links: the signIn callback
In auth.ts:
import NextAuth from 'next-auth'
import Resend from 'next-auth/providers/resend'
import { PrismaAdapter } from '@auth/prisma-adapter'
import { prisma } from '@/lib/prisma'
import { checkSignupEmail } from '@/lib/signup-check'
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Resend],
pages: { signIn: '/login' },
callbacks: {
async signIn({ user, email }) {
if (!email?.verificationRequest || !user.email) return true
if ('emailVerified' in user && user.emailVerified) return true
const verdict = await checkSignupEmail(user.email)
if (verdict.allowed) return true
const params = new URLSearchParams({ error: 'EmailRejected', reason: verdict.reason })
if (verdict.suggestion) params.set('suggestion', verdict.suggestion)
return `/login?${params}`
},
},
})
Any adapter works; Prisma is only the example. The Resend provider reads its key from AUTH_RESEND_KEY.
How the callback behaves:
email.verificationRequestistrueonly on the first call, when someone asks for a link. When they click it, the property is absent and the callback returnstrueat once.- A known address arrives with the stored user, whose
emailVerifiedis set after their first sign-in, so existing users are never checked again. - Returning a string redirects to it, so the login page can show the reason and the suggestion from the query string. Returning
falsewould send the person to the generic error page instead.
On your /login page, read error, reason and suggestion from the search params and show a message next to the email field: "This address cannot receive mail. Did you mean jane@gmail.com?" for an undeliverable reason, "Please use a permanent email address" for disposable.
3. Credentials: check in your sign-up action
The Credentials provider signs existing users in; creating them is your code. Check before you write anything, for example in app/signup/actions.ts:
'use server'
import { headers } from 'next/headers'
import { redirect } from 'next/navigation'
import { checkSignupEmail, needsReview } from '@/lib/signup-check'
import { createUser } from '@/lib/users'
export type SignUpState = { error: string | null }
export async function signUp(_previous: SignUpState, formData: FormData): Promise<SignUpState> {
const email = String(formData.get('email') ?? '').trim()
const password = String(formData.get('password') ?? '')
const ip = (await headers()).get('x-forwarded-for')?.split(',')[0]?.trim()
const [verdict, review] = await Promise.all([
checkSignupEmail(email),
ip ? needsReview(ip) : Promise.resolve(false),
])
if (!verdict.allowed && verdict.reason === 'disposable') {
return { error: 'Please sign up with a permanent email address.' }
}
if (!verdict.allowed) {
return { error: verdict.suggestion ? `This address cannot receive mail. Did you mean ${verdict.suggestion}?` : 'This address cannot receive mail. Please check it.' }
}
await createUser({ email, password, emailStatus: verdict.reason, needsReview: review })
redirect('/login?registered=1')
}
createUser stands for your own function that hashes the password and inserts the row. Wire the action to the form with React's useActionState and render state.error under the email field. review flags signups from high-risk networks or Tor: store it, and ask for an extra step later rather than refusing, since many people use a VPN. Read the client address from the header your host sets; Flag VPN and data center traffic explains why.
4. What each answer does
| Result | Meaning | Action |
|---|---|---|
undeliverable |
Bad syntax, or a domain that does not exist or takes no mail | Refused, with did_you_mean when there is one |
risky, reason disposable |
A disposable inbox service | Refused with a request for a permanent address |
risky, other reasons |
A catch-all domain or a full mailbox | Accepted |
unknown |
In fast mode, smtp_not_checked: nothing wrong was found |
Accepted |
| No answer | API unreachable, key or balance problem | Accepted, reason unchecked, logged |
Never refuse on unknown. To flag disposable inboxes instead of refusing them, return true from the callback and store the reason; Block disposable email signups helps you choose.
5. Test it
With a test key, request a link for disposable@example.com or nomx@example.com: you land on /login with the reason, and no email is sent. jane@example.com gets its link, since fast mode answers unknown with smtp_not_checked for it.
Handle errors and rate limits
Nothing is billed on errors. The helper never retries inside a sign-in: a 429 or a 5xx answer becomes unchecked, so the form stays fast. 401, 402 and 403 mean a wrong key, an empty balance or a restricted key: alert on those log lines, which carry the error type and the request id. A key allows 200 requests per 10 seconds, and each email check costs 1 credit. Rate limit your sign-up and magic-link routes, so a bot cannot spend your credits or flood inboxes. See Errors and Rate limits.
Related
Frequently asked questions
Why check in the signIn callback and not when the email is sent?
Does the check run on every magic-link sign-in?
Does Auth.js handle sign-up for the Credentials provider?
More in Auth & backend
All integrationsPut this check in your product
Get an API key in under a minute. 100 free credits every month, no card required.
- 100 free credits a month
- No card
- Test keys spend nothing
- Cancel any time