Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

3.0 KB
Raw Blame
import { frontendURL } from 'dashboard/helper/URLHelper';
import { clearBrowserSessionCookies } from 'dashboard/store/utils/api';
import { getShopifyBillingRedirect, hasAuthCookie } from './AuthHelper';
import { DEFAULT_REDIRECT_URL } from 'dashboard/constants/globals';
import { replaceRouteWithReload } from './CommonHelper';

const validateSSOLoginParams = to => {
  const isLoginRoute = to.name === 'login';
  const { email, sso_auth_token: ssoAuthToken } = to.query || {};
  const hasValidSSOParams = email && ssoAuthToken;
  return isLoginRoute && hasValidSSOParams;
};

export const validateRouteAccess = (to, next, chatwootConfig = {}) => {
  // Pages with ignoreSession:true would be rendered
  // even if there is an active session
  // Used for confirmation or password reset pages
  if (to.meta && to.meta.ignoreSession) {
    next();
    return;
  }

  if (validateSSOLoginParams(to)) {
    clearBrowserSessionCookies();
    next();
    return;
  }

  const pendingInstallToken = to.query?.shopify_pending_install;
  const isPendingShopifySignup =
    to.name === 'auth_signup' &&
    typeof pendingInstallToken === 'string' &&
    /^[0-9a-f]{32}$/.test(pendingInstallToken);

  // Redirect to dashboard if a cookie is present, the cookie
  // cleanup and token validation happens in the application pack.
  if (hasAuthCookie()) {
    const { redirect_url: requestedRedirectUrl } = to.query || {};
    if (isPendingShopifySignup) {
      replaceRouteWithReload(
        frontendURL(
          `shopify/select-account?shopify_pending_install=${pendingInstallToken}`
        )
      );
      return;
    }

    const redirectUrl =
      requestedRedirectUrl || getShopifyBillingRedirect(to.query);
    const redirectTarget = redirectUrl
      ? `${DEFAULT_REDIRECT_URL}?redirect_url=${encodeURIComponent(redirectUrl)}`
      : DEFAULT_REDIRECT_URL;
    const accountId = to.query?.sso_account_id;
    const accountQuery = accountId
      ? `${redirectUrl ? '&' : '?'}sso_account_id=${encodeURIComponent(accountId)}`
      : '';
    replaceRouteWithReload(`${redirectTarget}${accountQuery}`);
    return;
  }

  // If the URL is an invalid path, redirect to login page
  // Disable navigation to signup page if signups are disabled
  // Signup route has an attribute (requireSignupEnabled) in it's definition
  const isAnInalidSignupNavigation =
    !isPendingShopifySignup &&
    chatwootConfig.signupEnabled !== 'true' &&
    to.meta &&
    to.meta.requireSignupEnabled;

  // Disable navigation to SAML login if enterprise is not enabled
  // SAML route has an attribute (requireEnterprise) in it's definition
  const isEnterpriseOnlyPath =
    chatwootConfig.isEnterprise !== 'true' &&
    to.meta &&
    to.meta.requireEnterprise;

  if (!to.name || isAnInalidSignupNavigation || isEnterpriseOnlyPath) {
    next(frontendURL('login'));
    return;
  }

  next();
};

export const isOnOnboardingView = route => {
  const { name = '' } = route || {};

  if (!name) {
    return false;
  }

  return name.includes('onboarding_');
};