[英]How can I modify API so user can login with email and registration field as well?
I want that users can login with the registration number assigned to them or email.我希望用户可以使用分配给他们的注册号或 email 登录。 I have working api which only login users with email
我有工作 api 只用 email 登录用户
public $successStatus = 200;
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
You can check the value of the email
in your request
parameter to determine if an email
or number
has been provided and then attempt to log in your user that way.您可以在
request
参数中检查email
的值,以确定是否提供了email
或number
,然后尝试以这种方式登录您的用户。
public function login()
{
// determine if the value of the `email` field in the request
// is a valid `email` or not
$identity = filter_var(request('email'), FILTER_VALIDATE_EMAIL) ? 'email' : 'number';
// use the value of $identity for specifying the field,
// to compare against the value of the `email` in the request
if (Auth::attempt([$identity => request('email'), 'password' => request('password']))) {
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
}
return response()->json(['error'=>'Unauthorised'], 401);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.