2025-05-30 16:50:18 -05:00

34 lines
1.1 KiB
TypeScript

'use server';
import 'server-only';
import { createServerClient } from '@/utils/supabase';
import { type EmailOtpType } from '@supabase/supabase-js';
import { type NextRequest } from 'next/server';
import { redirect } from 'next/navigation';
export const GET = async (request: NextRequest) => {
const { searchParams } = new URL(request.url);
const token_hash = searchParams.get('token');
const type = searchParams.get('type') as EmailOtpType | null;
const redirectTo = searchParams.get('redirect_to') ?? '/';
if (token_hash && type) {
const supabase = await createServerClient();
const { error } = await supabase.auth.verifyOtp({
type,
token_hash,
});
if (!error) {
if (type === 'signup' || type === 'magiclink' || type === 'email')
return redirect('/');
if (type === 'recovery' || type === 'email_change')
return redirect('/profile');
if (type === 'invite')
return redirect('/sign-up');
else return redirect(`/?Could not identify type ${type as string}`)
}
else return redirect(`/?${error.message}`);
}
return redirect('/');
};