1

I am building a custom login process which follow these steps:

  1. Request user email (submits to custom controller)
  2. Check if user has an account
  3. If they have an account, show a form requesting their password. Or if they don't have an account then send them to the registration form.

The forms exist as front end templates.

I can accomplish all these steps but when the user is redirected I need to make the email address that they entered available to the template.

My custom controller calls a service to check if the user exists which is all fine. And I can redirect the user to the appropriate URL. But I can't figure out how to retain the email address they entered.

Currently I've just done it by appending the email as a query string but this is undesirable for a couple of reasons.

The controller currently looks like this although I feel this is totally the wrong way to be doing things.

public function actionEmailCheck() {
        $this->requirePostRequest();

        $email = Craft::$app->request->post('loginName');

        $result = MyModule::$instance->order->checkEmail($email);

        if($result) {
            $redirect = '/login/password?email='.$email;
        } else {
            $redirect = '/register?email='.$email;
        }

        $this->redirect($redirect);
    }

I feel like I should be able to set some template variables and load the appropriate template but that doesn't seem to work.

I also tried this

if($result) {
            $this->renderTemplate('/login/password', array('email'=>$email));
        } else {
            $this->renderTemplate('/register', array('email'=>$email));
        }

But while the template was displayed, I couldn't access the value for 'email'.

So how should I approach this?

1 Answer 1

1

You could do this by saving the email address to a custom session variable like this in your controller:

Craft::$app->getSession()->set('email', $email);

This should then be available in your template using:

{{ set email = craft.app.session.get('email') }}
3
  • I did try that but for some reason it wasn't working. I'll give it another go.
    – foamcow
    Oct 15, 2019 at 11:47
  • Well it worked that time with a minor change to Craft::$app->getSession()->set('email',$email). Would still like to know if there's a way to do it without setting the session variable.
    – foamcow
    Oct 15, 2019 at 12:44
  • The only way to do it would be either to set a session variable or to pass GET variables to the redirect destination Oct 15, 2019 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.