-
Notifications
You must be signed in to change notification settings - Fork 4
Fix email auth confirmation to use token-hash verification #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
96af8b8
0c8dd75
ee38554
e288481
2801d7d
5231044
e2e4694
f013495
2ecd280
9d50c7a
ee5a33a
1947f4d
a546dea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { captureException, captureMessage } from '@sentry/nextjs'; | ||
| import type { EmailOtpType } from '@supabase/supabase-js'; | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { recordSignupSideEffects } from '@/lib/auth/signup-side-effects'; | ||
| import { | ||
| createAuthRedirectResponse, | ||
| getLocaleFromAuthHints, | ||
| getSafeAuthRedirectPath, | ||
| } from '@/lib/supabase/auth-redirect'; | ||
| import { createClient } from '@/lib/supabase/server'; | ||
|
|
||
| const EMAIL_OTP_TYPES: ReadonlySet<EmailOtpType> = new Set([ | ||
| 'signup', | ||
| 'invite', | ||
| 'magiclink', | ||
| 'recovery', | ||
| 'email_change', | ||
| 'email', | ||
| ] satisfies EmailOtpType[]); | ||
|
|
||
| const SIGNUP_EMAIL_OTP_TYPES: ReadonlySet<EmailOtpType> = new Set([ | ||
| 'signup', | ||
| 'email', | ||
| ] satisfies EmailOtpType[]); | ||
|
|
||
| const isEmailOtpType = (value: string | null): value is EmailOtpType => | ||
| Boolean(value && EMAIL_OTP_TYPES.has(value as EmailOtpType)); | ||
|
|
||
| const isSignupEmailOtpType = (type: EmailOtpType) => | ||
| SIGNUP_EMAIL_OTP_TYPES.has(type); | ||
|
|
||
| const getDefaultSuccessPath = (type: EmailOtpType, locale: string) => | ||
| type === 'recovery' | ||
| ? `/${locale}/protected/update-password` | ||
| : `/${locale}/dashboard`; | ||
|
|
||
| export async function GET(request: Request) { | ||
| // Email auth links use token_hash + verifyOtp so confirmation and recovery | ||
| // links do not depend on a browser-local PKCE code verifier. | ||
| // https://supabase.com/docs/guides/auth/auth-email-templates#redirecting-the-user-to-a-server-side-endpoint | ||
| const requestUrl = new URL(request.url); | ||
| const origin = requestUrl.origin; | ||
| const tokenHash = requestUrl.searchParams.get('token_hash'); | ||
| const type = requestUrl.searchParams.get('type'); | ||
| const redirectTo = | ||
| requestUrl.searchParams.get('redirect_to') ?? | ||
| requestUrl.searchParams.get('next'); | ||
| const safeRedirectPath = getSafeAuthRedirectPath(redirectTo, origin); | ||
| const locale = getLocaleFromAuthHints({ | ||
| acceptLanguage: request.headers.get('accept-language'), | ||
| locale: requestUrl.searchParams.get('lang'), | ||
| redirectPath: safeRedirectPath, | ||
| }); | ||
| const loginPath = `/${locale}/login`; | ||
|
|
||
| if (!(tokenHash && isEmailOtpType(type))) { | ||
| captureMessage( | ||
| 'Email auth confirmation link missing required parameters.', | ||
| { | ||
| level: 'warning', | ||
| tags: { | ||
| area: 'auth', | ||
| flow: 'email-auth-confirm', | ||
| error_type: 'missing-confirmation-params', | ||
| }, | ||
| extra: { | ||
| hasTokenHash: Boolean(tokenHash), | ||
| type, | ||
| redirectTo, | ||
| safeRedirectPath, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| return NextResponse.redirect(`${origin}${loginPath}`); | ||
| } | ||
|
|
||
| const supabase = await createClient(); | ||
| const { data, error } = await supabase.auth.verifyOtp({ | ||
| token_hash: tokenHash, | ||
| type, | ||
| }); | ||
|
gianpaj marked this conversation as resolved.
|
||
|
|
||
| if (error) { | ||
| captureException(error, { | ||
| tags: { | ||
| area: 'auth', | ||
| flow: 'email-auth-confirm', | ||
| error_type: 'verify-otp-failed', | ||
| }, | ||
| extra: { | ||
| type, | ||
| redirectTo, | ||
| safeRedirectPath, | ||
| }, | ||
| }); | ||
|
|
||
| return NextResponse.redirect(`${origin}${loginPath}`); | ||
| } | ||
|
|
||
| if (isSignupEmailOtpType(type)) { | ||
| const user = data.user ?? (await supabase.auth.getUser()).data.user; | ||
|
|
||
| if (user) { | ||
| await recordSignupSideEffects(user, 'email'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎. |
||
| } else { | ||
| captureMessage('Email signup confirmation completed without a user.', { | ||
| level: 'error', | ||
| tags: { | ||
| area: 'auth', | ||
| flow: 'email-auth-confirm', | ||
| error_type: 'missing-confirmed-user', | ||
| }, | ||
| extra: { | ||
| type, | ||
| redirectTo, | ||
| safeRedirectPath, | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return createAuthRedirectResponse( | ||
| `${origin}${safeRedirectPath ?? getDefaultSuccessPath(type, locale)}`, | ||
| ); | ||
| } | ||
|
gianpaj marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.