[SalesForce] Create a custom login failure message using Site.Login

We are having a force.com site and we are currently using a site.login method to authenticate the user. The Site.Login returns a page reference which tells us whether the login is successful or not. I am trying to override the default salesforce login failure message with our own and it does not work.

PageReference pf =  Site.login(viewstatecontroller.userID_str, 
                               viewstatecontroller.password_str,landingUrl);
If(pf == null)
    add custom apex message
else
    return pf;

Now if i do this, the login never works successfully and i am forced to do
a return on the Site.Login. I tried to remove the default apex message and add our own and we don't have remove message on the ApexMessage. How can we put our own error message on the login failure on a force.com site? Does any body know how to do this?
Thanks
Buyan

Best Answer

I have faced the same problem. Not sure why it's not working. But I was able to overcome that by creating a pageMessage in VF page as follows:

<apex:pageMessage severity="error" summary="Login Failed.Check the userName or Password" rendered="{!isLoginFailed}"/>

<apex:commandbutton value="Login" action="{!login}"/>

In Controller:

public Boolean isLoginFailed {get; set;}
public String password { get; set; }
public String username { get; set; }

public PageReference login()
{
   PageReference ref=Site.login(username,password,'/apex/sample');
   if(ref==null)
   {
    isLoginFailed=true;
    return null;
   }
   return ref;      
}

Hopefully, this works for you.

If I use rerender attribute on commandbutton, this again does not work.

Related Topic