|
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import { createAdminClient } from '@/lib/database/supabase-admin' |
| 3 | + |
| 4 | +export async function POST(request: Request) { |
| 5 | + try { |
| 6 | + const { email, password } = await request.json() |
| 7 | + |
| 8 | + if (!email || !password) { |
| 9 | + return NextResponse.json({ success: false, error: 'Email and password are required' }, { status: 400 }) |
| 10 | + } |
| 11 | + |
| 12 | + const supabaseAdmin = createAdminClient() |
| 13 | + if (!supabaseAdmin) { |
| 14 | + return NextResponse.json({ success: false, error: 'Server configuration error' }, { status: 500 }) |
| 15 | + } |
| 16 | + |
| 17 | + // Since we need to bypass existing user passwords, we will search for the user by email |
| 18 | + // and forcefully update their password. |
| 19 | + // In @supabase/supabase-js recent versions, generating a link is easy or listing users handles it. |
| 20 | + |
| 21 | + const { data: { users }, error: listError } = await supabaseAdmin.auth.admin.listUsers() |
| 22 | + |
| 23 | + if (listError) { |
| 24 | + return NextResponse.json({ success: false, error: listError.message }, { status: 400 }) |
| 25 | + } |
| 26 | + |
| 27 | + const user = users.find((u: any) => u.email === email) |
| 28 | + |
| 29 | + if (user) { |
| 30 | + // User exists! Force update their password to the hidden password |
| 31 | + const { error: updateError } = await supabaseAdmin.auth.admin.updateUserById( |
| 32 | + user.id, |
| 33 | + { password } |
| 34 | + ) |
| 35 | + |
| 36 | + if (updateError) { |
| 37 | + return NextResponse.json({ success: false, error: updateError.message }, { status: 400 }) |
| 38 | + } |
| 39 | + |
| 40 | + return NextResponse.json({ success: true, message: 'Password synced' }) |
| 41 | + } else { |
| 42 | + // User does not exist, standard signup will handle it in the client |
| 43 | + return NextResponse.json({ success: false, error: 'User not found' }, { status: 404 }) |
| 44 | + } |
| 45 | + |
| 46 | + } catch (error: any) { |
| 47 | + return NextResponse.json({ success: false, error: error.message }, { status: 500 }) |
| 48 | + } |
| 49 | +} |
0 commit comments