3

How could I go about allowing a (not logged in) guest user to submit their email address, check behind the scenes if it belongs to a user account, and then return:

  1. A login form if it does
  2. A registration form if it doesn't
2

I assume you have basic knowledge about plugins and routing, if not I'll update my post in a few hours.. otherwise Your form could be

<!-- check if user submitted something -->
{% if userExists is defined %}
    {% if userExists == true %}
        <!-- user exists login -->
        hello {{ user.name }}

        <form method="post" accept-charset="UTF-8">
            {{ getCsrfInput() }}
            <input type="hidden" name="action" value="users/login">

            <h3><label for="loginName">Username or email</label></h3>
            <input id="loginName" type="text" name="loginName"
                   value="{{ craft.session.rememberedUsername }}">

            <h3><label for="password">Password</label></h3>
            <input id="password" type="password" name="password">

            <label>
                <input type="checkbox" name="rememberMe" value="1">
                Remember me
            </label>

            <input type="submit" value="Login">

            {% if errorMessage is defined %}
                <p>{{ errorMessage }}</p>
            {% endif %}
        </form>

        <p><a href="{{ url('forgotpassword') }}">Forget your password?</a></p>

    {% else %}
        <!-- user does not exist register -->
        <!-- snip see here for full content https://craftcms.com/docs/templating/user-registration-form -->

    {% endif %}

{% else %}
    <!-- parameter not set show search form -->
    <form method="post">
        <input type="hidden" name="action" value="test/checkEmail">
        <input type="text" name="email">
        <input type="submit">
    </form>
{% endif %}

And your controller

public function actionCheckEmail(){
    $email = craft()->request->getParam('email');
    if($user = craft()->users->getUserByEmail($email)){
        // user registered
        $userExists = true;
    }else{
        $userExists = false;
    }
    craft()->urlManager->setRouteVariables([
        'userExists'    => $userExists,
        'user'          => $user
    ]);
}
|improve this answer|||||
  • Thanks Robin. I'm heading to bed now. I will take a good look tomorrow. – Jonathan Schofield Dec 13 '17 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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