0

We have a custom LWC Login page under an experience builder site, which uses a custom apex controller and site.login method to authenticate the user.

Apex Authentication code :

ApexPages.PageReference pageRef = Site.login(username, password, startUrl);

        if(pageRef != null){
            system.debug(pageRef);
         return pageRef.getUrl();
        }

After the user is authenticated, pageRef is sent back to LWC as string with session.

In LWC, I need to navigate user to home page after logging in, but navigationMixin is opening the URL is new tab instead of refreshing current page.

LWC Code to Navigate:

navigateToWebPage(url) {
    this[NavigationMixin.Navigate]({
        "type": "standard__webPage",
        "attributes": {
            "url": url
        }
    });
}

I also tried window.location.href but that does not navigate the user at all.

How do I redirect the user to the home page after they are authenticated with session in Experience cloud LWC?

2 Answers 2

0

If your LWC is headless then you can use invoke() method like below

@api invoke(){
  window.location.href = url   //your url value
}
2
  • Navigating the user to the homepage after login does not sound headless to me. So while this may be an answer to a question, I don't think that this answers this question.
    – Derek F
    Jan 5 at 15:10
  • @DerekF correct, this won't work. I tried navigationMixin as well but that always opens the page in new tab and doesn't redirect user. Jan 5 at 16:43
0

There are different types of pages in Salesforce.

  • recordPage - standard__recordPage
  • objectPage - standard__objectPage
  • for URL - standard__webPage home page, chatter or any standard/custom pages - standard__namedPage
  • and many more...

Please update your code with below

this[NavigationMixin.Navigate]({
            type: 'standard__namedPage',
            attributes: {
                pageName: 'home' // add apiname of page
            },
        });

enter image description here

Refer below article for more information - https://mukulmahawariya11.medium.com/navigation-in-salesforce-communities-ad6838488166

3
  • Thank you, I tried but it gives error a preNavigate hook listener blocked and as this is a page within experience cloud, I added comm__namedPage instead of standard. Jan 5 at 16:42
  • can you tell did it worked or not? Jan 6 at 5:27
  • it did not, as I mentioned it gives that hook listener error and it cannot be standard since the Home page is within community. Jan 6 at 15:01

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.