Jump to content
psy

LoginRegister forms

Recommended Posts

I want to have both a login form and a registration form on the same page. After much tweaking in CSS & Hooks, I managed to get @ryan's LoginRegister module to display as required. All goes well until it doesn't, ie if there is an error in the form submission.

Each form has the wrapper ID hardcoded to "LoginRegister". Problem is when both forms are on the same page, both forms get the error messages and the Login form turns into a Register form. See attached before-and-after images and my code is in the 'spoiler'.

Spoiler

<?php namespace ProcessWire;

// the message doesnt work but the goose user who tried to login with both client and admin roles is auto-logged out
$message = '';
if ($input->get('logout')==1 && $input->get('g')==1 ) {
    $message = "You have been auto-logged out as the account you logged in with has both Client and Admin privileges which would completely screw up the application!";
}

if($user->isLoggedin() && !$input->get('profile') && !$input->get('logout')) {
    // you take over and render your own output or redirect elsewhere
    $session->redirect('/dashboard/');
} else {

    $wire->addHookBefore('Inputfield::render', function ($event) {
        $inputfield = $event->object;

        if ($inputfield instanceof InputfieldTextarea) {
            // textarea input
            $inputfield->addClass('form-control');
            $inputfield->columnWidth = 100;

        } else if ($inputfield instanceof InputfieldText) {
            // includes most single-line text types
            $inputfield->addClass('form-control');
            $inputfield->columnWidth = 100;

        } else if ($inputfield instanceof InputfieldSubmit) {
            // submit button
            $inputfield->addClass('button button-rounded');
        }

        if ($inputfield->required) {
            $inputfield->addClass('required');
            $inputfield->attr('required','required');
        }
    });

    $loginRegister = $modules->get('LoginRegister');

    $loginRegister->set('renderStyles', false);
    $loginRegister->set('renderScripts', true);

    if ($input->get('register')|| $input->get('register_confirm')) {
        $loginRegister->set('features', array('login', 'register', 'profile', 'forgot','login-email'));
    } else {
        $loginRegister->set('features', array('login', 'profile', 'forgot','login-email'));
    }
    $loginForm = $loginRegister->execute();

    $loginRegister->set('features', array('login', 'register', 'profile', 'forgot','login-email'));
    $input->get->register = 1;
    $registerForm = $loginRegister->execute();
    $input->get->register = 0;
}
?>

<region id="regHeadCSS">
    <link type="text/css" href="/wire/templates-admin/styles/font-awesome/css/font-awesome.min.css?v=30e" rel="stylesheet">

    <?php
    foreach(wire('config')->styles as $file) : ?>
        <link rel='stylesheet' type='text/css' href='<?=$file?>' />
    <?php endforeach;?>

</region>

<region id="regContent">
    <!-- Content
    ============================================= -->

    <section id="content">

        <div class="content-wrap">

            <div class="container clearfix">

                <div class="col_one_third nobottommargin">

                    <div class="well well-lg nobottommargin">
                        <?=$message?>
                        <?php if (!$input->get('register') && !$input->get('register_confirm')) : ?>
                        <?php
                            try {
                                echo $loginForm;
                            } catch (WireException $e) {
                                echo "Too many failed login attempts.<br>" . $e->getMessage();
                            }
                        ?>
                        <?php endif;?>
                    </div>

                </div>

                <div class="col_two_third col_last nobottommargin">
                    <?=$page->body?>
                    <?php
                    // needed to avoid server 500 error when user tries to login too many times in the other form
                    try {
                        echo $registerForm;
                    } catch (WireException $e) {
                        echo "Too many failed login attempts.<br>" . $e->getMessage();
                    }
                    ?>
                </div>

            </div>

        </div>

    </section><!-- #content end -->

</region><!--regContent-->

Help to fix much appreciated

 

Screenshot 2019-11-02 13.51.49.png

Screenshot 2019-11-02 13.52.19.png

Share this post


Link to post
Share on other sites

This is a very hackish solution, but then again it's Saturday, I have never used the LR module before, and I could do with more coffee :)

Spoiler

if($page->id === 1027) {
    
    if( !isset($_GET["register"]) && !isset($_GET["login"]) ) {
        $content .= $modules->get('LoginRegister')->execute();
        $css = "<style>
        .LoginRegisterLinks {
            display: none;
        }
        #ireg {
            display: block;
            border: none;
            height: 100vh;
            width: 100%;
            overflow: hidden;
        }
        </style>";
        $thisUrl = $pages->get(1027)->url;
        $iframe = "<iframe id='ireg' src='$thisUrl?register=1'></iframe>";
        $content .= $css . $iframe;
    } else {
    
        $css = "<style>
        .LoginRegisterLinks {
            display: none;
        }
        h1,
        .languages,
        .topnav,
        .breadcrumbs,
        .search,
        footer {
            display: none !important;
        }
        body {
            padding: 0;
        }
        </style>";
        
        $content .= $css;
        $content .= $modules->get('LoginRegister')->execute();
    }
}
 

Basically: Create an iFrame. May not be elegant, but it seems to work. You'd have to add a logic for the registration confirmation page, so that this doesn't load in the iframe, but in its own page. btw, the very first line above was me trying out another idea: create several pages with the LR-template and then tinker around with form targets or hidden input fields.

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @dragan will give it a try.

Another idea I had was to use FormBuilder for the login form - again hackish.

Really wish PW had a solid Login/Register module...

  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...