[英]Laravel Auth Login by company name, username, and password
I like user to login by company name, username, and password - they need to enter all those fields. 我喜欢用户通过公司名称,用户名和密码登录 - 他们需要输入所有这些字段。
username
, and password
fields are in the users
table. username
和password
字段位于users
表中。 company_name
are in the companies
table. company_name
在companies
表中。
username
can be duplicated in the users
table but each username is belong to different company name. username
可以在users
表中复制,但每个用户名都属于不同的公司名称。 company_id
is defined in users
table. company_id
在users
表中定义。
How can I Auth against company name, username, and password? 如何根据公司名称,用户名和密码进行身份验证?
I had a look at the attemptLogin()
method. 我看了一下
attemptLogin()
方法。
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
}
I can't override this because I need to pass specific user that is belong to company then do auth check. 我无法覆盖这个因为我需要传递属于公司的特定用户然后进行身份验证。
Update: 更新:
protected function authenticated(Request $request, User $user)
{
if (!$user->enable) {
Auth::logout();
return redirect('/login')->with('status', 'Account is disabled');
}
return redirect()->intended('/');
}
Since username
and company_name
pair is unique, you can do this: 由于
username
和company_name
对是唯一的,因此您可以这样做:
protected function attemptLogin(Request $request)
{
$user = User::where('username', $request->username)
->where(function ($q) use ($request) {
q->whereHas('company', function ($q) use ($request) {
$q->where('company_name', $request->company_name);
})
->orWhereHas('warehouse', function ($q) use ($request) {
$q->where('company_name', $request->company_name);
});
})
->first();
return !is_null($user) && \Hash::check($request->password, $user->password));
}
protected function validateLogin(Request $request)
{
$this->validate($request, [
'company_name' => 'required',
'username' => 'required|string',
'password' => 'required|string',
]);
}
You can DIY the login Request or Controller , such like that 您可以DIY登录请求或控制器,如此
// find user
$user = User::where...
// check user
// ...
// make he login
auth()->login($user);
Also, if you have the company_id field in users table. 此外,如果您在users表中有company_id字段。
It's belongs to One To One eloquent-relationships. 它属于One To One雄辩的关系。
https://laravel.com/docs/5.4/eloquent-relationships#one-to-one https://laravel.com/docs/5.4/eloquent-relationships#one-to-one
So you can use this method. 所以你可以使用这种方法。
$user_company_name = User::first()->company_name;
you could be check this thing . 你可以检查这件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.