有没有办法在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')); }