Laravel JWT auth get User ID when refreshing token

  authentication, jwt, laravel, php

I’m using the tymon jwt-auth package and am utilising refresh tokens. My database has a column for when a user last logged in, which is set upon log in to my app, but I need to update this column when the user’s token is refreshed.

I’m having some trouble getting the id of the user, so that I know to update their record, Auth::id() is null, and thus never runs.

Even if I run this after doing a refresh. How can I obtain the user to update their last logged in column?

/**
 * Refresh a token.
 *
 * @return IlluminateHttpJsonResponse
 */
public function refresh()
{
    try {
        if (Auth::id()) {
            ProcessLastUserLogin::dispatch(
                Auth::id(), Carbon::now()
            );
        } else {
            Log::debug('unable to obtain user ID');
        }
    } catch (Exception $e) { }

    // return refresh token
    return $this->respondWithToken(auth()->refresh(), true);
}

Source: Laravel

Leave a Reply