skip to Main Content

When I tested my login function on Postman with username and password field, it always got error message "invalid credentials".

I’ve tried modify $credentials variable definition, and search through forums, nothing works for me. I didn’t modify any original file from tymon/jwt-auth, anything like change email to username. For the version of JWT, I use tymon/jwt-auth:"dev-develop".

Here’s my UserController.php

public function login(Request $request)
    {
        // I changed variable definition from $request->only('email', 'password') to $request->only('username', 'password')
        $credentials = $request->only('username', 'password');

        // variable below have the same results
        // $credentials = ['username' => $request->input('username'), 'password' => $request->input('password')];


        try {
            if(! $token = JWTAuth::attempt($credentials)) {
                // Keep getting this error
                return response()->json(['error' => 'invalid credentials'], 400);
            }
        }
        catch (JWTException $e) {
            return response()->json(['error' => 'could not create token'], 500);
        }

        return  $this->createNewToken($token);
    }

And here’s my model User.php

class User extends Model
{
    use HasFactory;
    protected $table='users';
    protected $primaryKey='id_user';
    public $timestamps=false;
    /**
     * fillable
     * 
     * @var array
     */
    protected $fillable = [
        'nama_user', 'role', 'username', 'password'
    ];
    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }

2

Answers


  1. The User model should extends Authenticatable

    use TymonJWTAuthContractsJWTSubject;
    use IlluminateNotificationsNotifiable;
    use IlluminateFoundationAuthUser as Authenticatable;
    
    class User extends Authenticatable implements JWTSubject
    {
        use Notifiable;
    
        // Rest omitted for brevity
    
        /**
         * Get the identifier that will be stored in the subject claim of the JWT.
         *
         * @return mixed
         */
        public function getJWTIdentifier()
        {
            return $this->getKey();
        }
    
        /**
         * Return a key value array, containing any custom claims to be added to the JWT.
         *
         * @return array
         */
        public function getJWTCustomClaims()
        {
            return [];
        }
    }
    
    Login or Signup to reply.
  2. that works me

    https://jwt-auth.readthedocs.io/en/develop/quick-start/

     public function login()
        {
            $credentials = request(['email', 'password']);
    
            if (! $token = auth()->attempt($credentials)) {
                return response()->json(['error' => 'Unauthorized'], 401);
            }
    
            return $this->respondWithToken($token);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search