Fix email unconfirmed login flow with OTP resend and update email API field names

This commit is contained in:
dwindown
2026-01-02 19:33:51 +07:00
parent 8f46c5cfd9
commit eee6339074
13 changed files with 668 additions and 65 deletions

View File

@@ -0,0 +1,97 @@
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};
interface GetUserRequest {
email: string;
}
serve(async (req: Request) => {
if (req.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
try {
const { email }: GetUserRequest = await req.json();
// Validate required fields
if (!email) {
return new Response(
JSON.stringify({ success: false, message: "Missing required field: email" }),
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
return new Response(
JSON.stringify({ success: false, message: "Invalid email format" }),
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
// Initialize Supabase client with service role
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseServiceKey, {
auth: {
autoRefreshToken: false,
persistSession: false
}
});
console.log(`Looking up user with email: ${email}`);
// Get user by email from auth.users
const { data: { users }, error } = await supabase.auth.admin.listUsers();
if (error) {
console.error('Error listing users:', error);
throw new Error(`Failed to lookup user: ${error.message}`);
}
// Find user with matching email
const user = users?.find(u => u.email === email);
if (!user) {
console.log('User not found:', email);
return new Response(
JSON.stringify({
success: false,
message: "User not found",
user_id: null
}),
{ status: 404, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
console.log('User found:', { id: user.id, email: user.email, emailConfirmed: user.email_confirmed_at });
return new Response(
JSON.stringify({
success: true,
user_id: user.id,
email_confirmed: !!user.email_confirmed_at,
created_at: user.created_at
}),
{ status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
} catch (error: any) {
console.error("Error getting user by email:", error);
return new Response(
JSON.stringify({
success: false,
message: error.message || "Failed to lookup user",
user_id: null
}),
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
});