简体   繁体   中英

how to use auth upon more than 1 table in laravel

I've 3 tables in my data base {

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

}

now I want a user log in to be upon his

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

This sounds like a wrong structure to me. You should keep user data(id, username ,email, pass etc) under one table. Then you can use those as foreign keys in other tables that you want to relate. The relationships between the tables would help you manage the user better, and it is more efficient. Also, if you follow this structure you would be able to Authenticate against every unique field like :

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

Else in the case you are right now you should extend the Auth class and use your own logic for authentication.

EDIT : The best way is to create a new table to hold the multiple emails.Lets say you create a table named user emails. This table will have 3 fields,id auto increment for primary, user_id as foreign key for user.id, and user_email, as the field that will hold the multiple emails. Note that the user_id field will not be unique, it will just be an index. you may have the following result :

sql表

The above represents 3 emails for user with id=1. Now, if you want to be able to login with any of those emails, you could do it as following :

 //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));

I understand that sometimes the tables our clients have are just odd, so if this is your case, create a separate class to process login from any of your needed types:

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;
    }
}

The login() does the job of logging in your user to your application exactly like Laravel does internally with Auth::attempt() , so everything else will work as it should after this.

And in your controller now you can do this:

$logon = new Logon;

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

return "logged in successfully";

or

...

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

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM