<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AjustadorAuth
{
    public function handle(Request $request, Closure $next): Response
    {
        if (! Auth::guard('ajustador')->check()) {
            if ($request->expectsJson()) {
                return response()->json(['message' => 'Unauthenticated.'], 401);
            }

            return redirect()->route('ajustador.login');
        }

        return $next($request);
    }
}
