dongzhanbi0027
2019-04-09 20:18
浏览 167
已采纳

Laravel身份验证 - 不同表格中的电子邮件

I have two tables:

persons:

id   name   email   phone   person_type_id

users

id   person_id  password role_id etc...

Could you please tell me how to make Laravel's (5.8) built in Authentication System use the email field from persons table when authenticating a user? FYI, the value of guard provider has to be users.

图片转代码服务由CSDN问答提供 功能建议

我有两个表:

人员:

  id name email phone person_type_id 
   
 
 

用户

  id person_id password role_id etc  ... 
   
 
 

您能否告诉我如何使Laravel(5.8)内置身份验证系统使用 电子邮件字段 验证用户时,>人表 仅供参考,警卫提供者的价值必须是用户

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doupu3635 2019-04-10 13:09
    已采纳

    You could create a custom user provider (which inherits from Illuminate\Auth\EloquentUserProvider) and override the retrieveByCredentials method.

    See Laravel Docs - The User Provider Contract

    点赞 评论
  • dsklzerpx64815631 2019-04-14 14:41

    @Thomas's suggestion helped me to solve the problem. In case it helps anyone, here's my code:

    I created CustomUserProvider.php

    <?php
    
    namespace App\Providers;
    use Illuminate\Support\Str;
    use Illuminate\Auth\EloquentUserProvider;
    use Illuminate\Contracts\Hashing\Hasher as HasherContract;
    
    class CustomUserProvider extends EloquentUserProvider { 
    
    private $method_to_email_model;
    
    public function __construct(HasherContract $hasher, $model, $method_to_email_model)
    {
    
        parent::__construct($hasher, $model);
    
        $this->method_to_email_model = $method_to_email_model;
    }
    
    public function retrieveByCredentials(array $credentials)
    {
    
        if (empty($credentials) ||
           (count($credentials) === 1 &&
            array_key_exists('password', $credentials))) {
            return;
        }
    
        $query = $this->createModel()->newQuery();
    
        foreach ($credentials as $key => $value) 
        {
            if (Str::contains($key, 'password')) {
                continue;
            }
    
            if (is_array($value) || $value instanceof Arrayable) {
    
                $query->with([$this->method_to_email_model => function($q) use($key, $value){
                    $q->whereIn($key, $value);
                }]);
    
            } else {
    
    
                $query->with([$this->method_to_email_model => function($q) use($key, $value){
                    $q->where($key, $value);
                }]);
            }
        }
    
        return $query->first();
     }   
    
    }
    

    Then in App\Providers\AuthServiceProvider.php file, inside boot function:

    Auth::provider('custom_user_provider', function ($app, array $config) {
        return new CustomUserProvider($app['hash'], $config['model'], $config['method_to_email_model']);
    });
    

    Inside config/auth.php

     'providers' => [ 
        'users' => [
            'driver'                => 'custom_user_provider',
            'model'                 => App\User::class,
            'method_to_email_model' => 'person',
        ],
      ],
    

    Finally in App\User.php (User Model)

    protected $appends = [
        'email', 'first_name', 'last_name'
     ];
    
    public function person()
    {
        return $this->belongsTo(Person::class, 'person_id', 'id');
    }
    
    public function getEmailAttribute()
    {
        return $this->person->getAttribute('email');
    }
    
    public function getFirstNameAttribute()
    {
        return $this->person->getAttribute('first_name');
    }
    
    public function getLastNameAttribute()
    {
        return $this->person->getAttribute('last_name');
    }
    
    点赞 评论

相关推荐 更多相似问题