0

I have made a custom LWC component for login and put it in the Experience Builder Site Login Page. Now After I putting the username and password in that and invoking my Apex controller it is not redirecting to the home page after a successful login.

@AuraEnabled
public static String Sitelogin(String username, String password, String startUrl) {
   ApexPages.PageReference lgn = Site.login(username, password, startUrl);
   lgn.setRedirect(true);
   //aura.redirect(lgn);
   return null;
}

I have checked the debug and it is giving me a very long URL so when I copy pasted that URL in the browser window then it is allowed me to logging as the User but still not redirecting to that URL.

2
  • The examples suggest you should be returning the PageReference, but you return null.
    – Phil W
    Jul 14 at 11:10
  • but in this example it is not taking any username and password to validate. Then according to this example how will it going to validate the user credentials?
    – H John
    Jul 14 at 11:18
0

After searching on many websites and blogs, I found there are some issues when redirecting from Login to home page using LWC. So, here is the working solution..

@AuraEnabled
public static String Sitelogin(String username, String password, String startUrl) {
   ApexPages.PageReference lgn = Site.login(username, password, startUrl);
   
   return lgn.getUrl();
}

//In LWC

afterLogin( {username: uName, password: pwd, startUrl: starturl} )
        .then(result => {
            window.location.href = result;
        }
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.