vendredi 27 avril 2018

Laravel 5.5 Switching users in the session

I have made a simple action 'Act as this user' for aplication adminis. It looks like this:

  1. logout current user

  2. log in as given user

  3. set 'shared_cookie' based on new current user ( \App\Auth\AuthUser::getUserData() gets prepared data based on Auth::user() )

    public function actAsUser(Request $request, $id)
    {
      Auth::guard()->logout();
      $request->session()->invalidate();
      \Cookie::queue(\Cookie::forget('shared_cookie'));
    
      $id = \Hashids::decode($id)[0];
      $user = User::find($id);
    
      Auth::guard()->login($user);
      \Cookie::queue(\Cookie::make('shared_cookie', \App\Auth\AuthUser::getUserData(), config('session.lifetime') , null, config('app.cookie_shared_domain'), false, false));
    }
    

It should work, but not quite. The result is random!

'shared_cookie' is always set properly, but in the next request, Auth::user() randomly returns old or new user.

Sometimes it seems, that it depends from the user, but not quite..

Any ideas? Please help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire