avatar
handle redirect login page in AEM AEM

> Direction 1:

> Login.ts
  > flatick_service.ts
    > createLoggedCookies() = (jwtToken: string, userID: string)
    {
        cookieUtils.setCookie(JWT_TOKEN_FLAGTICK, jwtToken);
        cookieUtils.setCookie(USER_ID_FLAGTICK, userID);
    }
    > isAuthenticated() = (callback: LoggedInCallBack): void => {
         /** Be assumption that you use Cognito or Firebase for Authentication*/
        const cognitoUser = CognitoUtilInstance.getCurrentUser();
        or const firebaeUser = FirebaseAuth.getCurrentUser();
    }
> Navigation.ts => Call Component (using init() to check isAuthentcated() before calling APIs)
> Profile.ts => Call Component (using init() to check isAuthentcated() before calling APIs)

> Direction 2:

/** Apply for API. Be assumption that you want to redirect to Login URL, this URL can't be hard code from JS - Front-end. Need authoring it from one of AEM component will be triggered everything. */
  > Navigation.ts => navigation.html => NavigationModel.java => loginURL (can be authoring)
  > Navigation.ts
    > flagtick_service.ts
      export interface LoggedInCallback {
          isLoggedIn(message: string, loggedIn: boolean): void;
      }

      LoginServiceInstance.isAuthenticated(this);
      
      isLoggedIn = (message: string, isLoggedIn: boolean): void => {
         if (isLoggedIn) {
            const jwtToken = cookieUtils.getCookie(JWT_TOKEN_FLAGTICK);
            const userID = cookieUtils.getCookie(USER_ID_FLAGTICK);
            if (!jwtToken || !userID) {
                LoginServiceInstance.logout(this.loginPage);
            } else {
                this.getUserData();
            }
            this.inactivityEvents();
         } else {
            LoginServiceInstance.removeCookies();
            if (this.loginPage && this.loginPage !== '/') {
               window.location.href = this.loginPage;
            }
         }
      });

/** References */
inactivityEvents = (): void => {
  window.onload = this.inactivityTime;
  window.onmousemove = this.inactivityTime;
  window.onmousedown = this.inactivityTime;
  window.ontouchstart = this.inactivityTime;
  window.onclick = this.inactivityTime;
  window.onkeydown = this.inactivityTime;
  window.addEventListener('scroll', this.inactivityTime, true);
};

You need to login to do this manipulation!