UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

54 lines (53 loc) 2.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../domain/constants"); const cookie_util_1 = __importDefault(require("./cookie-util")); class AuthUtil { /** * Generates the URL to redirect to after login. If returnUrl is provided, sets the RETURN_URL cookie with the returnUrl and redirects to the login page. * @param origin The origin of the application * @param loginUrl The URL of the login page * @param returnUrl The url to redirect to after login * @returns The url to redirect to */ static getPostAuthUrl(origin, loginUrl, returnUrl = constants_1.Defaults.String.Value) { if (returnUrl) { cookie_util_1.default.setCookie(constants_1.Key.Cookie.RETURN_URL, returnUrl); } return `${origin}${loginUrl}`; } /** * Generates the login URL for the user. If returnUrl is provided, sets the RETURN_URL cookie with the returnUrl and redirects to the login page. * @param origin The origin of the application * @param orgId The organization ID * @param domainId The domain ID * @param returnUrl The url to redirect to after login * @returns The url to redirect to */ static getLoginUrl(origin, orgId = constants_1.Defaults.String.Value, domainId = constants_1.Defaults.String.Value, returnUrl = constants_1.Defaults.String.Value, search = constants_1.Defaults.String.Value) { if (returnUrl) { cookie_util_1.default.setCookie(constants_1.Key.Cookie.RETURN_URL, returnUrl); } const extraQuery = search ? ((search === null || search === void 0 ? void 0 : search.startsWith('?')) ? search === null || search === void 0 ? void 0 : search.substring(1) : search) : constants_1.Defaults.String.Value; let url = `${process.env.AUTH_BASE_URL}?appUrl=${encodeURI(origin)}&appCode=${process.env.SSO_AUTH_MODULE}&source=external${extraQuery ? `&returnUrl=${encodeURIComponent(extraQuery)}` : constants_1.Defaults.String.Value}`; if (orgId) { url = `${url}&orgId=${orgId}`; if (domainId) { url = `${url}&domainId=${domainId}`; } } return url; } /** * Generates the logout URL for the user. * @param origin The origin of the application * @returns The url to redirect to */ static getLogoutUrl(origin) { return `${process.env.AUTH_BASE_URL}/Account/Logout?appUrl=${origin}&appCode=${process.env.SSO_AUTH_MODULE}&source=external`; } } exports.default = AuthUtil;