Jump to content

Cannot get Login/Register to login user


psy
 Share

Recommended Posts

Having problems with the new Login/Register module and hoping someone can help.

Desired result:

  • Guest lands on site (or later specific page) and is redirected to the Login page.
  • The user’s landing page is saved to a session and once logged-in, is redirected back to the original landing page

What’s happening:

  • Guest lands on any page on the site and is redirected to the Login page
  • Guest enters credentials, hits submit, then sometimes a variety of error messages get written to the logs including sessionCSRF and MYSQL errors. Guest user is presented with the Internal Server Error. Regardless, guest is not logged in

Scenario:

  • Using PW 3.0.76 and PHP 5.6

blob.thumb.png.fa15a413cea222e183059b39bb3df22c.png

  • User account created manually in admin with login-register privilege
  • User is my old mate:

blob.thumb.png.ccbe8035174c62e2a03ed68e9cc658ea.png

  • Site is configured to use delayed output with Regions & Functions API

In _init.php I have:

<?php namespace ProcessWire;
if(!$user->isLoggedin() && $page->id!=1193) { // not for login page
    $session->set('returnPage', $page->url);
    $session->redirect('/login/');
    die;
}

In login.php template:

 
<?php namespace ProcessWire;

if($user->isLoggedin() && !$input->get('profile') && !$input->get('logout')) {
    // login and go back to the previous page or go to the home page
    $goToUrl = $session->returnPage ? $session->returnPage : '/';
    $session->redirect($goToUrl);
} else {
    // let the LoginRegister module have control
    $content = $modules->get('LoginRegister')->execute();
}
?>

<div id="regContent">
    <div class="content-wrap">
        <div class="container clearfix">
            <region id="regPostContent">
                <!-- Post Content
                ============================================= -->
                <div class="postcontent nobottommargin clearfix col_three_fourth">
                    <?=$content?>
                 </div><!-- .postcontent end -->
            </region><!--#regPostContent-->
        </div>
    </div>
</div><!-- #content end -->

 

What am I doing wrong? Why can’t Fred login?

Any help to resolve much appreciated.

 

TIA

Psy

PS: Tried to use TracyDebugger to see what was happening but got js errors. Just not my day. Uninstalled TD and no errors :(

Edited by psy
typo
Link to comment
Share on other sites

@wbmnfktr thanks for recreating the scenario - epic :) and also for the feedback.

Still more weirdness on my site and I have a horrible feeling the conflicting session-related module may be FormBuilder even though at one point, I removed all FB forms from the login page (there was one in the footer) and turned off SessionCSRF on all FB forms. :( Will keep working on it. Any other help/suggestions in the meantime most welcome

Link to comment
Share on other sites

Managed to get Login/Register working on a clean install with no FormBuilder with a few minor tweaks to the code:

Firstly, to be on the safe side, I changed $page->url to $page->httpUrl. Secondly, on occasion my frontend page named 'login' got confused with the admin page named 'login' so specified exactly where to redirect.

if(!$user->isLoggedin() && $page->id!=1193) { // not for login page
    $session->set('returnPage', $page->httpUrl);
    $session->redirect($pages->get(1193)->url);
}

Still having issues with SessionCSRF with LoginRegister + FormBuilder. Will post in the FB forum.

Thanks

  • Like 1
Link to comment
Share on other sites

I saw your question in the FormBuilder linking to this thread, so replying here rather than there because I don't think it's related to FormBuilder, as it doesn't look like you are using FormBuilder for the forms here and as a result it shouldn't come into play. A couple things to look into: I'm wondering if there is an unexpected extra redirect occurring somewhere. It might be good to watch your developer tools Network tab (in Chrome) to look for 301 requests. It could be as simple as a page requiring a trailing slash and one not being present, or the opposite, and thus a redirect occurring. Or it could be that you've got those pages access protected using PW's template access control, and its redirects are happening before your _init.php even gets called. While you are testing, you might want to disable the _init.php code just to see what difference it makes.

Take a look at markup regions and make sure that your final output is as you expect when viewing the source of the pages. I noticed you are using the markup region tag termination hint <!--#regPostContent-->, which is good—that gives Markup Regions a shortcut to find where your tag ends, saving it time. But in another case you are using <!-- #content end -->, which might be confusing the markup regions because it should instead be <!--#regContent-->, and I don't see a <div id='content'> in the markup you pasted in. I think markup regions is probably just ignoring your <!-- #content end -->, but try replacing it with <!--#regContent--> just in case. 

  • Like 1
Link to comment
Share on other sites

Thank you @ryan for pointing me in the right direction. Problem solved.

FormBuilder was a red herring, all good there. The cryptic debug message was key to finding the cause.

Quote

Fatal error: Uncaught PDOException: You cannot serialize or unserialize PDO instances in [no active file]:0 Stack trace: #0 [internal function]: PDO->__sleep() #1 [internal function]: session_write_close() #2 {main} thrown in [no active file] on line 0

I refer to the home page throughout the site and normally add $homePage = $pages->get('/'); in the _init.php file.

Tried to be too clever and tweak page load speed by reducing the number of database calls so put the following below the LoginRegister code:

// shortcut to home page saved in session
$homePage = $session->get('home-page');  // Fairly certain this is the culprit!
if (!$homePage) {
    $homePage = $pages->get('/');
    $session->set('home-page', $homePage);
}

While the above works for $cache, it doesn't for $session, or even replacing $session with $_SESSION and using array_key_exists('home-page', $_SESSION), etc.

Reverted to $homePage = $pages->get('/'); for every page and Login/Register working perfectly. 

  • Like 3
Link to comment
Share on other sites

  • 6 years later...
Posted (edited)

[solved] And so it happened again but this time it was same but different.

Two weeks ago, all worked perfectly according to the LRP recipes. Today the fe user wouldn't log in??? I could login to the protected pages as superuser.

Checked all the advice given in above and other posts. I had not changed anything on the site and nothing recorded in the logs, Tracy Debugger etc. The only hint was in the Chrome browser dev tools that the LoginRegisterPro init() function wasn't loaded. 

I'm using MarkupRegions and on the latest versions of PW and LRP and PHP 8.1

My bad, I had in my template code:

<region id="regHeadCSS" pw-append>
    <?php $loginRegister->renderStyles() ?>
    <?php $loginRegister->renderScripts() ?>
</region>

Should have been:

<region id="regHeadCSS" pw-append>
    <?= $loginRegister->renderStyles() ?>
    <?= $loginRegister->renderScripts() ?>
</region>

OR:

<region id="regHeadCSS" pw-append>
    <?php echo $loginRegister->renderStyles() ?>
    <?php echo $loginRegister->renderScripts() ?>
</region>

Still don't understand why it worked originally but all's well that ends well

 

Edited by psy
more relevant info
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...