How to set up JWT authentication with refresh
-Vue-jwt-auth - Jwt Auth library for Vue.js -Jwt-auth - JSON Web Token Authentication for Laravel & Lumen
Edit config/jwt.php
:
...
'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', false)` in
...
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');
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'));
}
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'));
}
You don't need a method just call the middleware in the route:
Route::get('/sessions/refresh-token', ['middleware' => 'jwt.refresh', function() {}]);