return true instead of JWT token
I successfully created a JWT token in one of the applications in cloud hosting using Tymon/jwt-auth. But, another app that has the same project that is in the same cloud hosting returns true for JWT instead of a token. How can I Fix this ??
Here is my config/app.php.
guard.
'admin' => [
'driver' => 'jwt',
'provider' => 'admins',
],
providers.
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
In config/app.php change 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], to 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],
I have added a new guard and provider to config/app.php like below.
'admin' => [ 'driver' => 'jwt', 'provider' => 'admins', ],
'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ],
But, the ting is it works find in one sever and doesn't work in other sever. Why is that ??
auth('api')->attempt($credentials)
Instead of
auth()->attempt($credentials)
you have to set the guard 'admin' in your case in the auth helper in order to get the correct behavior
Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
maybe you haven't added the following code in the User model
`<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject { use Notifiable;
// Rest omitted for brevity
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}`
In config/app.php change 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], to 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],
It was really helpful, thank you.
In my case, I wanted to keep the default laravel session authentication for the web app and let the mobile app consume jwt auth from the api. My solution is to change the guard whenever I'm trying to authenticate a user via api:
$token = auth()->guard('api')->attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $token;
OR you can pass the guard as a parameter to auth() like this:
$token = auth('api')->attempt($credentials)