Yii 2 IdentityInterface data is lost after redirect

Looks good - thanks :)

Perhaps throw in this at the start of the function:


    	if (!\Yii::$app->user->isGuest) {

        	return $this->goHome();

    	}



May I suggest that you print and log like crazy - in the controller and the form and the model.

Good luck!

I hope that you implement the auth key functions because those are required when the enableAutoLogin option is set to true.

See vendor/yiisoft/yii2/web/IdentityInterface.

Thanks jacome for your feedback.So, far nothing has worked for me :(. But i’ll keep everyone posted with my achievement(mistakes) :)

Please modify your login method like this:




    public function login()

    {

        try {

            if ($this->validate()) {

                $test = Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);

                var_dump($test);

            }

            else {

                var_dump($this->getErrors());

            }

        }

        catch (\Exception $e) {

             var_dump($e->getMessage());

        }

        die;

    }



and let us know what comes up.

Thanks I tried your given code as well. But nothing has changed for me.I think there is some misunderstanding about this problem. Kindly go through below steps.

  1. user enter valid username/password on login screen.

  2. Yii::$app->user->login($this->getUser(), $this->rememberMe ? 36002430 : 0);

return true after successful validation. It means that user identity has invoked.I am able to get $id = Yii::$app->user->id; as well as Yii::$app->user->identity->username.

From this point user has redirected to some xyz page.

  1. Now user is clicking any other link from menu. At that time Yii::$app->user->identity is getting NULL.

So step 3) is having a problem. It means that Yii::$app->user->login($this->getUser(), $this->rememberMe ? 36002430 : 0) is not holding user identity even for a few minutes. I hope it will give you more insight of my problem.

Did you try with:


Yii::$app->user->login($this->getUser(), 3600*24*30)

I have already tried this code as well :( :(. This issue is driving me crazy now. I’m debugging each and every line of my code start with the entry script.

Have you tried to switch off db session?




I have removed 

'class' => 'yii\web\DbSession', from my web.php. there is no problem with database side as well.



Folks !!

So,finally I was able to figure it out that where was a mistake :). Some where in one of custom widget view file there was line of code, which i found in between the HTML tag.And that piece of line was something like as given below. >:( >:( :huh:




Yii::$app->user->logout();



Yes it is …it was a source of problem, which was destroying a user session.And off course it has to do it as it’s natural behaviour.

But in the end I would like to thank you jacome,bizley and softark for your constant watch on this topic and for your inputs here :). This kind of support make our community more stronger.

Thank you again !!

OMG - that is [size=“4”][color="#006400"]Classic[/color][/size] ! :lol:

Kudos to you for not giving up - if it’s any consolation, things like that keeps happening to me - it’s part of being a programmer, I guess.

It is a nightmare - until the problem is solved, in which case it feels almost as good as sex :P

oh em gee… XD I’m glad it’s finally solved, it drived me crazy.

On the link below you can find a good tutorial explaining how to login user from a database in Yii2. The only difference between your case and tutorial is that your sessions are stored in DB and in tutorial sessions are stored in files, but you can easily change it to the database.

http://code-epicente…ii-framework-2/

@Duri: He got it sorted :)

Ah, I didn’t see there is more discussion posts on another page

Let it be there, it might help to someone else :)

:lol: yeah it make some sense.

you were a bit late in that discussion.nevertheless thanks for bouncing back. :)