Laravel:具有 2 个表的自定义身份验证

Laravel: Custom Auth with 2 tables

我正在使用 Laravel 8,我需要使用来自 2 table 的数据来验证用户:用户和客户。当用户登录时,他们将输入 3 个值:电子邮件、密码和帐户。 “帐户”字段来自客户 table.

所以我需要连接才能从“客户”访问“帐户”字段table。

我看到的一个选项是:

public function validateCredentials(UserContract $user, array $credentials) {
        $plain = $credentials['password'];

        //Custom Query

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

在“自定义查询”部分,我可以使用 $user->customer_id 进行查询以获取客户数据并检查是否匹配 $credentials['account'],但不确定是否匹配是最好的方法。

在此先感谢您的帮助。

我最终构建了一个自定义提供程序,并覆盖了“retrieveByCredentials”方法,这成功了。

public function retrieveByCredentials(array $credentials)
    {

        if (empty($credentials) ||
           (count($credentials) === 1 &&
            Str::contains($this->firstCredentialKey($credentials), 'password'))) {
            return;
        }

       
        $query = User::select(['customer.identifier', 'users.*'])->join('customer', 'customer_id', '=', 'customer.id');

        foreach ($credentials as $key => $value) {
            if (Str::contains($key, 'password')) {
                continue;
            }

            if($key == 'account') {
                $query->where('customer.identifier', '=', $credentials['account']);
            } else {
                $query->where($key, $value);
            }

        }

        return $query->first();

    }