8

I have built a custom login page for salesforce communities.I need to add additional conditions for authentication other than username and password.Where do i need to add additional conditions for authentication and custom login error messages ?

global with sharing class CustomLoginController {
global String username{get;set;}
global String password{get;set;}
global CustomLoginController () {}
global PageReference forwardToCustomAuthPage() {
    return new PageReference( '/CustomLogin');
}
global PageReference login() {
    return Site.login(username, password, null);
}

}

<apex:page docType=”html-5.0″ controller=”CustomLoginController” showHeader=”false” sidebar=”false” standardStylesheets=”false”>
<apex:stylesheet value=”{!URLFOR($Resource.bootstrap, ‘dist/css/bootstrap.min.css’)}”/>
<apex:includeScript value=”{!URLFOR($Resource.bootstrap, ‘dist/js/bootstrap.min.js’)}”/>
<div>
<apex:form styleClass=”form-horizontal”>
  <fieldset>
    <div id=”legend”>
      <legend class=”">Login</legend>
    </div>
    <div>
      <!– Username –>
      <apex:outputLabel value=”Username” styleClass=”col-lg-2 control-label” for=”username”/>
      <div>
          <apex:inputText value=”{!username}” id=”username” styleClass=”form-control” />
      </div>
    </div>
    <div>
      <!– Password–>
      <apex:outputLabel value=”Password” styleClass=”col-lg-2 control-label” for=”password”/>
      <div>
        <apex:inputSecret id=”password” value=”{!password}” styleClass=”form-control” />
      </div>
    </div>
    <div>
      <!– Button –>
      <div></div>
      <div>
        <apex:commandButton action=”{!login}” styleClass=”btn btn-success” value=”Login”/>
      </div>
    </div>
  </fieldset>
</apex:form>
</div>

I have implemented below answer.

  • What are you trying to validate? You could add some logic in your login() method that checks certain conditions, and unless it passes, it doesnt call the site.login, but rather adds errors to the page. – Chris Duncombe Nov 7 '13 at 17:05
  • I am trying to check the user's Contact is associated with an Account that has an active license and if the license is not active show error message on custom login page – sfdc Nov 7 '13 at 17:39
  • Think Keith has nailed it in his answer below. Exactly the type of thing I was thinking – Chris Duncombe Nov 7 '13 at 17:43
  • @ SF_Ninja ..Its not displaying any errors for me when I followed the below answer – sfdc Nov 7 '13 at 19:18
  • @SF_Ninja Can you please look into this – sfdc Nov 8 '13 at 20:44
6

I suggest code like that below, where once the password verified login has succeeded you go on to check further factors (that you have added as fields to your login page):

global with sharing class CustomLoginController {

    global String username{get;set;}
    global String password{get;set;}
    global String other1{get;set;}
    global String other2{get;set;}

    global PageReference login() {
        PageReference pr = Site.login(username, password, '/FirstPageAfterLogin');
        if (pr != null && pr.getUrl() == '/FirstPageAfterLogin') {
           // Add custom fields to User for other factors or query further to get them
           User u  = [select Other1__c, Other2__c from User where UserName = :username];
           if (u.Other1__c != other1) {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ‘Other 1 mismatch’));
           }
           if (u.Other2__c != other2) {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ‘Other 2 mismatch’));
           }
           if (ApexPages.hasMessages()) {
               // Redisplay login page with errors
               return null;
           }
        }
        return pr;
    }
}
  • Its not displaying any error messages for me. – sfdc Nov 7 '13 at 19:14
  • If you haven't added an <apex:pageMessages/> element to your page that is probably the cause. If you have then I do not know what the problem is. – Keith C Nov 7 '13 at 19:32
  • <apex:pageMessages/> is already added to the custom login page still no error messages – sfdc Nov 7 '13 at 19:33
  • Suggest you compare what happens with say an invalid password (that Site.login should report in your page) compared to what happens for an error generated by your own code. And as usual add a pile of System.debug statements. – Keith C Nov 7 '13 at 19:51
  • debug logs are not showing up for my custom login page where debugs logs shows up for home page ..when there some invalid password..i get standard salesforce error Error:Your login attempt has failed. Make sure the username and password are correct. – sfdc Nov 7 '13 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.