5

I have the application which have 2 Auth system. there is : User and Company.

is there a way to make an single Auth System for that's different models, without using any package (auth) ?

antoniputra
  • 4,251
  • 6
  • 26
  • 31

1 Answers1

14

I will tell you step by step.

  1. In default config laravel4, see on app/config/auth.php , you will see the default models is a user. and you want to login as a company.

  2. Trying to login, if Auth credentials is false (this means your user credential in users table is unavailable), Change auth models to Company.

  3. If step 2 is fails you cant login.

the code is

if(Auth::attempt($credentials)){
    // User is login
}
else{
    // trying login as company

    // setting auth model to Company
    Config::set('auth.model', 'Company');
    $auth = Auth::createEloquentDriver();
    Auth::setProvider($auth->getProvider());

    if(Auth::attempt($credentials)){
        //if sucess
        Session::put('isCompany', true);
    }
    else{
        //login fails
    }
}


//in routes.php

if (Session::has('isCompany')) {
    Config::set('auth.model', 'Company');
}

normally you can show Auth details, by using Auth::user()

I hope this can help you, thanks