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
The User model should extends Authenticatable
that works me
https://jwt-auth.readthedocs.io/en/develop/quick-start/