Yii2: User stays logged in on one application when logging out from another

I have a weird issue with my Yii2 setup. When I log out from the frontend app, I’m still logged into the backend admin panel. Same thing happens the other way around. If I logout from backend, the frontend keeps me signed in.

I tried looking for answers but couldn’t find much help. How can I make both applications logout together when user clicks logout on either side?

The logout function works normally on each app separately:

public function actionSignOut()
{
    Yii::$app->user->logout();
    
    return $this->goHome();
}

Both login processes work perfectly fine. It’s just the logout that doesn’t sync between frontend and backend.

sounds like a session config issue. check if both apps use the same session name and cookie domain in your config files. i had the same problem - frontend and backend were treating sessions separately when they should’ve been sharing the same user state.

The issue you’re experiencing is due to Yii2’s default behavior of maintaining separate user sessions for different applications. To achieve synchronized logouts across both your frontend and backend apps, you need to modify your configuration. Set both applications to share the same session storage by ensuring they utilize the same session name and cookie domain in their config files. Additionally, consider enhancing your logout action to terminate sessions in both instances simultaneously by making an internal request to the other app’s logout endpoint. This method has proven effective in my experience with similar setups.

Hmm, interesting - are you running both apps on the same domain or different subdomains? What session storage are you using (db, redis, files)? The session isolation might be intentional depending on your setup, but I’m curious about your specific config.