Create auth confirmation route
Added a new route for authentication confirmation that uses OTP verification. If successful, it redirects the user to the specified next path; otherwise, it navigates them to an error page. This is part of implementing more robust user authentication.
This commit is contained in:
33
apps/web/app/auth/confirm/route.ts
Normal file
33
apps/web/app/auth/confirm/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { type EmailOtpType } from '@supabase/supabase-js';
|
||||
|
||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const token_hash = searchParams.get('token_hash');
|
||||
const type = searchParams.get('type') as EmailOtpType | null;
|
||||
const next = searchParams.get('next') ?? '/';
|
||||
const redirectTo = request.nextUrl.clone();
|
||||
|
||||
redirectTo.pathname = next;
|
||||
|
||||
if (token_hash && type) {
|
||||
const supabase = getSupabaseRouteHandlerClient();
|
||||
|
||||
const { error } = await supabase.auth.verifyOtp({
|
||||
type,
|
||||
token_hash,
|
||||
});
|
||||
|
||||
if (!error) {
|
||||
return NextResponse.redirect(redirectTo);
|
||||
}
|
||||
}
|
||||
|
||||
// return the user to an error page with some instructions
|
||||
redirectTo.pathname = '/auth/callback/error';
|
||||
|
||||
return NextResponse.redirect(redirectTo);
|
||||
}
|
||||
Reference in New Issue
Block a user