有什么方法可以在laravel jwt-auth中使用用户名(或任何自定义列)而不是电子邮件使用身份验证?
这是现有的代码
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
我想做这样的事情
$credentials = $request->only('username', 'password');
一种方法是将用户名放在电子邮件列中,但这是一个不好的解决方案。
我在这里找到了办法
我们可以从模型(基于自定义字段)中吸引用户,例如
//抓取一些用户
$user = User::first();
并手动进行身份验证。
$token = JWTAuth::fromUser($user);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句