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.
34 lines
954 B
TypeScript
34 lines
954 B
TypeScript
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);
|
|
}
|