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: web middleware fetch TLS issues #588

Merged
merged 1 commit into from
Jul 22, 2024
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
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