Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajkumar_zuan's avatar

Loginas User - Impersonate

Hey everyone, Currently I'm working on laravel 8 application. in this we use Login as another user from admin. recently I'm facing an issue, that when i loggedin as another user while navigating to pages it automatically loggedout from current user back to admin. not able to troubleshoot where it occurs, sometimes in logs i can see permission denied for creating sessions inside storage/framework/session. So I checked in google many of them suggested to change file permission of the folder to 777. I tried that also. after executing this command from terminal, login as working for sometime but again the file permission changed to 775. I don't know how it revert back.

currently I hosted my application in webhost cpanel. in this I hosted multiple sites. for particular application I use below command to prevent "access denied" error. but sometimes later it revert back.

any Idea on this? could anyone give a suggestion to fix both issues (Loginas & file permission)

chmod -R 775 /home/site/public_html/storage
chmod -R 777 /home/site/public_html/storage/framework/cache/
chmod -R 777 /home/site/public_html/storage/logs/
chmod -R 777 /home/site/public_html/storage/framework/sessions/
chmod -R 777 /home/site/public_html/storage/framework/views/
chmod -R 777 /home/site/public_html/storage/framework/cache/data/
chmod -R 775 /home/site/public_html/bootstrap/cache

From controller

public function stopImpersonate(){
    Auth::user()->stopImpersonating();
    return redirect()->route('home');
}

public function loginas($userid){
    $user=User::findOrFail($userid);
    Session::put('impersonate', $user->id);
        return redirect()->route('home')->withStatus(__('LoggedIn as ').$user->name);
}

Middleware

public function handle(Request $request, Closure $next)    {
    if (Session::has('impersonate')) {
        $userId = Session::get('impersonate');
        $user = User::find($userId);
        if ($user) {
            Auth::setUser($user);
        }
    }
    return $next($request);
}

route

Route::group(['middleware' => ['auth','impersonate']], function () {

});
0 likes
0 replies

Please or to participate in this conversation.