当我用用户名和密码字段在Postman上测试我的登录功能时,它总是得到错误消息“无效凭据”。
我试过修改$credentials变量定义,并搜索论坛,没有任何效果。我没有修改任何原始文件从tymon/jwt-auth,任何东西像改变电子邮件到用户名。对于JWT的版本,我使用tymon/jwt-auth:“dev-develop”。
这是我的用户控制器. 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);
}
这是我的模型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条答案
按热度按时间djmepvbi1#
用户模型应扩展可验证
iyfjxgzm2#
这对我有用
https://jwt-auth.readthedocs.io/en/develop/quick-start/