Access Lightning Apps in Public Communities using ltng:allowGuestAccess Interface

Let us discuss here how to set up a Lightning Out dependency app to make it available to users without requiring them to authenticate with Salesforce. Add the ltng:allowGuestAccess interface to your Lightning Out dependency app to make it available to users without requiring them to authenticate with Salesforce.  A Lightning Out dependency app with the ltng:allowGuestAccess interface can be used with Lightning Components for Visualforce and with Lightning Out.

  • Using Lightning Components for Visualforce, you can add your Lightning app to a Visualforce page, and then use that page in Salesforce Tabs + Visualforce communities. Then you can allow public access to that page.
  • Using Lightning Out, you can deploy your Lightning app anywhere Lightning Out is supported—which is almost anywhere!

1.Create Lightning Component 

Now let us create a simple lightning component and controller that we will use it with guest access. In this component, we will get all the accounts and show them to the guest user.

Apex Class

Component 

2. Create a Lightning App 

Now create a Lightning app for public sites. we will be calling this lightning app from the visualforce page. Now your application needs to ltng:allowGuestAccess interface to your Lightning Out dependency app. Here is the lightning app.

 

3.Create a Community 

The ltng:allowGuestAccess interface is only usable in orgs that have Communities enabled, and your Lightning Out app is associated with all community endpoints that you’ve defined in your Org. When you add the ltng:allowGuestAccess interface to a Lightning app, the app is open to guest users and is available through every community in your org, whether that community is enabled for public access or not. You can’t prevent it from being accessible via community URLs, and you can’t make it available for some communities but not others.

4.Create a visualforce page

Now we will create a visualforce page that will be accessible by the guest user. here we will be using the  Salesforce Tabs + Visualforce communities with guest access.

With Lightning Out, we need to add the  <script> tag that references the library directly, using a community endpoint URL as shown below.

Add the JavaScript code to load and activate your Lightning app. This code is standard Lightning Out, with the important addition that you must use one of your org’s community URLs for the endpoint.

5. Add the page to the community 

Now go and add the visualforce page to the community and make sure you have set up guest user access so that we will be able to see this page without login into the system. You can able to see the page as shown below