0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.