简体   繁体   English

Laravel Auth按公司名称,用户名和密码登录

[英]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. usernamepassword字段位于users表中。 company_name are in the companies table. company_namecompanies表中。

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_idusers表中定义。

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: 由于usernamecompany_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.

相关问题 Laravel:使用Auth驱动程序登录,但没有密码 - Laravel: Login with Auth driver, but without password 如何在Laravel 5.5.28中更改默认的身份验证密码和用户名 - How to change default auth password and username in laravel 5.5.28 Laravel 5.7 使用 tymon/jwt 使用电子邮件/用户名和密码进行身份验证 - Laravel 5.7 Auth with email/username and password using tymon/jwt auth.basic不断询问用户名/密码-Laravel 4 - auth.basic keeps on asking for username/password - laravel 4 Laravel身份验证无法使用正确的用户名/密码组合进行工作 - Laravel auth not working using correct username/password combination Laravel 4 Auth ::尝试使用电子邮件或用户名和密码 - Laravel 4 Auth::attempt using either email or username and password 如何仅使用用户名和密码在 laravel 中验证登录? - how to authenticate login in laravel using only username and password? 如何处理laravel登录错误,用户名与密码不同 - how to handle laravel login errors, username different from password 如何使用预定义的电子邮件/用户名和密码登录Laravel? - How Can I Login In Laravel With Predefined Email/Username and Password? 如何仅在 Laravel 中使用 url 和 hash 参数登录? (没有用户名和密码) - How to login with url and hash parameter only in Laravel? (without username and password)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM