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:
giancarlo
2024-04-19 16:00:40 +08:00
parent 667f9cbb57
commit 92f0ba4dd5

View 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);
}