UNPKG

@shopify/shopify-app-remix

Version:

Shopify Remix - to simplify the building of Shopify Apps with Remix

54 lines (40 loc) 1.69 kB
import {redirect} from '@remix-run/server-runtime'; import {BasicParams, LoginError, LoginErrorType} from '../../types'; export function loginFactory(params: BasicParams) { const {api, config, logger} = params; return async function login(request: Request): Promise<LoginError | never> { const url = new URL(request.url); const shopParam = url.searchParams.get('shop'); if (request.method === 'GET' && !shopParam) { return {}; } const shop: string | null = shopParam || ((await request.formData()).get('shop') as string); if (!shop) { logger.debug('Missing shop parameter', {shop}); return {shop: LoginErrorType.MissingShop}; } const shopWithoutProtocol = shop .replace(/^https?:\/\//, '') .replace(/\/$/, ''); const shopWithDomain = shop?.indexOf('.') === -1 ? `${shopWithoutProtocol}.myshopify.com` : shopWithoutProtocol; const sanitizedShop = api.utils.sanitizeShop(shopWithDomain); if (!sanitizedShop) { logger.debug('Invalid shop parameter', {shop}); return {shop: LoginErrorType.InvalidShop}; } const authPath = `${config.appUrl}${config.auth.path}?shop=${sanitizedShop}`; const adminPath = api.utils.legacyUrlToShopAdminUrl(sanitizedShop); const installPath = `https://${adminPath}/oauth/install?client_id=${config.apiKey}`; const shouldInstall = config.isEmbeddedApp && config.future.unstable_newEmbeddedAuthStrategy; const redirectUrl = shouldInstall ? installPath : authPath; logger.info(`Redirecting login request to ${redirectUrl}`, { shop: sanitizedShop, }); throw redirect(redirectUrl); }; }