Jwt-auth: JWT Auth doesn't work

Created on 26 May 2016  路  2Comments  路  Source: tymondesigns/jwt-auth

Hello guys I'am new to laravel and try to get this boilerplate up and running. I have setup the database and seeded a testuser.

This is my User class:

<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    /**
     * This mutator automatically hashes the password.
     *
     * @var string
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }
}麓

And the important part of the AuthController

 public function login(Request $request)
    {
        $credentials = $request->only(['username', 'password']);

        $validator = Validator::make($credentials, [
            'username' => 'required',
            'password' => 'required',
        ]);

        if($validator->fails()) {
            throw new ValidationHttpException($validator->errors()->all());
        }

        try {
            if (! $token = JWTAuth::attempt($credentials)) {
                return $this->response->errorUnauthorized();
            }
        } catch (JWTException $e) {
            return $this->response->error('could_not_create_token', 500);
        }

        return response()->json(compact('token'));
    }

I get {"message":"Unauthorized","status_code":401} when try to login with my testuser and don't have any idea whats going wrong. Haven't changed anything of the default configuration despite setting the auth fields to "username" and "password" in the User class and login function.

Any help is much appreciated thank you!

Most helpful comment

Hello I made a terrible mistake when seeding my testuser:

class UserTableSeeder extends Seeder {

    public function run() {
        User::truncate();

        User::create( [
            'username' => 'Developer',
            'password' => \Hash::make('Developer'),
        ] );
    }
}

And in my User class I had

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }

So the password got double hashed. It's working now. Thank you!

All 2 comments

Hrm I don't see anything obviously wrong, but that error looks like Dingo's errorUnauthorized that you're calling in AuthController right after this line:

if (! $token = JWTAuth::attempt($credentials)) {

If attempt is returning false, it's probably something to do with your credentials process. Try using Laravel's standard login functions and see what these return: (put them above the try-catch)

var_dump(Auth::once($credentials));
var_dump(Auth::user());
var_dump(User::first());

Ideally that would return true and two copies of your test user.

Hello I made a terrible mistake when seeding my testuser:

class UserTableSeeder extends Seeder {

    public function run() {
        User::truncate();

        User::create( [
            'username' => 'Developer',
            'password' => \Hash::make('Developer'),
        ] );
    }
}

And in my User class I had

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }

So the password got double hashed. It's working now. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phamduong picture phamduong  路  3Comments

functionpointdaniel picture functionpointdaniel  路  3Comments

lloy0076 picture lloy0076  路  3Comments

mihailo-misic picture mihailo-misic  路  3Comments

shah-newaz picture shah-newaz  路  3Comments