[英]How to login with phone number and country code in Laravel
我有一个带有电子邮件 ID 和密码的登录页面。 现在在 API 中,我收到了国家代码和电话号码以及密码。 我的代码如下。
if(!isset($request->phone)){
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
'remember_me' => 'boolean',
]);
$credentials = request(['email', 'password']);
} else {
$request->validate([
'phone' => 'required|string',
'country_code' => 'required',
'password' => 'required|string',
'remember_me' => 'boolean',
]);
$credentials = request(['phone', 'country_code', 'password']);
}
if(isset($request->phone)){
$user = User::where('phone',$credentials['phone'])->where('country_code',$credentials['country_code'])->first();
if(!Hash::check($credentials['password'], $user->password)){
return response()->json([
'message' => 'Unauthorized'
], 401);
}
} else {
if (!Auth::attempt($credentials)) {
return response()->json([
'message' => 'Unauthorized'
], 401);
}
$user = $request->user();
}
return $user->only(['id', 'name', 'email', 'api_token']);
如何使用电话号码和国家代码登录? 以及 Auth:attempt 在哪里,以便我可以复制它并以相同的方式使用它
检查国家代码和电话号码是否匹配,然后使用它检索用户详细信息,然后运行
Hash::check('INPUT PASSWORD', $user->password);
类似的描述参见https://stackoverflow.com/a/25136309/8594737 ,虽然有点不同,但概念相似。
如果您还需要什么,请告诉我,我会尽力回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.