<?php

namespace App\Http\Middleware;

use Closure;

class authJWT
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        try {
            // 如果用户登陆后的所有请求没有jwt的token抛出异常
            $user = \App\Vendor\Jwt::verifyToken($request->input('token')); 
            if(!$user){
                return toJson(-30001);
            }
           
            S('token_user_id',$user['sub']);

        } catch (Exception $e) {
            return toJson(-30001);
            // if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
            //     return response()->json(['error'=>'Token 无效']);
            // }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
            //     return response()->json(['error'=>'Token 已过期']);
            // }else{
            //     return response()->json(['error'=>'出错了']);
            // }
        }
        return $next($request);
    }
}
