UNPKG

@commercetools-frontend/mc-dev-authentication

Version:

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

53 lines (45 loc) 2.52 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/stringify'); var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/starts-with'); var _URL = require('@babel/runtime-corejs3/core-js-stable/url'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var _JSON$stringify__default = /*#__PURE__*/_interopDefault(_JSON$stringify); var _startsWithInstanceProperty__default = /*#__PURE__*/_interopDefault(_startsWithInstanceProperty); var _URL__default = /*#__PURE__*/_interopDefault(_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__default["default"]({ 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__default["default"](_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__default["default"](request.originalUrl, trimTrailingSlash(applicationConfig.env.mcApiUrl)); response.writeHead(301, { Location: redirectTo.toString() }).end(); return; } } } } next(); }; } const transformerLocal = () => { // noop }; exports.createMcDevAuthenticationMiddleware = createMcDevAuthenticationMiddleware; exports.transformerLocal = transformerLocal;