Jwt-auth: ErrorException on login attempt

Created on 13 Sep 2015  路  5Comments  路  Source: tymondesigns/jwt-auth

I got an error on login attempt:

"Argument 1 passed to Tymon\JWTAuth\JWTAuth::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in /vendor/tymon/jwt-auth/src/JWTAuth.php on line 84 and defined"

// 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'], 401);
    }
} catch (JWTException $e) {
    // something went wrong whilst attempting to encode the token
    return response()->json(['error' => 'could_not_create_token'], 500);
}

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

I am using the 0.6.*-dev package with Sentinel provider.

Most helpful comment

Well, it works fine now. Thanks!

<?php

namespace App;

use Cartalyst\Sentinel\Users\EloquentUser;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends EloquentUser implements JWTSubject
{
    /**
     * Get the identifier that will be stored in the subject claim of the JWT
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getUserId();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

All 5 comments

The user model needs to implement Tymon\JWTAuth\Contracts\JWTSubject in the new version. This is where you define the subject claim, and any custom claims

Well, it works fine now. Thanks!

<?php

namespace App;

use Cartalyst\Sentinel\Users\EloquentUser;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends EloquentUser implements JWTSubject
{
    /**
     * Get the identifier that will be stored in the subject claim of the JWT
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getUserId();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

:+1:

thanx, man

Thanx, man i solve my problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aofdev picture aofdev  路  3Comments

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments

lloy0076 picture lloy0076  路  3Comments

CBR09 picture CBR09  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments