Need help with forced redirect after email activation

Hi,

I have a forced redirect for users who have not yet activated their account via email confirmation. I’ve run into a new problem since updating to 2.0.0 and not sure how to fix it. Before, once the link in the email was clicked, the user would be directed to a “email confirmed” page basically saying that their email has been confirmed, account is activated, and they can browse the site freely. However, for some reason unknown to me, after clicking the link in the email, the app returns you back to the “confirm your email page”, even though the database has been updated and the conditions for the redirect no longer apply. What’s more is that, despite seeing that page, you’re free to browse the rest of the site without problem.

Would appreciate some help in finding the error in my code that prevents the app from displaying the "email confirmed" page. The pertinent code is below.

Site controller




    public function beforeAction($action)

    {

        if (parent::beforeAction($action)) {

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

                if (Yii::$app->user->identity->status == '5' && $this->getRoute() != 'site/confirm-email' && $this->getRoute()

                    != 'site/resend-email') {

                    $this->redirect(['site/confirm-email']);

                }

            }

            return true;

        } else {

            return false;

        }

    }


    public function actionEmailConfirmation($token)

    {

        try {

            $model = new ConfirmEmail($token);

        } catch (InvalidParamException $e) {

            throw new BadRequestHttpException($e->getMessage());

        }

        $model->emailConfirmed();

        $this->actionEmailConfirmed();

    }


    public function actionEmailConfirmed()

    {

        return $this->render('emailConfirmed');

    }



If anymore information is needed, please let me know. Thanks!

Hi,

You can check in this:

if (Yii::$app->user->identity->status == ‘5’ && $this->getRoute() != ‘site/confirm-email’ && $this->getRoute()

                != 'site/resend-email') {


                $this->redirect(['site/confirm-email']);


            }

The conditions to check in "if" is ok?

Good luck.

Thanks for the suggestion, but the status is updated accordingly (no longer 5) and thus, the before action shouldn’t trigger, however, it still does.

I think that if you check the email confirmation of the user, you must not use beforeAction, because "!Yii::$app->user->isGuest" is loginned status, this is incorrect.

If you want to redirect to actionEmailConfirmation($token), here, you should use the conditions that can check the updated status of the user.For instance,if status==0, redirect to ‘site/confirm-email’, and vice versa redirect to ‘site/already-active’.

When user click the actived link that in your email,this call actionEmailConfirmation($token),so,

Why i do not see any conditions to check that their updated status was updated already?

$this->actionEmailConfirmed() is always called whether the status has been updated or not?

Now, i think you had a soulution yourself.

LeVanLau

Thank you for replying, but I’m having a hard time understanding what you mean. Could you please try to clarify and perhaps modify the code I posted to show what you mean?

Ok,

I think your issue related to code below:

public function beforeAction($action)

{


    if (parent::beforeAction($action)) {


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


            if (Yii::$app->user->identity->status == '5' && $this->getRoute() != 'site/confirm-email' && $this->getRoute()


                != 'site/resend-email') {


                $this->redirect(['site/confirm-email']);


            }


        }


        return true;


    } else {


        return false;


    }


}
  • beforeAction is always called in SiteController, before other actions are called.

  • $this->redirect([‘site/confirm-email’]) is always called, this mean that the IF condition is always right.

  • i think you must check IF condition again.

Ok.