php – 使用用户名而不是电子邮件进行JWTAuth身份验证
发布时间:2020-12-13 17:00:14 所属栏目:PHP教程 来源:网络整理
导读:有没有办法在laravel jwt-auth中使用用户名(或任何自定义列)而不是电子邮件进行身份验证? 这是现有的代码 public function authenticate(Request $request){ $credentials = $request-only('email','password'); try { // verify the credentials and creat
有没有办法在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')); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |