@shopify/shopify-app-express
Version:
Shopify Express Middleware - to simplify the building of Shopify Apps with Express
40 lines (32 loc) • 1.11 kB
text/typescript
import {Request, Response} from 'express';
import {ApiAndConfigParams} from '../types';
import {RedirectToShopifyOrAppRootMiddleware} from './types';
export function redirectToShopifyOrAppRoot({
api,
config,
}: ApiAndConfigParams): RedirectToShopifyOrAppRootMiddleware {
return function () {
return async function (req: Request, res: Response) {
if (res.headersSent) {
config.logger.info(
'Response headers have already been sent, skipping redirection to host',
{shop: res.locals.shopify?.session?.shop},
);
return;
}
const host = api.utils.sanitizeHost(req.query.host as string)!;
const redirectUrl = api.config.isEmbeddedApp
? await api.auth.getEmbeddedAppUrl({
rawRequest: req,
rawResponse: res,
})
: `/?shop=${res.locals.shopify.session.shop}&host=${encodeURIComponent(
host,
)}`;
config.logger.debug(`Redirecting to host at ${redirectUrl}`, {
shop: res.locals.shopify.session.shop,
});
res.redirect(redirectUrl);
};
};
}