JWT登录JWTAuth::attempt只能验证邮箱+密码,如果登录接口需要同时还兼容:用户名+密码,邮箱+密码,手机号+密码,该怎么做呢?
自己从数据库判断如手机号+密码,验证通过之后,JWTAuth::fromUser($user); 返回token 可以吗?
-------------分隔线,解决了------------
JWTAuth::attempt传入数组,key 对应数据库字段名即可 就可以验证,比如我的代码
//把账号设置到 name、email、phone字段里
$request->merge([
'name' => $request->get('account'),
'email' => $request->get('account'),
'phone' => $request->get('account'),
]);
try {
if ( $token = JWTAuth::attempt($request->only('name', 'password')) ||
$token = JWTAuth::attempt($request->only('email', 'password')) ||
$token = JWTAuth::attempt($request->only('phone', 'password')) ){
$user = Auth::user();
$token = JWTAuth::fromUser($user);
}else{
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}