jwt-auth: Problem with authentication

Hi, I have a problem using following code:

   public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');     

    try {
        // 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
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // if no errors are encountered we can return a JWT
    return response()->json(compact('token'));
}

Im sending the login data via POST. The user account exists in the database. I can echo the login data in the function. For any reason I dont know, I can not authenticate, it always throws 401. Can you please provide me a solution for that? Or any workaround?

Im using laravel 5.1., jwt-auth 0.5. and Apache 2.2.22 (Debian)

Best regards, Oliver

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (2 by maintainers)

Most upvoted comments

After spending hours I have fixed this by defining primary key in the User model class as

protected $primaryKey = ‘user_id’;

My User table primary key was different than just defining “id”.

Hey guys, thanks for the help. What you mentioned brought me on the right track. Im building a new authentication from scratch without migrations of any existing users. So what I did for testing was to create users with non hashed passwords in the database, e.g. username: test, password: 123 It seems like the jwt authentication is just working with the laravel Hash::make function since it hashes the POST password to compare with the password from database. So I had to create a user with a laravel hashed password which worked out perfectly fine. Thanks!

I have an issue about changing $primaryKey. After changing primaryKey in User Model to 'user_id, JWT creates a token but cannot authenticate the user as returning ‘user_not_found’. When I look up the token, sub value is just my user’s id, not user’s user_id. Then I changed the jwt-auth\src\config\config.php file identifier from id to user_id. However still in the token, sub is my id column. Why is it not working? User Model: protected $primaryKey = 'user_id';

public $incrementing = false;

Config.php: 'identifier' => 'user_id',

Thanks for your prompt reponse @OFranke I actually used bcrypt for the password and Config::set(‘auth.model’, ‘App\Vendor’); Config::set(‘auth.table’, ‘vendors’); sets the model and table. I’ve checked all I need to do but all it’s still not authenticating.

I had a similar issue when migrating an existing database scheme to use this library. My issue was related to the database scheme differences on the User table. Laravel was Hashing the passwords differently with a longer lenth than my database column was able to hold.

I imagine you have a similar issue that when retrieving from the database it’s blowing up.

Here’s a couple of things to try:

  1. Inside of app/Http/Kernel.php make sure \App\Http\Middleware\VerifyCsrfToken::class is commented out.
  2. Inside config/jwt.php make sure you have the identifier set to the primary key of your Users table.
'identifier' => 'userid' // default is set to 'id'