Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel How Auth::attempt knows about Table

I have two tables in my Database 1: Items 3: Users and trying to log in the user and it is working properly, but my question is that i have not mentioned that from which table it will fetch the data, but it is fetching from the desired table automatically? Is it a Feature of Laravel or am doing something wrong? or i don't understand why this is happening?

Form

{{ Form::open(array('class'=> 'forming')) }}

    {{ Form::text('username',null,array( 'placeholder' => 'Username' ,'class' => 'insi')); }}<br>
    {{ Form::password('password',array('placeholder'=>'Password', 'class'=>'paswor')); }}<br>
    {{ Form::submit('SignIn!',array('class'=> 'but-n')); }}

{{ Form::close() }}

and the AuthController

class AuthController extends Controller{

        public function getLogin(){
            return View::make('login');
        }

        public function postLogin(){
            $rules = array('username'=> 'required', 'password'=>'required');

            $validator = Validator::make(Input::all(),$rules);

            if($validator->fails()){
                return Redirect::route('login')
                    ->withErrors($validator);
            }

            $auth = Auth::attempt(array(
                'username' => Input::get('username'),
                'password' => Input::get('password')
                ), false);
            if(!$auth){

                return Redirect::route('login')
                    ->withErrors(array(
                        'Invalid'
                        ));
            }

            return Redirect::route('home');

        }
}
like image 502
Junaid Farooq Avatar asked May 02 '14 15:05

Junaid Farooq


1 Answers

Auth uses the model you have in your file app/config/auth.php:

If you are using the Eloquent driver for Auth:

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/

'model' => 'User',

If you are using the Database driver:

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

If you need to do authentication using more than one table, you have some options, one of them is to use a construction like this one to proceed login:

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

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')))

...
like image 109
Antonio Carlos Ribeiro Avatar answered Sep 22 '22 15:09

Antonio Carlos Ribeiro