0
votes

In my Laravel 5.1 based app I have to login using multiple user tables. Currently I have changed my constructor and postLogin functions in AuthController

function postLogin(){
      //Try login using table1
      //If it fails then check creds in table2
      // If this succeeds then do 3 things
      // 1- Change provider
      $userprovider = new \Illuminate\Auth\EloquentUserProvider(app('hash'), 'App\Table2');
      Auth::setProvider($userprovider); 
      // 2- Login
      auth()->loginUsingId($user->table2_id, false);
      // 3- Add table2 login in session
      session(['table2_login' => true]);
}

public function __construct() {

    if (session()->get('table2_login')) {

        $userprovider = new \Illuminate\Auth\EloquentUserProvider(app('hash'), 'App\Table2');
        Auth::setProvider($userprovider);
    }

    $this->middleware('guest', ['except' => 'getLogout']);
}

After these changes Primary user table login is working fine but when I enter the credentials from table2 it redirects me to '/home' after logging-in. All subsequent requests also redirect me to '/home'. In AuthController constructor I get proper logged-in user object using auth()->user() after changing the provider.

1

1 Answers

1
votes

I resolved this by moving my change provider logic from the postLogin function to the constructor of Base controller. Now every things seems to be working fine.