php – 使用用户名而不是电子邮件进行JWTAuth身份验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用用户名而不是电子邮件进行JWTAuth身份验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1495字,纯文字阅读大概需要3分钟。
内容图文
![php – 使用用户名而不是电子邮件进行JWTAuth身份验证](/upload/InfoBanner/zyjiaocheng/788/43d396da8beb4761aa82e80bbc7cc88c.jpg)
有没有办法在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');
一种方法是将用户名放在电子邮件列中,但这是一个糟糕的解决方案.
解决方法:
Looking around,看起来可以通过设置凭证来实现这一点,例如$credentials = $request-> only(‘username’,’password’);.
所以它看起来像这样:
public function authenticate(Request $request)
{
// grab credentials from the request
//$credentials = $request->only('email', 'password');
// Change email to username
$credentials = $request->only('username', 'password');
try {
// attempt to 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 whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
内容总结
以上是互联网集市为您收集整理的php – 使用用户名而不是电子邮件进行JWTAuth身份验证全部内容,希望文章能够帮你解决php – 使用用户名而不是电子邮件进行JWTAuth身份验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。