/** * Alternative registration endpoint using Supabase Auth * This auto-confirms the email to bypass email confirmation requirement * * NOTE: This requires SUPABASE_SERVICE_ROLE_KEY in environment variables * This should only be used if you need Supabase Auth instead of custom users table */ import { NextRequest, NextResponse } from 'next/server' import { createClient } from '@supabase/supabase-js' const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY // Create admin client with service role key const supabaseAdmin = serviceRoleKey && supabaseUrl ? createClient(supabaseUrl, serviceRoleKey, { auth: { autoRefreshToken: false, persistSession: false } }) : null export async function POST(request: NextRequest) { try { if (!supabaseAdmin) { return NextResponse.json( { error: 'Supabase service role key not configured. Use custom authentication instead.' }, { status: 500 } ) } const { email, password, username } = await request.json() if (!email || !password) { return NextResponse.json( { error: 'Email and password are required' }, { status: 400 } ) } // Create user with auto-confirmed email const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({ email: email.trim().toLowerCase(), password, email_confirm: true, // Auto-confirm email to bypass confirmation requirement user_metadata: { username: username || email.split('@')[0] } }) if (authError) { console.error('Supabase auth error:', authError) return NextResponse.json( { error: authError.message }, { status: 400 } ) } if (!authData.user) { return NextResponse.json( { error: 'Failed to create user' }, { status: 500 } ) } return NextResponse.json({ success: true, user: { id: authData.user.id, email: authData.user.email, email_confirmed: true } }, { status: 201 }) } catch (error) { console.error('Registration error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } }