<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureMfaVerified
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();
        if ($user && $user->mfa_enabled_at) {
            if (!$request->session()->get('mfa_passed')) {
                return redirect()->route('admin.mfa.challenge');
            }
        }
        return $next($request);
    }
}
