JWT with Laravel and VueJS

How to set up JWT authentication with refresh

Requirements

-Vue-jwt-auth - Jwt Auth library for Vue.js -Jwt-auth - JSON Web Token Authentication for Laravel & Lumen

Laravel

Edit config/jwt.php:

...
'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', false)` in 
...

Routes

Route::post('/auth/login', 'Cms\AuthenticateController@authenticate');
Route::get('/sessions/refresh-token', ['middleware' => 'jwt.refresh', function() {}]);
Route::get('/api/me', 'Cms\AuthenticateController@me')->name('me');

Controller

Login

public function authenticate(Request $request)
{
    // grab credentials from the request
    $credentials = $request->only('email', 'password');

    try {
        // attempt to verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials','message'=>"Invalid credentials"], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token', 'message'=>"Server token error"], 500);
    }

    // all good so return the token
    return response()->json(compact('token'));
}

Register

    public function register(UserRequest $request)
{
    $newUser = [
        'name' => $request->get('name'),
        'email' => $request->get('email'),
        'password' => bcrypt($request->get('password')),
    ];
    try {
        $user = User::create($newUser);
    } catch (Exception $e) {
        return Response::json(['error' => 'User already exists.'], HttpResponse::HTTP_CONFLICT);
    }

    $token = JWTAuth::fromUser($user);

    return response()->json(compact('token'));
}

Refreshing token

You don't need a method just call the middleware in the route:

Route::get('/sessions/refresh-token', ['middleware' => 'jwt.refresh', function() {}]);