[英]How to add user_id to login parameter in Laravel?
Below is some method in a trait AuthenticatesUsers
where Illuminate\\Foundation\\Auth
, Laravel. 下面是特征AuthenticatesUsers
中的一些方法,其中Illuminate\\Foundation\\Auth
,Laravel。
...
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
...
public function username()
{
return 'email';
}
Originally, my goal is to make another login form with user_id and password in mobile device, so this will check Auth() and if success, it will work some method and automatically logout after then. 最初,我的目标是在移动设备中使用user_id和密码制作另一个登录表单,因此这将检查Auth(),如果成功,它将使用某种方法并在此之后自动注销。 could you tell me detailed advice? 你能告诉我详细的建议吗?
Additional question. 附加问题。
as Jaskaran Singh's
advice I added it also as below. 根据Jaskaran Singh's
建议,我还如下添加了它。
protected function authenticated(Request $request, $user)
{
if($request->Inn == 'Inn') {
return redirect()->route('mobiles_start', ['Inn' => 'Inn']);
}
elseif($request->Ut == 'Ut') {
return redirect()->route('mobiles_destroy', ['Ut' => 'Ut']);
}
return view('welcome');
}
but if login failed, then it is redirected back to the /login
page instead of expected view page that pre defined in the route(mobiles_start and mobiles_destroy) above. 但如果登录失败,则会将其重定向回/login
页面,而不是上面路由(mobiles_start和mobiles_destroy)中预定义的预期视图页面。 How could I do? 我该怎么办?
You can login with User ID like this: 您可以使用以下用户名登录:
if(Auth::loginUsingId($user->id)){
return response()->json(['success' => $user], $this->successStatus);
}
You don't have to extend the core trait or any core Laravel code. 您不必扩展核心特征或任何核心Laravel代码。
in app/Http/Controllers/Auth/LoginController.php add the following functions, this will override the default functions in AuthenticatesUsers trait. 在app / Http / Controllers / Auth / LoginController.php中添加以下功能,这将覆盖AuthenticatesUsers特征中的默认功能。
add username() function 添加username()函数
public function username()
{
if(request('id')){
return 'id'; // if request contains id then return it
}
return 'email'; // else return email
}
add validateLogin() function 添加validateLogin()函数
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required', //remove |string
'password' => 'required|string',
]);
}
that's it. 而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.