Jwt-auth: JWTAuth::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject

Created on 28 Sep 2015  路  6Comments  路  Source: tymondesigns/jwt-auth

Hi,

I'm currently testing out the develop branch, as it was referenced in a ticked #127

When hitting the attempt method (through the Facade) with basic credentials, I'm getting an ErrorException thrown:

exception 'ErrorException' with message 'Argument 1 passed to Tymon\JWTAuth\JWTAuth::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in /Users/koen/Desktop/starter/vue-starter-laravel-api/vendor/tymon/jwt-auth/src/JWTAuth.php on line 84 and defined' in /Users/koen/Desktop/starter/vue-starter-laravel-api/vendor/tymon/jwt-auth/src/JWTAuth.php:64
Stack trace:
#0 /Users/koen/Desktop/starter/vue-starter-laravel-api/vendor/tymon/jwt-auth/src/JWTAuth.php(64): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(4096, 'Argument 1 pass...', '/Users/koen/Des...', 64, Array)
#1 /Users/koen/Desktop/starter/vue-starter-laravel-api/vendor/tymon/jwt-auth/src/JWTAuth.php(84): Tymon\JWTAuth\JWTAuth->fromUser(Object(App\User))
#2 /Users/koen/Desktop/starter/vue-starter-laravel-api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(210): Tymon\JWTAuth\JWTAuth->attempt(Array)
#3 /Users/koen/Desktop/starter/vue-starter-laravel-api/app/Api/Controllers/AuthController.php(26): Illuminate\Support\Facades\Facade::__callStatic('attempt', Array)
#4 /Users/koen/Desktop/starter/vue-starter-laravel-api/app/Api/Controllers/AuthController.php(26): Tymon\JWTAuth\Facades\JWTAuth::attempt(Array)
#5 [internal function]: Api\Controllers\AuthController->authenticate(Object(Dingo\Api\Http\Request))

Am I doing something wrong or is the current state of the develop branch broken?

Cheers,
K

Most helpful comment

For a high-level summary of claims, checkout the Wikipedia and MSDN articles on claims-based security.

Claims are simply properties of a key/user communicated from one system to another. For the purposes of JWT and this library, we tend to only care about a few of the core claims identified in the JWT spec:

  • iss (issuer) The system asserting these claims. For us, the URL of the endpoint that created the token.
  • sub (subject) Who the claims are about -- effectively the user. The spec says this has to be unique (locally, if not also globally), so the standard choice is the user's PK.
  • iat (issued at), exp (expiration), & nbf (not before) Timestamps identifying when the token was created and when it's valid.
  • jti (JWT ID) An identifier for the token itself, ideally one that will be unique. This is how we reference specific tokens (e.g. for blacklisting).

The "identifier" in getJWTIdentifier() refers to the value of the subject claim (it's a method on the JWTSubject interface). As noted above, the normal choice would be the primary key of the user, but part of the reason for the switch to the new interface is to aid in flexibility there. My current implementation is super simple:

public function getJWTIdentifier()
{
    return $this->getKey(); \\ Eloquent Model method
}

public function getJWTCustomClaims()
{
    return [];
}

All 6 comments

The new version requires you to implement Tymon\JWTAuth\Contracts\JWTSubject (here) on your user model. You must then add the required methods, which are getJWTIdentifier() and getJWTCustomClaims()

aha, ok, makes sense, I'll try that!

What actually goes into those methods? I'm unsure what is meant by 'Claims' or 'Identifier'. Are claims essentialy relationships? Is the identifier an Eloquent of the user, the user's PK, or some custom user attribute?

For a high-level summary of claims, checkout the Wikipedia and MSDN articles on claims-based security.

Claims are simply properties of a key/user communicated from one system to another. For the purposes of JWT and this library, we tend to only care about a few of the core claims identified in the JWT spec:

  • iss (issuer) The system asserting these claims. For us, the URL of the endpoint that created the token.
  • sub (subject) Who the claims are about -- effectively the user. The spec says this has to be unique (locally, if not also globally), so the standard choice is the user's PK.
  • iat (issued at), exp (expiration), & nbf (not before) Timestamps identifying when the token was created and when it's valid.
  • jti (JWT ID) An identifier for the token itself, ideally one that will be unique. This is how we reference specific tokens (e.g. for blacklisting).

The "identifier" in getJWTIdentifier() refers to the value of the subject claim (it's a method on the JWTSubject interface). As noted above, the normal choice would be the primary key of the user, but part of the reason for the switch to the new interface is to aid in flexibility there. My current implementation is super simple:

public function getJWTIdentifier()
{
    return $this->getKey(); \\ Eloquent Model method
}

public function getJWTCustomClaims()
{
    return [];
}

i am try to get token using only Email but i am get error this one -> Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\ContractsJWTSubject, instance of App\Model\User given, called in C:\wamp\www\users-project\vendor\illuminate\support\Facades\Facade.php on line 217 and defined

i am using this code to Get token ->
$user=User::where('email','=','[email protected]')->first();

if (!$userToken=JWTAuth::fromUser($user)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}

return response()->json(compact('userToken'));

Please help me how i get token using only rmail address

Update the flippin docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lloy0076 picture lloy0076  路  3Comments

kofi1995 picture kofi1995  路  3Comments

gandra picture gandra  路  3Comments

johncloud200 picture johncloud200  路  3Comments

hfalucas picture hfalucas  路  3Comments