[SalesForce] Authenticate Users on Salesforce Communities

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.

Best Answer

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;
    }
}
Related Topic