Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using two different models and tables for auth system at laravel 4?

Tags:

laravel-4

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

like image 858
antoniputra Avatar asked Mar 21 '14 23:03

antoniputra


1 Answers

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

like image 166
Arryangga Aliev Pratamaputra Avatar answered Oct 13 '22 10:10

Arryangga Aliev Pratamaputra