I'm creating a SPA using Laravel 5.8 and VUE and I'm using JWT to Authenticate by a token.
The authentications work but it returns true as the value instead of the token value. I think to have corrupted my config and I'm not able to fix it again.
My login method is:
$credentials = request(['email', 'password']);
if (Auth::attempt($credentials)) {
if (! $token = auth()->attempt($credentials, false)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $token;
}
The result is "true" but I need the JWT value.
Some few steps back:
I'm using: tymon/jwt-auth
the Auth:
function auth($guard = null)
{
if (is_null($guard)) {
return app(AuthFactory::class);
}
return app(AuthFactory::class)->guard($guard);
}
the config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'users' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
Any suggetsion to check my config?
You should use the JWTAuth facade provided by the package:
if (! $token = \JWTAuth::attempt($credentials)) {
return Response::json(['error' => 'Unauthorized'], 401);
}
you can use guard('api') or guard('users') method, since you have multiple guards
$credentials = request(['email', 'password']);
if (!$token = auth()->guard('api')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $token;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With