laravel passport 全局捕获 401 ,返回数据给前台

Exceptions/handle

protected function unauthenticated($request, AuthenticationException $exception)
    {
        if(!$request->expectsJson()){
            return response()->json([
                'code' => 401,
                'message' => $exception->getMessage(),
                'status' => true,
                'data' => null,
            ], 401);
        }
        // 注释内容为网上示例
        // return $request->expectsJson()
        //             ? response()->json(['message' => $exception->getMessage()], 401)
        //             : redirect()->guest(route('login'));
    }

  

参考链接:https://learnku.com/docs/laravel/5.7/authentication/2269

posted @ 2020-05-21 10:52  winyh  阅读(670)  评论(0编辑  收藏  举报