{"version":3,"file":"login.mjs","sources":["../../../../../../src/server/authenticate/login/login.ts"],"sourcesContent":["import {redirect} from 'react-router';\n\nimport {\n  BasicParams,\n  LoginError,\n  LoginErrorType,\n  AppDistribution,\n} from '../../types';\n\nexport function loginFactory(params: BasicParams) {\n  const {api, config, logger} = params;\n\n  return async function login(request: Request): Promise<LoginError | never> {\n    const url = new URL(request.url);\n    const shopParam = url.searchParams.get('shop');\n\n    if (request.method === 'GET' && !shopParam) {\n      return {};\n    }\n\n    const shop: string | null =\n      shopParam || ((await request.formData()).get('shop') as string);\n\n    if (!shop) {\n      logger.debug('Missing shop parameter', {shop});\n      return {shop: LoginErrorType.MissingShop};\n    }\n\n    const shopWithoutProtocol = shop\n      .replace(/^https?:\\/\\//, '')\n      .replace(/\\/$/, '');\n    const shopWithDomain =\n      shop?.indexOf('.') === -1\n        ? `${shopWithoutProtocol}.myshopify.com`\n        : shopWithoutProtocol;\n    const sanitizedShop = api.utils.sanitizeShop(shopWithDomain);\n\n    if (!sanitizedShop) {\n      logger.debug('Invalid shop parameter', {shop});\n      return {shop: LoginErrorType.InvalidShop};\n    }\n\n    const authPath = `${config.appUrl}${config.auth.path}?shop=${sanitizedShop}`;\n\n    const adminPath = api.utils.legacyUrlToShopAdminUrl(sanitizedShop);\n    const installPath = `https://${adminPath}/oauth/install?client_id=${config.apiKey}`;\n\n    const shouldInstall = config.distribution !== AppDistribution.ShopifyAdmin;\n    const redirectUrl = shouldInstall ? installPath : authPath;\n\n    logger.info(`Redirecting login request to ${redirectUrl}`, {\n      shop: sanitizedShop,\n    });\n\n    throw redirect(redirectUrl);\n  };\n}\n"],"names":[],"mappings":";;;AASM,SAAU,YAAY,CAAC,MAAmB,EAAA;IAC9C,MAAM,EAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAC,GAAG,MAAM;AAEpC,IAAA,OAAO,eAAe,KAAK,CAAC,OAAgB,EAAA;QAC1C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC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