Skip to content

Commit 6233060

Browse files
fix: web middleware fetch TLS issues (#588)
1 parent ca84b79 commit 6233060

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

apps/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "PORT=3000 next dev --turbo",
88
"build": "next build && cp -r .next/static .next/standalone/apps/web/.next",
9-
"start": "node .next/standalone/apps/web/server.js",
9+
"start": "HOSTNAME=0.0.0.0 node .next/standalone/apps/web/server.js",
1010
"check": "tsc --noEmit && next lint"
1111
},
1212
"dependencies": {

apps/web/src/lib/middleware-utils.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import 'server-only';
22
import { cookies, headers as nextHeaders } from 'next/headers';
33

4-
function getBaseUrl() {
4+
function getPlatformUrl() {
55
if (process.env.NEXT_PUBLIC_PLATFORM_URL)
66
return process.env.NEXT_PUBLIC_PLATFORM_URL;
77
throw new Error('NEXT_PUBLIC_PLATFORM_URL is not set');
88
}
99

10+
function clientHeaders() {
11+
const allHeaders = nextHeaders();
12+
const clientHeaders = new Headers();
13+
const notAllowedHeaders = ['host', 'origin', 'referer'];
14+
allHeaders.forEach((value, key) => {
15+
if (notAllowedHeaders.includes(key)) return;
16+
clientHeaders.append(key, value);
17+
});
18+
return clientHeaders;
19+
}
20+
1021
export async function getAuthRedirection() {
1122
if (!cookies().has('unsession')) return { defaultOrgShortcode: null };
12-
return fetch(`${getBaseUrl()}/auth/redirection`, {
13-
headers: nextHeaders()
23+
return fetch(`${getPlatformUrl()}/auth/redirection`, {
24+
headers: clientHeaders()
1425
}).then((r) => (r.ok ? r.json() : { defaultOrgShortcode: null })) as Promise<{
1526
defaultOrgShortcode: string | null;
1627
}>;
@@ -22,8 +33,8 @@ export async function isAuthenticated(shallow = false) {
2233
if (!cookies().has('unsession')) return false;
2334
if (shallow) return true;
2435
try {
25-
const data = (await fetch(`${getBaseUrl()}/auth/status`, {
26-
headers: nextHeaders()
36+
const data = (await fetch(`${getPlatformUrl()}/auth/status`, {
37+
headers: clientHeaders()
2738
}).then((r) => r.json())) as {
2839
authStatus: 'authenticated' | 'unauthenticated';
2940
};

0 commit comments

Comments
 (0)