自定义Laravel Passport响应未经身份验证 [英] Customize Laravel Passport response unauthenticated

查看:77
本文介绍了自定义Laravel Passport响应未经身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用Laravel中使用护照功能制作的api具有登录,注册,更新和删除功能.一切正常,可以使用此api很好地插入数据并从数据库中获取数据.现在我想知道,当令牌过期时,如何自定义api的响应.令牌的到期也可以正常工作.它会自动显示此消息

Currently I have a login, register, update and delete functionality using my api made in Laravel using passport feature. Everything works fine the insertion of data and fetching the data from the database using this api. Now I wonder, how can I customize the response of the api when the token is expired. The expiration of token is working fine too. It automatically show this message

{"message":未经身份验证"}

这是路由的代码,其中受我的Oauth令牌保护,如果该用户未先登录,则该用户未通过身份验证来浏览路由

This is the code of routes where it is protected by my Oauth token where if the user did not login first then the user is not authenticated to browse the routes

 Route::middleware('auth:api')->get('/user', function (Request $request){return $request->user();});



Route::post('/timekeeping','Auth\Api\AuthController@timekeeping');

Route::post('/login','Auth\Api\AuthController@login');

 Route::middleware('auth:api')->group(function () {Route::post('/timekeeping_app','Auth\Api\AuthController@timekeeping_app');

Route::post('/logout','Auth\Api\AuthController@logout');

Route::post('/register','Auth\Api\AuthController@register');

Route::post('/show_dtr_list','Auth\Api\AuthController@show_dtr_list');

Route::post('/update','Auth\Api\AuthController@update');

Route::post('/delete','Auth\Api\AuthController@delete');

 });

这就是每当用户成功登录,注册甚至注销其帐户时,我都会做出的回应.

Then this is how I response whenever the user successfully logged in, registered, or even logged out their accounts.

返回响应(['status'=>'oK','message'=>'成功!']);

我想要的是何时用户每次使用过期的令牌时.api应该响应这样的

What I want is when everytime the user is using the expired token. The api should response something like this

{消息":令牌已过期"}

不仅仅是

{"message":"Unententicated"}

一些线程讨论过,我需要覆盖laravel的某些功能,但是我不知道我将从何处开始以及如何开始.

Some threads discussed that I need to overwrite some functionalities of laravel but I don't know where and how am I going to start.

推荐答案

这是我解决问题的方法.如果您使用的是Laravel 5.5或更高版本,则可以通过编辑 app/Exceptions/Handler.php 以添加以下内容来覆盖默认的异常处理程序:

Here's how I solved it. If you are using Laravel 5.5 or above you can override the default exception handler by editing app/Exceptions/Handler.php to add the following:

use Illuminate\Auth\AuthenticationException;

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        $json = [
            'isAuth'=>false,
            'message' => $exception->getMessage()
        ];
        return response()
            ->json($json, 401);
    }
    $guard = array_get($exception->guards(),0);
    switch ($guard) {
        default:
            $login = 'login';
            break;
    }
    return redirect()->guest(route($login));
}

在JSON返回中,您可以根据需要添加任何参数.

In the JSON return, you can add any parameters per your requirement.

这篇关于自定义Laravel Passport响应未经身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆