<?php

namespace App\libs\middleware;

use App\Auth;
use App\Models\User;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CheckAuth
{
    /**
     * 处理传入请求。
     *
     * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
     */
    public function handle(Request $request, \Closure $next): Response
    {
        $this->user   = auth('api')->user();
        $this->userId = $this->user ? $this->user->id : 0;
        $user         = User::query()->where('id', $this->userId)->where('is_black', 0)->first();
        if (!$user) {
            abort(401);
        }
        if ($user->is_black) {
            abort(402);
        }
        Auth::$user   = $user;
        Auth::$userId = $user->id;

        return $next($request);
    }
}
