我如何修改 API 以便用户也可以使用电子邮件和注册字段登录?
How can I modify API so user can login with email and registration field as well?
我希望用户可以使用分配给他们的注册号或电子邮件登录。我正在工作 api,仅使用电子邮件
登录用户
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);
}
}
您可以检查 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);
}
我希望用户可以使用分配给他们的注册号或电子邮件登录。我正在工作 api,仅使用电子邮件
登录用户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);
}
}
您可以检查 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);
}