Jwt-auth: Always getting invalid_credentials

Created on 21 Dec 2016  路  8Comments  路  Source: tymondesigns/jwt-auth

I checked this issue but did not work: https://github.com/tymondesigns/jwt-auth/issues/341

//seeder file

$faker = Faker\Factory::create();
        $index =1;
        foreach(range(1, 1000) as $index)
        {
            \DB::table('users')->insert([
                'email' => 'admin'.$index++.'@admin.com', 
                'password' => 'password', 
                'created_at' => date('Y-m-d H:i:s')  
            ]);
        }

// Controller

use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;

class AuthController extends Controller 
{
    use Helpers;

    public function login(Request $request)
    {
        try {
            if (!$token = \JWTAuth::attempt($request->only('email', 'password'))) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
        }
        return response()->json(compact('token'));
    }
}

// .env
API_PREFIX=api
API_STANDARDS_TREE=vnd
API_VERSION=v1
API_CONDITIONAL_REQUEST=false
API_STRICT=false
API_DEBUG=true

//model

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $primaryKey = 'id';
    protected $table="users";
    protected $fillable = [
       'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

// composer

"require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*", 
         "tymon/jwt-auth": "0.5.*",
         "dingo/api": "1.0.*@dev"
    },

// route

use Illuminate\Http\Request;
$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function ($api) {
    $api->group(['namespace' => 'App\Http\Controllers'], function ($api) {
        $api->post('/auth/login', 'AuthController@login');
    });
});

//output

{
  "error": "invalid_credentials"
}

How can i solve this problem?
Thanks

Most helpful comment

Solution...

If your code is correct, then also if your getting output:
{
"error": "invalid_credentials"
}

Just Follow this Steps:---------------

first step check:

dd($request->only('email', 'password')); or dd($credentials);
// output should be like this..

array:2 [
"email" => "[email protected]"
"password" => "test123"
]

second step check:

dd($token);
// output should be:

false

Last Step Goto: AppConfigauth.php

On line number: 70 Change Model Location where your saved model(User)
for example: AppModelMyuserUser

and

On line number: 71 Change Table Name to what you have set in your model(User)
for example: protected $table = 'my_user';

_'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsMyuserUser::class,
'table' => 'my_user'
],_

Happy to help you....

All 8 comments

i have the same problem , Do you solve it @shurjomoy

@engsamar
Unfortunately i could not figure out why JWTAuth::attempt() not working.
However I manually Hash::check() the credential.
and generate token using JWTAuth::fromUser() method.

Solution...

If your code is correct, then also if your getting output:
{
"error": "invalid_credentials"
}

Just Follow this Steps:---------------

first step check:

dd($request->only('email', 'password')); or dd($credentials);
// output should be like this..

array:2 [
"email" => "[email protected]"
"password" => "test123"
]

second step check:

dd($token);
// output should be:

false

Last Step Goto: AppConfigauth.php

On line number: 70 Change Model Location where your saved model(User)
for example: AppModelMyuserUser

and

On line number: 71 Change Table Name to what you have set in your model(User)
for example: protected $table = 'my_user';

_'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsMyuserUser::class,
'table' => 'my_user'
],_

Happy to help you....

EDIT: sorry wrong password :D of course invalid_credentials :D

Hi i followed your steps
1) is correct
2) is correct = false
3)
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppUser::class,
'table' => 'users',
],

and still invalid_credentials :(

sorry

I have the same problem

I have the same problem

I have the same problem

Any fix on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heroghost picture heroghost  路  3Comments

aofdev picture aofdev  路  3Comments

kofi1995 picture kofi1995  路  3Comments

lbottoni picture lbottoni  路  3Comments

marciomansur picture marciomansur  路  3Comments