[SalesForce] not able to login to community with custom login page

I have created a community and i have somewhat modified my community login page which on proper credentials should redirect me to community login page.

But i am not able to make my login work.I read something about site.login function and i tried to implement that but it doesn't work..i tried with simple authentication also and it also didn't help me.

It just that this is my first time on community..So hope u guys can help me here even if i m being silly.

Apex code :

global with sharing class CommunitiesLoginController {

    Public string userName {get;set;}
    public string passWord {get;set;}

    global CommunitiesLoginController () {


    }

    // Code we will invoke on page load.
   global PageReference forwardToAuthPage() {
        /*String startUrl = System.currentPageReference().getParameters().get('startURL');
        String displayType = System.currentPageReference().getParameters().get('display');
        return Network.forwardToAuthPage(startUrl, displayType); */
        return null;

       // PageReference objPageReference = new PageReference('/CommunitiesLanding');
       // return objPageReference;

    }

    public PageReference Login() {


        PageReference PageRef=Site.login(username, password, null);
        if(PageRef!=null)
        {
            system.debug('in if--->');
            PageReference PortalLogin=page.CommunitiesLanding;
            return PortalLogin;
        }else
        {
             system.debug('not in if--->');
            return null;
        }
         /* 
        if(Site.login(userName , passWord ,'') != null){
            return Site.login(userName ,passWord , '/CommunitiesLanding');
        }
        else{
           return Site.login(username, password, null);
        }

      if(username =='samir' && password == 'samir'){
            PageReference objPageReference = new PageReference('/CommunitiesLanding');
            return objPageReference ;
        }
        else {
             PageReference objPageReference = new PageReference('/CommunitiesLanding');
            return objPageReference ;
        }*/
    }

}

my vf page :

<apex:page id="loginPage" controller="CommunitiesLoginController"   title="{!$Label.site.site_login}">
<apex:form >

        <apex:panelGrid columns="5">
            <apex:pageBlock >
              <apex:pageBlockButtons >
                        <apex:commandButton value="Login" action="{!Login}" style="width:90px" />
               </apex:pageBlockButtons>
                <apex:pageBlockSection >
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="UserName"></apex:outputLabel>
                            <apex:inputText id="username" title="username" value="{!userName }"/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Password"></apex:outputLabel>
                            <apex:inputText id="Password" title="Password" value="{!passWord }"/>
                        </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:pageBlock>
            </apex:panelGrid>
        </apex:form>
</apex:page>

there is lot of commented code in controller, its because i was trying different things

Thanks in advance guys

Best Answer

What do you mean when you say it doesn't work? What results are you getting exactly?

You should be fine simply with:

public PageReference Login() {
  return Site.login(username, password, null);
}

This will forward successful login attempts to the page you define in the community setup page:

Setup > Communities > [Community Name] > Manage > Administration > Pages > Community Home

Unsuccessful login attempts will cause Site.login() to return null. Therefore you can check for this and, if null, display an error message to the user.

For more information, see the "Customize Your Community’s Login Experience" section of the Getting Started With Communities guide.

Related Topic