Expand my Community achievements bar.

SOLVED

Handle authoring of pages on Author Instance

Avatar

Community Advisor

Hello,

We are in a situation and maybe it's a common problem in AEM Project.

 

So we have specific pages which require user login to be accessed such as payment-summary, wishlist etc.
The project is AEM SPA and this condition is written on React components, to redirect to login if user is not logged in when try to access these protected pages.

 

Now, same code is available on Author instance pages as well and it's making a redirection and unable to author the page.

How to resolve this issue and Author should be able to author the component on Author instance?

 

 

@kautuk_sahni @arunpatidar @aanchal-sikka 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @iamnjain ,

If my understanding is correct. You need to bypass the login redirection on the author instance and implement on the publish instance. 
Maybe you can try to implement the logic on react side using any approach mentioned. 
1. Conditional Redirects : 

// Check if the environment is Author or Publish
const isAuthorEnvironment = window.location.hostname.includes('author-instance');

// Redirect only if not logged in and in the Publish environment
if (!isAuthorEnvironment && !isLoggedIn()) {
  window.location.href = '/login'; // Redirect to login page
}

 

2. Excluding Redirects in Author Mode:

// Assuming a flag or environment variable distinguishes Author and Publish
const isAuthorEnvironment = process.env.NODE_ENV === 'development';

if (!isAuthorEnvironment && !isLoggedIn()) {
  window.location.href = '/login'; // Redirect only in the Publish environment
}

View solution in original post

3 Replies

Avatar

Correct answer by
Level 6

Hi @iamnjain ,

If my understanding is correct. You need to bypass the login redirection on the author instance and implement on the publish instance. 
Maybe you can try to implement the logic on react side using any approach mentioned. 
1. Conditional Redirects : 

// Check if the environment is Author or Publish
const isAuthorEnvironment = window.location.hostname.includes('author-instance');

// Redirect only if not logged in and in the Publish environment
if (!isAuthorEnvironment && !isLoggedIn()) {
  window.location.href = '/login'; // Redirect to login page
}

 

2. Excluding Redirects in Author Mode:

// Assuming a flag or environment variable distinguishes Author and Publish
const isAuthorEnvironment = process.env.NODE_ENV === 'development';

if (!isAuthorEnvironment && !isLoggedIn()) {
  window.location.href = '/login'; // Redirect only in the Publish environment
}

Avatar

Community Advisor

Hi @iamnjain 
You can add a class in your page if you are on author mode

 

<sly data-sly-test.isAuthorMode="${wcmmode.edit || wcmmode.design}"/>
<div class="c-ac-container__default-parsys ${isAuthorMode ? 'author-page' : ''}">
....

 
or you can use wcmmode cookies to identify if this is author instance or publish



Arun Patidar

Avatar

Community Advisor

@arunpatidar 

The first suggestion I thought about it. But, since this is AEM SPA, we don't have HTL files for components, everything is controlled at FE component.

In your second suggestion, I checked the browser cookie, we have wcmmode=edit. This might work. Let me try with this.

I will update here.