mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-25 07:31:13 -05:00
Refactor: Implement full authentication overhaul
This commit is contained in:
@@ -5,6 +5,8 @@ import { useToast } from '@/hooks/use-toast';
|
||||
import { Header } from '@/components/layout/Header';
|
||||
import { MFAChallenge } from '@/components/auth/MFAChallenge';
|
||||
import { Shield } from 'lucide-react';
|
||||
import { getStepUpRequired, getIntendedPath, clearStepUpFlags } from '@/lib/sessionFlags';
|
||||
import { getEnrolledFactors } from '@/lib/authService';
|
||||
|
||||
export default function MFAStepUp() {
|
||||
const navigate = useNavigate();
|
||||
@@ -14,30 +16,28 @@ export default function MFAStepUp() {
|
||||
useEffect(() => {
|
||||
const checkStepUpRequired = async () => {
|
||||
// Check if this page was accessed via proper flow
|
||||
const needsStepUp = sessionStorage.getItem('mfa_step_up_required');
|
||||
if (!needsStepUp) {
|
||||
if (!getStepUpRequired()) {
|
||||
console.log('[MFAStepUp] No step-up flag found, redirecting to auth');
|
||||
navigate('/auth');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the verified TOTP factor
|
||||
const { data: factors } = await supabase.auth.mfa.listFactors();
|
||||
const totpFactor = factors?.totp?.find(f => f.status === 'verified');
|
||||
// Get enrolled MFA factors
|
||||
const factors = await getEnrolledFactors();
|
||||
|
||||
if (!totpFactor) {
|
||||
if (factors.length === 0) {
|
||||
console.log('[MFAStepUp] No verified TOTP factor found');
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'MFA not enrolled',
|
||||
description: 'Please enroll in two-factor authentication first.',
|
||||
});
|
||||
sessionStorage.removeItem('mfa_step_up_required');
|
||||
clearStepUpFlags();
|
||||
navigate('/settings?tab=security');
|
||||
return;
|
||||
}
|
||||
|
||||
setFactorId(totpFactor.id);
|
||||
setFactorId(factors[0].id);
|
||||
};
|
||||
|
||||
checkStepUpRequired();
|
||||
@@ -46,33 +46,26 @@ export default function MFAStepUp() {
|
||||
const handleSuccess = async () => {
|
||||
console.log('[MFAStepUp] MFA verification successful');
|
||||
|
||||
// Clear the step-up flag
|
||||
sessionStorage.removeItem('mfa_step_up_required');
|
||||
|
||||
toast({
|
||||
title: 'Verification successful',
|
||||
description: 'You now have full access to all features.',
|
||||
});
|
||||
|
||||
// Redirect to home or intended destination
|
||||
const intendedPath = sessionStorage.getItem('mfa_intended_path') || '/';
|
||||
sessionStorage.removeItem('mfa_intended_path');
|
||||
const intendedPath = getIntendedPath();
|
||||
clearStepUpFlags();
|
||||
navigate(intendedPath);
|
||||
};
|
||||
|
||||
const handleCancel = async () => {
|
||||
console.log('[MFAStepUp] MFA verification cancelled');
|
||||
|
||||
// Clear flags
|
||||
sessionStorage.removeItem('mfa_step_up_required');
|
||||
sessionStorage.removeItem('mfa_intended_path');
|
||||
|
||||
// Sign out for security
|
||||
await supabase.auth.signOut();
|
||||
// Clear flags and redirect to sign-in (less harsh than forcing sign-out)
|
||||
clearStepUpFlags();
|
||||
|
||||
toast({
|
||||
title: 'Verification cancelled',
|
||||
description: 'You have been signed out.',
|
||||
description: 'Please sign in again to continue.',
|
||||
});
|
||||
|
||||
navigate('/auth');
|
||||
|
||||
Reference in New Issue
Block a user