简体   繁体   English

Laravel 8 不记名令牌未找到时的不记名令牌异常处理

[英]Laravel 8 bearer token exception handling if bearer token not found

Hello I can't understand how to set error message when user put some wrong bearer token currently I am not getting any exceptional error message if I try wrong bearer token in postman.你好,我不明白当用户当前输入错误的不记名令牌时如何设置错误消息如果我在 postman 中尝试错误的不记名令牌,我不会收到任何异常错误消息。

public function general_information(Request $request) {
    try {
        return $token = $request->bearerToken();
    } catch(Exception $e) {
        return $e->message();
    }
}

in app/Exceptions/Handler.php add this function and change the message as you wantapp/Exceptions/Handler.php添加此 function 并根据需要更改消息

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'haha'], 401);
    }

    return redirect()->guest('login');
}

here return haha message这里回哈哈消息

note define this before write the function注意在写 function 之前定义这个
Illuminate\Auth\AuthenticationException;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM