Loading
Experience Cloud
Customize the Apex Login Controller

Customize the Apex Login Controller

Salesforce provides an easy way to customize your login process using the Login Discovery Page, which lets you implement passwordless login. However, if you want even more control over your login process, you can modify the Apex login controllers and use customized Visualforce pages.

Required Editions

Available in: Salesforce Classic and Lightning Experience
Available in: Enterprise, Performance, Unlimited, and Developer Editions
User Permissions Needed
To create and edit Visualforce pages: Customize Application
To edit Apex classes:

Author Apex

AND

Customize Application

You can use these steps to customize the login process for Default, Experience Builder, and Visualforce Pages. If you're using the Login Discovery Page to implement passwordless login programmatically, see Email or Phone Number Registration with Passwordless Login.

For more information on Login Pages, see Login Page Types.

  1. From Setup, in the Quick Find box, enter Apex Classes, and then select Apex Classes.
  2. Next to CommunitiesLoginController, click Edit.
  3. Add the following code.
    global PageReference forwardToCustomAuthPage() {
            String startUrl = System.currentPageReference().getParameters().get('startURL');
            return new PageReference(Site.getPathPrefix() + '/SiteLogin?startURL=' + EncodingUtil.urlEncode(startURL, 'UTF-8'));
        }
  4. If you created a custom login page, replace SiteLogin with the name of your Visualforce page.
  5. Click Save.
  6. Click Edit next to CommunitiesLandingController.
  7. Add this code.
    public PageReference forwardToCustomAuthPage() {
            String startUrl = System.currentPageReference().getParameters().get('startURL');
            return new PageReference(Site.getPathPrefix() + '/SiteLogin?startURL=' + EncodingUtil.urlEncode(startURL, 'UTF-8'));
        }
  8. If you created a custom login page, replace SiteLogin with the name of your Visualforce page.
  9. Click Save.
  10. From Setup, enter Visualforce Pages in the Quick Find box, then select Visualforce Pages.
  11. Click Edit next to CommunitiesLogin.
  12. In the first line of code, add the following:
    action="{!forwardToCustomAuthPage}"
  13. Click Save.
  14. Click Edit next to CommunitiesLanding.
  15. In the first line of code, add the following:
    action="{!forwardToCustomAuthPage}"
  16. Click Save.
 
Loading
Salesforce Help | Article