<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Validation\UnauthorizedException;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        /** @var CheckUserStatus $user */
        $user = $request->user();
        if (!$user || !$user->canAccessAdmin()) {
            do_log("denied!");
            throw new UnauthorizedException('Unauthorized!');
        }
        do_log("allow!");
        return $next($request);
    }
}
