Search code examples
salesforceapexsalesforce-lightningsalesforce-communitiesaura-framework

Unable to login Experience cloud site


Whenever I'm trying to login, I'm getting this error 'You do not have access to the Apex class named LightningLoginFormController'. I've made changes in Apex Class Security and added all the profiles, also checked by using 'with sharing' and 'without sharing', but it's still not working.

global class LightningLoginFormController {

    public LightningLoginFormController() {

    }

    @AuraEnabled
    public static String login(String username, String password, String startUrl) {
        try{
            ApexPages.PageReference lgn = Site.login(username, password, startUrl);
            aura.redirect(lgn);
            return null;
        }
        catch (Exception ex) {
            return ex.getMessage();            
        }
    }

    @AuraEnabled
    public static Boolean getIsUsernamePasswordEnabled() {
        Auth.AuthConfiguration authConfig = getAuthConfig();
        return authConfig.getUsernamePasswordEnabled();
    }

    @AuraEnabled
    public static Boolean getIsSelfRegistrationEnabled() {
        Auth.AuthConfiguration authConfig = getAuthConfig();
        return authConfig.getSelfRegistrationEnabled();
    }

    @AuraEnabled
    public static String getSelfRegistrationUrl() {
        Auth.AuthConfiguration authConfig = getAuthConfig();
        if (authConfig.getSelfRegistrationEnabled()) {
            return authConfig.getSelfRegistrationUrl();
        }
        return null;
    }

    @AuraEnabled
    public static String getForgotPasswordUrl() {
        Auth.AuthConfiguration authConfig = getAuthConfig();
        return authConfig.getForgotPasswordUrl();
    }

    @TestVisible
    private static Auth.AuthConfiguration getAuthConfig(){
        Id networkId = Network.getNetworkId();
        Auth.AuthConfiguration authConfig = new Auth.AuthConfiguration(networkId,'');
        return authConfig;
    }

    @AuraEnabled
    global static String setExperienceId(String expId) {
        // Return null if there is no error, else it will return the error message 
        try {
            if (expId != null) {
                Site.setExperienceId(expId);
            }
            return null; 
        } catch (Exception ex) {
            return ex.getMessage();            
        }
    }   
}

Solution

  • For this to work, you need to add the Apex Class in Community Portal User Profile i.e. the Profile using "Guest User License".

    Navigation: All sites --> Workspaces --> Administration --> Pages --> Go to Force.com --> Public Access Settings --> Enabled Apex Class Access.

    And if you have a custom function in your Apex Class that is using SOQL query, for that to fetch data you will need to use "without sharing" security settings!!