<?php

namespace App\Http\Middleware;

use Closure;

use Illuminate\Support\Facades\Cache;

class CheckManageToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $token = $request->header("Authorization");
        if($token) {
            $token = trim($token);
            $token = str_replace("Bearer ", "", $token);
            if(Cache::has($token)) {
                return $next($request);
            } else {
                return Response()->json([
                    "status" => -1,
                    "error" => 403,
                    "message" => "请先登陆"
                ]);
            }
        } else {
            return Response()->json([
                "status" => -1,
                "error" => 403,
                "message" => "请先登陆"
            ]);
        }
    }
}
