jbehrens94
jbehrens94

Reputation: 2406

Laravel with JWT tokens returns a too long refreshed token

Currently, I have this code.

/**
     * Refresh a token.
     *
     * @param $token
     * @return \Illuminate\Http\JsonResponse
     */
    public function token($token)
    {
        if(!$token)
        {
            throw new BadRequestHttpException('Geen token aangeleverd');
        }

        try {
            $newToken = \JWTAuth::refresh($token);
        } catch(TokenInvalidException $e) {
            throw new AccessDeniedHttpException('Incorrect token');
        }

        return \Response::json(compact('newToken'));
    }

When I send a valid token to this method, I receive an extremely long token back from the JWTAuth::refresh call. It looks like the new token is appended to my existing token.

I'm not too sure what I am doing wrong, so please fire at will. ;)

Upvotes: 1

Views: 613

Answers (1)

Andrii Lutskevych
Andrii Lutskevych

Reputation: 1389

We have next method for refresh token. Let JWTAuth itself find token.

public function tokenRefresh(Request $request)
{
    try {
        $newToken = JWTAuth::setRequest($request)->parseToken()->refresh();
    } catch (TokenExpiredException $e) {
        event("jwt.expired", [$e], true);
    } catch (JWTException $e) {
        event("jwt.invalid", [$e], true);
    }

    /** Our Response wrapper :) */
    return ApiResponse::success([
        'token' => $newToken,
    ]);
}

Upvotes: 1

Related Questions