Jump to content

Login form not working


hansv
 Share

Recommended Posts

I read the many topics on this forum about login-forms from Ryan's post in 2012 to  improvements of Renobirds and many other contributors.   My login-template-file (that I found in the many topics and changed a little bit) is beneath.

What's working:

  • When I'm logged in, the redirect works correctly
  • When I'm not logged in, the form is shown correctly, login failure message is shown

What's not working:

  • but the try ... catch goes always to the else-side.  I made a new user to be sure the username and password are correct but login fails always.

I'm working with PW 3.0.29 on localhost

I hope someone can help me

<?php
$body = "";

if ($user->isLoggedin()) {
        $session->redirect($pages->get("/sidenav/ledenzone")->url);
}

if ($input->post->user && $input->post->pass) 
{
    $username = $sanitizer->username($input->post->user);
    $password = $input->post->pass;

   try {       
        if ($session->login($username, $password)) {
            $session->redirect($pages->get("/sidenav/ledenzone")->url);  // redirect  after user login
       } else {
          $error = "Wrong username or password. Login failed.";
       }
   } catch (Exception $e) {
       $error = $e->getMessage();
   }
}

$body .= "
        <div class='login-box'>
        <div class='panel panel-default'>
            <div class='panel-heading'>
                <h3 class='panel-title'><span class='lock-icon'></span><strong>Member Log-in</strong></h3>
            </div>
            <div class='panel-body'>
                <form role='form' action='./' method='post'>
                    <div class='message-error'>$error</div>
                    <div class='form-group'>
                        <label for='user'>Username</label>
                        <input type='text' name='user' id='user' class='form-control' placeholder='Username' />
                    </div>
                    <div class='form-group'>
                        <label for='pass'>Password</label>
                        <input type='password' name='pass' id='pass' class='form-control' placeholder='Password' />
                    </div>
                    <button type='submit' class='btn btn-sm btn-primary'>Login</button>
                </form>
            </div>
        </div>
        </div>
";

 

Link to comment
Share on other sites

Looks okay to me. A couple of minor things that won't be related to your problem:

  • $sanitizer->username is deprecated in favour of $sanitizer->pageName
  • I don't see where you set the default state of $error, so you may get an undefined variable notice when an exception is not thrown.

Things to try:

  • log $username and $password before $session->login() to check that the variables are holding the correct information.
  • can you login with this username and password at the PW admin login?
  • Like 3
Link to comment
Share on other sites

The documented example for $session->login() sets the returned user to a variable and then tests the variable (and this is how I use the method in my custom login).

$u = $session->login('bob', 'laj3939$a');
if($u) {
  echo "Welcome Bob";
} else {
  echo "Sorry Bob";
}

The way you are doing it should be fine, but try setting a variable and then you can log/dump the variable to see what is being returned. I guess it is returning null but it pays to check.

You could also test if you can log the user in successfully by using $session->login() directly as per the example above, to narrow down where the problem lies.

 

 

  • Like 1
Link to comment
Share on other sites

@Robin S

I tried everything you suggested.  As it should be working, it didn't, therefore I started from a new PW installation, and de login-form is working fine now!  It is still a mystery, but I'm glad you confirmed the code was right and thx for all suggestions.

hansv

  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...