fix(auth): add CORS headers to BetterAuth raw HTTP handler (#112)
Some checks failed
ci/woodpecker/push/ci Pipeline failed
Some checks failed
ci/woodpecker/push/ci Pipeline failed
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
This commit was merged in pull request #112.
This commit is contained in:
@@ -7,16 +7,17 @@ import { AUTH } from './auth.tokens.js';
|
|||||||
export function mountAuthHandler(app: NestFastifyApplication): void {
|
export function mountAuthHandler(app: NestFastifyApplication): void {
|
||||||
const auth = app.get<Auth>(AUTH);
|
const auth = app.get<Auth>(AUTH);
|
||||||
const nodeHandler = toNodeHandler(auth);
|
const nodeHandler = toNodeHandler(auth);
|
||||||
|
const corsOrigin = process.env['GATEWAY_CORS_ORIGIN'] ?? 'http://localhost:3000';
|
||||||
|
|
||||||
const fastify = app.getHttpAdapter().getInstance();
|
const fastify = app.getHttpAdapter().getInstance();
|
||||||
|
|
||||||
// Use Fastify's addHook to intercept auth requests at the raw HTTP level,
|
// BetterAuth is mounted at the raw HTTP level via Fastify's onRequest hook,
|
||||||
// before Fastify's body parser runs. This avoids conflicts with NestJS's
|
// bypassing NestJS middleware (including CORS). We must set CORS headers
|
||||||
// custom content-type parser.
|
// manually on the raw response before handing off to BetterAuth.
|
||||||
fastify.addHook(
|
fastify.addHook(
|
||||||
'onRequest',
|
'onRequest',
|
||||||
(
|
(
|
||||||
req: { raw: IncomingMessage; url: string },
|
req: { raw: IncomingMessage; url: string; method: string },
|
||||||
reply: { raw: ServerResponse; hijack: () => void },
|
reply: { raw: ServerResponse; hijack: () => void },
|
||||||
done: () => void,
|
done: () => void,
|
||||||
) => {
|
) => {
|
||||||
@@ -25,6 +26,27 @@ export function mountAuthHandler(app: NestFastifyApplication): void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const origin = req.raw.headers.origin;
|
||||||
|
const allowed = corsOrigin.split(',').map((o) => o.trim());
|
||||||
|
|
||||||
|
if (origin && allowed.includes(origin)) {
|
||||||
|
reply.raw.setHeader('Access-Control-Allow-Origin', origin);
|
||||||
|
reply.raw.setHeader('Access-Control-Allow-Credentials', 'true');
|
||||||
|
reply.raw.setHeader(
|
||||||
|
'Access-Control-Allow-Methods',
|
||||||
|
'GET, POST, PUT, PATCH, DELETE, OPTIONS',
|
||||||
|
);
|
||||||
|
reply.raw.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Cookie');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle preflight
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
reply.hijack();
|
||||||
|
reply.raw.writeHead(204);
|
||||||
|
reply.raw.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
reply.hijack();
|
reply.hijack();
|
||||||
nodeHandler(req.raw as IncomingMessage, reply.raw as ServerResponse)
|
nodeHandler(req.raw as IncomingMessage, reply.raw as ServerResponse)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user