Skip to content

Commit

Permalink
fix: web middleware fetch TLS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle committed Jul 22, 2024
1 parent ca84b79 commit 4168f9d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "PORT=3000 next dev --turbo",
"build": "next build && cp -r .next/static .next/standalone/apps/web/.next",
"start": "node .next/standalone/apps/web/server.js",
"start": "HOSTNAME=0.0.0.0 node .next/standalone/apps/web/server.js",
"check": "tsc --noEmit && next lint"
},
"dependencies": {
Expand Down
21 changes: 16 additions & 5 deletions apps/web/src/lib/middleware-utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import 'server-only';
import { cookies, headers as nextHeaders } from 'next/headers';

function getBaseUrl() {
function getPlatformUrl() {
if (process.env.NEXT_PUBLIC_PLATFORM_URL)
return process.env.NEXT_PUBLIC_PLATFORM_URL;
throw new Error('NEXT_PUBLIC_PLATFORM_URL is not set');
}

function clientHeaders() {
const allHeaders = nextHeaders();
const clientHeaders = new Headers();
const notAllowedHeaders = ['host', 'origin', 'referer'];
allHeaders.forEach((value, key) => {
if (notAllowedHeaders.includes(key)) return;
clientHeaders.append(key, value);
});
return clientHeaders;
}

export async function getAuthRedirection() {
if (!cookies().has('unsession')) return { defaultOrgShortcode: null };
return fetch(`${getBaseUrl()}/auth/redirection`, {
headers: nextHeaders()
return fetch(`${getPlatformUrl()}/auth/redirection`, {
headers: clientHeaders()
}).then((r) => (r.ok ? r.json() : { defaultOrgShortcode: null })) as Promise<{
defaultOrgShortcode: string | null;
}>;
Expand All @@ -22,8 +33,8 @@ export async function isAuthenticated(shallow = false) {
if (!cookies().has('unsession')) return false;
if (shallow) return true;
try {
const data = (await fetch(`${getBaseUrl()}/auth/status`, {
headers: nextHeaders()
const data = (await fetch(`${getPlatformUrl()}/auth/status`, {
headers: clientHeaders()
}).then((r) => r.json())) as {
authStatus: 'authenticated' | 'unauthenticated';
};
Expand Down

0 comments on commit 4168f9d

Please sign in to comment.