losing user identity upon redirect after successful login

i have created a new identity class, i have implemented IdentityInterface and all the necessary methods. note that it does NOT extend ActiveRecord, but rather extends my own base Model class that does not derive from AR.

everything seems to work fine - i can log in and be correctly authenticated. i have debugged the code and i can see that my identity class is correctly set in Yii::$app->user after IdentityInterface::validatePassword() and User::login() have been called.

however, once the login succeeds and the user is redirected, Yii::$app->user contains an empty IdentityInterface, rather than the one that was there immediately prior to the redirect. i have traced through code in yii\web\Controller, yii\base\Controller, yii\web\Response, SiteController, yii\web\User, etc., and i cannot see where the identity is getting lost. i only see that as soon as i am redirected after the successful login, the Yii::$app->user contains an empty identity, not the one that it contained immediately before the redirect.

i am not sure if this is a session issue (i don’t see anything in the session that seems to hold a yii\web\User), or what.

i should mention that when i change my app to use an ActiveRecord-based identity, it works fine - the identity is retained after the successful login redirect. but i have traced through all the code there as well, and i don’t see what is different in the logic.

any help at all would be GREATLY appreciated.

EDIT: forgot to mention that i am using the backend app of the advanced application template, yii2.

I have this problem too. Mine is in the frontend of an advanced app

switchIdentity() in yii\web\User sets the user id into the session. When the site redirects somewhere, this is reloaded in renewAuthStatus() which will find the session user id and if present, will call findIdentity() to instantiate the User class for the correct user.

I guess your class is either not setting the id anywhere when the user logs in or otherwise findIdentity() does not work properly in your User model.

1 Like