UNPKG

@commercetools-frontend/mc-dev-authentication

Version:

Authentication views when running webpack-dev-server in development mode

42 lines (38 loc) 1.99 kB
import _JSON$stringify from '@babel/runtime-corejs3/core-js-stable/json/stringify'; import _startsWithInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/starts-with'; import _URL from '@babel/runtime-corejs3/core-js-stable/url'; const trimTrailingSlash = value => value.replace(/\/$/, ''); function createMcDevAuthenticationMiddleware(applicationConfig) { const isDevAuthenticationMiddlewareDisabled = String(applicationConfig.env.disableAuthRoutesOfDevServer) === 'true' || applicationConfig.env.servedByProxy; return (request, response, next) => { if (request.originalUrl === '/api/graphql') { response.statusCode = 404; response.setHeader('Content-Type', 'application/json'); response.end(_JSON$stringify({ message: `This GraphQL endpoint is only available in production in the [Merchant Center Proxy Router](https://docs.commercetools.com/merchant-center-customizations/concepts/merchant-center-proxy-router). Please check that you are not calling this endpoint in development mode.` })); return; } if (applicationConfig.env.__DEVELOPMENT__?.oidc?.authorizeUrl) { var _context; // Handle login page for OIDC workflow when developing against a local MC API. if (_startsWithInstanceProperty(_context = applicationConfig.env.__DEVELOPMENT__.oidc.authorizeUrl).call(_context, 'http://localhost')) { if (request.originalUrl?.startsWith('/login/authorize')) { if (!isDevAuthenticationMiddlewareDisabled) { // Redirect to the MC API to initiate the authorize flow. const redirectTo = new _URL(request.originalUrl, trimTrailingSlash(applicationConfig.env.mcApiUrl)); response.writeHead(301, { Location: redirectTo.toString() }).end(); return; } } } } next(); }; } const transformerLocal = () => { // noop }; export { createMcDevAuthenticationMiddleware, transformerLocal };