Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev-auth): request / response compatibility #3178

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/yellow-moose-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-frontend/mc-dev-authentication': patch
'@commercetools-frontend/mc-scripts': patch
---

Adjust the dev auth server compat with HTTP request / response options
4 changes: 2 additions & 2 deletions packages/mc-dev-authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
},
"devDependencies": {
"@commercetools-frontend/application-config": "workspace:*",
"@types/express": "^4.17.17",
"@types/connect": "^3.4.35",
"@tsconfig/node16": "^16.0.0",
"express": "4.18.2"
"connect": "^3.7.0"
},
"engines": {
"node": "16.x || >=18.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Request, Response, NextFunction } from 'express';
import type { ServerResponse } from 'node:http';
import type { IncomingMessage, NextFunction } from 'connect';
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros
import pages from /* preval */ './pages';
import { logout } from './routes';
import type { TCustomApplicationRuntimeConfig } from './types';
// https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros

const trimTrailingSlash = (value: string) => value.replace(/\/$/, '');

Expand All @@ -19,11 +20,15 @@ function createMcDevAuthenticationMiddleware(
String(applicationConfig.env.disableAuthRoutesOfDevServer) === 'true' ||
applicationConfig.env.servedByProxy;

return (request: Request, response: Response, next: NextFunction) => {
return (
request: IncomingMessage,
response: ServerResponse,
next: NextFunction
) => {
if (request.originalUrl === '/api/graphql') {
response.statusCode = 404;
response.setHeader('Content-Type', 'application/json');
response.send(
response.end(
JSON.stringify({
message: `This GraphQL endpoint is only available in production in the [Merchant Center Proxy Router](https://docs.commercetools.com/custom-applications/concepts/merchant-center-proxy-router). Please check that you are not calling this endpoint in development mode.`,
})
Expand All @@ -38,11 +43,11 @@ function createMcDevAuthenticationMiddleware(
'http://localhost'
)
) {
if (request.originalUrl.startsWith('/login/authorize')) {
if (request.originalUrl?.startsWith('/login/authorize')) {
if (isDevAuthenticationMiddlewareDisabled) {
next();
} else {
response.send(htmlLogin);
response.end(htmlLogin);
}
return;
}
Expand All @@ -52,7 +57,7 @@ function createMcDevAuthenticationMiddleware(
if (isDevAuthenticationMiddlewareDisabled) {
next();
} else {
response.send(htmlLogin);
response.end(htmlLogin);
}
return;
}
Expand All @@ -62,7 +67,7 @@ function createMcDevAuthenticationMiddleware(
if (isDevAuthenticationMiddlewareDisabled) {
next();
} else {
response.send(htmlLogout);
response.end(htmlLogout);
}
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/mc-dev-authentication/src/routes/logout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Response } from 'express';
import type { ServerResponse } from 'node:http';

function logoutRoute(
response: Response,
response: ServerResponse,
additionalCookieParameters: string[] = []
) {
// NOTE: removing the cookie only works if your are running the MC API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, Connect } from 'vite';
import type { Plugin } from 'vite';
import type { ApplicationRuntimeConfig } from '@commercetools-frontend/application-config';
import { createMcDevAuthenticationMiddleware } from '@commercetools-frontend/mc-dev-authentication';
import {
Expand Down Expand Up @@ -27,9 +27,7 @@ const vitePluginCustomApplication = (

// Handle auth routes for internal local development.
server.middlewares.use(
createMcDevAuthenticationMiddleware(
applicationConfig
) as Connect.NextHandleFunction
createMcDevAuthenticationMiddleware(applicationConfig)
);
};
},
Expand Down
46 changes: 40 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading