mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 13:51:13 -05:00
Fix MFA verification error
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState } from 'react';
|
||||||
import { supabase } from '@/integrations/supabase/client';
|
import { supabase } from '@/integrations/supabase/client';
|
||||||
import { useToast } from '@/hooks/use-toast';
|
import { useToast } from '@/hooks/use-toast';
|
||||||
import { getErrorMessage } from '@/lib/errorHandler';
|
import { getErrorMessage } from '@/lib/errorHandler';
|
||||||
@@ -17,37 +17,23 @@ export function MFAChallenge({ factorId, onSuccess, onCancel }: MFAChallengeProp
|
|||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const [code, setCode] = useState('');
|
const [code, setCode] = useState('');
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [challengeId, setChallengeId] = useState<string | null>(null);
|
|
||||||
|
|
||||||
// Create MFA challenge on mount
|
|
||||||
useEffect(() => {
|
|
||||||
const createChallenge = async () => {
|
|
||||||
try {
|
|
||||||
const { data, error } = await supabase.auth.mfa.challenge({ factorId });
|
|
||||||
if (error) throw error;
|
|
||||||
setChallengeId(data.id);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
toast({
|
|
||||||
variant: "destructive",
|
|
||||||
title: "MFA Challenge Failed",
|
|
||||||
description: getErrorMessage(error)
|
|
||||||
});
|
|
||||||
onCancel();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
createChallenge();
|
|
||||||
}, [factorId, onCancel, toast]);
|
|
||||||
|
|
||||||
const handleVerify = async () => {
|
const handleVerify = async () => {
|
||||||
if (code.length !== 6 || !challengeId) return;
|
if (code.length !== 6) return;
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
|
// Create fresh challenge for each verification attempt
|
||||||
|
const { data: challengeData, error: challengeError } =
|
||||||
|
await supabase.auth.mfa.challenge({ factorId });
|
||||||
|
|
||||||
|
if (challengeError) throw challengeError;
|
||||||
|
|
||||||
|
// Immediately verify with fresh challenge
|
||||||
const { data, error } = await supabase.auth.mfa.verify({
|
const { data, error } = await supabase.auth.mfa.verify({
|
||||||
factorId,
|
factorId,
|
||||||
challengeId,
|
challengeId: challengeData.id,
|
||||||
code
|
code: code.trim()
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user