【问题标题】:how to use auth upon more than 1 table in laravel如何在 laravel 中的多个表上使用身份验证
【发布时间】:2014-04-28 10:00:15
【问题描述】:

我的数据库中有 3 个表 {

being : holds the id field of member, //for some reason i have to
member: holds member specific data,   //like unique name password ...           
email : holds emails for all users    //for no duplication and some few more things

}

现在我想要一个用户登录在他的身上

    {unique name and password} or 
    {id and password} or 
    {email and password}

【问题讨论】:

    标签: php authentication laravel laravel-4


    【解决方案1】:

    我知道有时我们的客户拥有的表只是奇怪的,所以如果这是您的情况,请创建一个单独的类来处理您需要的任何类型的登录:

    class Logon {
    
        public function loginViaEmail($email, $password)
        {
            if ($emailModel = Email::where('email', $email)->first())
            {
                return $this->login($emailModel->user_id, $password);
            }
    
            return false;
        }
    
        public function loginViaName($name, $password)
        {
            if ($memberModel = Member::where('name', $name)->first())
            {
                return $this->login($memberModel->user_id, $password);
            }
    
            return false;
        }
    
        public function loginViaId($id, $password)
        {
            if ($beingModel = Being::where('id', $id)->first())
            {
                return $this->login($beingModel->user_id, $password);
            }
    
            return false;
        }
    
        public function login($id, $password)
        {
            $user = Member::find($id);
    
            if(Hash::check($password, $user->password))
            {
                Auth::loginUsingId($id);
    
                return true;
            }
    
            return false;
        }
    }
    

    login() 负责将用户登录到您的应用程序,就像 Laravel 在内部使用 Auth::attempt() 所做的工作一样,因此此后一切都将正常工作。

    现在您可以在控制器中执行此操作:

    $logon = new Logon;
    
    if ( ! $logon->loginViaEmail(Input::get('email'), Input::get('password')))
    {
        return "username or password invalid";
    }
    
    return "logged in successfully";
    

    ...
    
    if ( ! $logon->loginViaName(Input::get('name'), Input::get('password')))
    
    ...
    

    【讨论】:

      【解决方案2】:

      对我来说,这听起来像是一个错误的结构。您应该将用户数据(id、用户名、电子邮件、密码等)保存在一张表下。然后,您可以将它们用作要关联的其他表中的外键。表之间的关系将帮助您更好地管理用户,并且更有效。此外,如果您遵循此结构,您将能够针对每个唯一字段进行身份验证,例如:

      Auth::attempt(array('id'=>$id,'password'=>$password)
      

      如果您现在处于其他情况,您应该扩展 Auth 类并使用您自己的逻辑进行身份验证。

      编辑:最好的方法是创建一个新表来保存多封电子邮件。假设您创建一个名为用户电子邮件的表。该表将有 3 个字段,主要是 id 自动递增,user_id 作为 user.id 的外键,user_email 作为保存多封电子邮件的字段。请注意,user_id 字段不是唯一的,它只是一个索引。你可能会得到以下结果:

      以上代表 id=1 的用户的 3 封电子邮件。 现在,如果您希望能够使用这些电子邮件中的任何一个登录,您可以执行以下操作:

       //UserEmail represents the Eloquent model for the table that holds the    user emails
      $emailrow = UserEmail::where('email','=',Input::get('email')->get();
      $id = User::find($emailrow->user_id);
      Auth::attempt(array('id'=>$id,'password'=>Input::get('password));
      

      【讨论】:

      • thnx 很多,但是对于结构,我需要为同一个用户存储多封电子邮件,所以我如何使用 1 个表来做到这一点?
      猜你喜欢
      • 2015-05-25
      • 2017-02-08
      • 2017-11-18
      • 2019-12-09
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2017-12-16
      相关资源
      最近更新 更多