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

@@ -12,6 +12,7 @@ interface AuthContextType {
signOut: () => Promise<void>;
sendAuthOTP: (userId: string, email: string) => Promise<{ success: boolean; message: string }>;
verifyAuthOTP: (userId: string, otpCode: string) => Promise<{ success: boolean; message: string }>;
getUserByEmail: (email: string) => Promise<{ success: boolean; user_id?: string; email_confirmed?: boolean; message?: string }>;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
@@ -173,8 +174,50 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}
};
const getUserByEmail = async (email: string) => {
try {
const { data: { session } } = await supabase.auth.getSession();
const token = session?.access_token || import.meta.env.VITE_SUPABASE_ANON_KEY;
console.log('Getting user by email:', email);
const response = await fetch(
`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/get-user-by-email`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ email }),
}
);
console.log('Get user response status:', response.status);
if (!response.ok) {
const errorText = await response.text();
console.error('Get user request failed:', response.status, errorText);
return {
success: false,
message: `HTTP ${response.status}: ${errorText}`
};
}
const result = await response.json();
console.log('Get user result:', result);
return result;
} catch (error: any) {
console.error('Error getting user by email:', error);
return {
success: false,
message: error.message || 'Failed to lookup user'
};
}
};
return (
<AuthContext.Provider value={{ user, session, loading, isAdmin, signIn, signUp, signOut, sendAuthOTP, verifyAuthOTP }}>
<AuthContext.Provider value={{ user, session, loading, isAdmin, signIn, signUp, signOut, sendAuthOTP, verifyAuthOTP, getUserByEmail }}>
{children}
</AuthContext.Provider>
);