自定义LALAVELE圣殿未经授权的响应 [英] Customize laravel sanctum unauthorize response

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

问题描述

我在我的项目中使用了幼体圣殿,但我面临着一个问题。我想自定义401响应代码(未经授权),以便在令牌无效时返回JSON,如下所示:

    {
    "data": {
        "code": 401,
        "book": null,
        "success": false,
        "error": {
            "message": "Not authenticated"
        }
    }
}

代替默认响应:

{
    "message": "Unauthenticated."
}

如何在幼体圣所做到这一点?提前谢谢。

推荐答案

Rendering exceptions

添加到ExceptionHandler@registerapp/Exceptions/ExceptionHandler.php

$this->renderable(function (IlluminateAuthAuthenticationException $e, $request) {
    if ($request->is('api/*')) {
        return response()->json([
            'message' => 'Not authenticated'
        ], 401);
    }
});

这篇关于自定义LALAVELE圣殿未经授权的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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