Print this page

Code implementation to consider when using custom Login / Landing and authentication pages for Salesforce1 communities

Knowledge Article Number 000220257
Description always recommends using the standard login and landing pages provided for use of 
Communities via mobile devices (through Salesforce1). If you choose to pursue a more custom login 
environment, however, here are some important aspects to consider when implementing the Login and 
authentication portions.

NOTE to Admins: If your Site Details page has " Active Site Home Page " set to non-standard page, then likely these are custom landing and controller pages handling the login for Salesforce1 communities (affects both hybrid and browser UI). The reason being, the page needs to confirm if the user is on a mobile device.  It is against best practice to edit the standard Communities Apex or Visualforce pages.  Changes such as editing the action=VF page or the StartUrl in the Apex Class: CommunitiesLoginController.  If changes need to be made, please build a new Visualforce page/Apex Class to handle this functionality.  

An Admin writes a custom landing controller or edits the existing controllers (CommunitiesLandingController OR CommunitiesLoginController), but is not seeing the correct landing page for mobile users.   

Step 1:
To avoid the issue, the controller apex class that gets the landing page after login should have the following code added:
    public PageReference forwardToStartPage() {
        return Network.communitiesLanding();

Step 2:
In the custom VisualForce page that handles the communities landing, in the first line of code, add the following:


This way, if the user is a Salesforce1 user, on a mobile device the user would land on the version of the community. If the user is not a Salesforce1 user and/or on a desktop browser, the user would land on the home tab of the community with full VisualForce UI.

// Code to invoke on page load.
    public PageReference forwardToCustomAuthPage() {
         String userAgent = System.currentPageReference().getHeaders().get('User-Agent');
         String device = System.currentPageReference().getParameters().get('device');
        if(UserInfo.getUserType() == 'Guest'){
            return new PageReference('/{CustomLoginPage}');
         isIPhone = (userAgent.contains('iPhone') || device == 'mobile');
         isIPad = (userAgent.contains('iPad') && (userAgent.contains('AppleWebKit') && device != 

         System.debug('homeStartPage' + (isIPhone || isIPad));
         if(isIPhone || isIPad){
             //return new PageReference('/one/');
             return null;

        User me = [select Id, Name, Accepted_Terms__c from User where Id = :UserInfo.getUserId()];
        if( me.Accepted_Terms__c == null ){
            PageReference p = new PageReference('/CompleteProfile?

            return p;

        return null;

promote demote