doulin6088 2018-11-13 08:25
浏览 135

改进json格式数据中的JWT异常处理

Hi i am currently having a problem about JWT Exception JSON Message. The problem is when i checkIn but my token is expired and i received this

"message": "Token has expired",
    "exception": "Tymon\\JWTAuth\\Exceptions\\TokenExpiredException",
    "file": "C:\\Users\\Snowfox1991\\Desktop\\Capstone project\\back-end\\back-end\\vendor\\tymon\\jwt-auth\\src\\Claims\\Expiration.php",
    "line": 31,

But when i write the getAuthUser() in APIController, i use try catch this one

public function getAuthUser(Request $request)
{
    $this->validate($request, [
        'token' => 'required'
    ]);

    $user = JWTAuth::authenticate($request->token);

    try {

        if (!$user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }
    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        response()->json([
            'status' => 'error',
            'message' => 'Token has expired'
        ], $e->getStatusCode()

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        response()->json([
            'status' => 'error',
            'message' => 'Token is invalid'
        ], $e->getStatusCode()

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

        response()->json([
            'status' => 'error',
            'message' => 'Token is absent'
        ], $e->getStatusCode()

    // }

    return response()->json(['user' => $user]);
}

And it still return the same as the original without the json format. Can i ask how to improve this exception using json format without handling the error like this. Thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 再不同版本的系统上,TCP传输速度不一致
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程