login form set failure message

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
sticcino
User
Posts: 1043

login form set failure message

Post by sticcino »

Hi,

just noticed that my alert messages are not displaying any longer in my login form, code was working in prev versions, but now doesn't display the error messages

here's a sample.

public function userLoggingIn($usr, &$pwd)
{
        				if($resultset['IsLockedOut'] == 1) {
        					$sFormCustomError = "Account Locked Out.";
        					$this->setFailureMessage($sFormCustomError); 
        					return FALSE;
        				}

}

does the login form not refresh itself any longer ?, has something changed. do i have to use a ->terminate(/login) instead of the return false?


mobhar
User
Posts: 11660

Post by mobhar »

Because that condition will never happened. You should check the if condition. Where the $resultset['IsLockedOut'] variable came from? You did not declare this variable in that event. So, make sure that condition happens.


sticcino
User
Posts: 1043

Post by sticcino »

Hi,

it does triggers along with the other 4 or 5 conditions in the UserLoggingIn(). This is the same code i've been using in past versions of PHPMaker. I only noticed it now, because I have my web servers behind a load balancer and it wasn't returning the "real" IP address. For this project its a single web server and returning the correct ip address, so now the code is triggering but with no messages being displayed.

I think the problem is when you return FALSE, the form is not refreshing itself in order to display the messages...

I added a terminate(/login) this does refresh the screen and display the messages, but the problem is that it logins the user in and bypasses the rest of the authentication code.

I know the code is working because when I try to fail it, it just sits on the login screen

as a matter of fact, not even the login cancelled message is being displayed...

            if ($validate) {
                // Call Logging In event
                $validate = $this->userLoggingIn($this->Username->CurrentValue, $this->Password->CurrentValue);
... code
                } else {
                    if ($this->getFailureMessage() == "") {
                        $this->setFailureMessage($Language->phrase("LoginCancelled")); // Login cancelled
                    }
                }

so when I return FALSE, it should display the Login Cancelled Message, if i'm reading the code correctly


mobhar
User
Posts: 11660

Post by mobhar »

If you meant that is a global variable, you should declare it inside the event. In addition, your code above do not include the return true; line at the end of the event. So, your code should be as follows:

public function userLoggingIn($usr, &$pwd)
{
    global $resultset; // <-- declare it as a global variable
    if($resultset['IsLockedOut'] == 1) {
        $sFormCustomError = "Account Locked Out.";
        $this->setFailureMessage($sFormCustomError); 
        return false;
    }
    return true; // <-- you missed this line; make sure you do not remove it
}

sticcino
User
Posts: 1043

Post by sticcino »

yes that code works and displays the error, but it also logs the user in because true is returned from the userLoggingIn() event.

the problem is, if the login is aborted by a condition like the example, nothing is displayed, the login process is stopped as it should be becuase FALSE is returned, when all your conditions are met, then true should be returned to continue the login process.


arbei
User
Posts: 9281

Post by arbei »

You better post your latest code for discussion and explain what is $resultset in your code. You may enable debug and check your condition using, e.g.

Log($resultset['IsLockedOut']);

and check the value in the log file.


sticcino
User
Posts: 1043

Post by sticcino »

that is just the results of a query from the user table that returns some fields regarding user status, device type etc..
even without my custom code, messages are not displayed if userLoggingIn() returns FALSE

the failure messages/code are getting triggered, but not displaying.

I use PHPed and am able to step through the code in the IDE and can see the results realtime, everything is triggering as should be but the messages are not displayed.

I've taken all my code out and have changed the code to this:

    // User Logging In event
    public function userLoggingIn($usr, &$pwd)
    {
        $this->setFailureMessage("Something Went Wrong at userLoggingIn"); 

        return false; 
   }

if you return FALSE in the above, the message will not be displayed...
if you return TRUE in the above, the message will be displayed...

i think because these are php queued messages they won't display until a page is refreshed...
when you return FALSE from userLoggingIn() the page is not being refreshed, thus the messages will not appear, when true is returned.. the run() continues and completes the login success which then refreshes or changes the landing page and the messages are then displayed.

even the message in function run(), if false is returned from userLoggingIn() the "LoginCancelled" message doesn't get triggered

In run()

condensed code...

            if ($validate) {
                // Call Logging In event
                $validate = $this->userLoggingIn($this->Username->CurrentValue, $this->Password->CurrentValue);
                if ($validate) {
                  LOGIN SUCCESSFUL...
                } else {
                    if ($this->getFailureMessage() == "") {
                        $this->setFailureMessage($Language->phrase("LoginCancelled")); // Login cancelled <<<<<<<<<<<====== this should display if FALSE returned from userLoggingIn()
                    }
                }

the else message does not get displayed as well


arbei
User
Posts: 9281

Post by arbei »

sticcino wrote:

if ($this->getFailureMessage() == "") {
$this->setFailureMessage($Language->phrase("LoginCancelled")); // Login cancelled <<<<<<<<<<<====== this should display if FALSE returned from userLoggingIn()
}

No, it won't. Because you have set failure message and therefore $this->getFailureMessage() does not equal "", the failure message you set will return.

In fact, your code does work. If not, it is probably because of something you did not mention. For example, if you have enabled two factor authentication, note that the login form is submitted and reloaded by Ajax, so setFailureMessage() will not work (which works by posting back and reloading the page). If that is the case, you may return JSON response instead, e.g.

    public function userLoggingIn($usr, &$pwd)
    {
        $error["error"]["description"] = "Something Went Wrong at userLoggingIn";
        WriteJson($error);
        $this->terminate();
    }

Post Reply