<?php

namespace App\Http\Middleware;

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

class EnsurePasswordChanged
{
    public function handle(Request $request, Closure $next): Response
    {
        if (
            auth('web')->check() &&
            auth('web')->user()->force_password_change &&
            !$request->routeIs('password.change') &&
            !$request->routeIs('password.update') &&
            !$request->routeIs('logout')
        ) {
            return redirect()->route('password.change')
                ->with('info', 'You must change your password before continuing.');
        }

        return $next($request);
    }
}
