Commit a580e56a authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: wrap authPlugin with fastify-plugin to expose decorator to child contexts

The authenticate decorator was encapsulated and invisible to route handlers
registered in child scopes, causing "request.server.authenticate is not a function".
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 81460cca
Pipeline #63 failed with stages
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
"@supabase/supabase-js": "^2.49.1", "@supabase/supabase-js": "^2.49.1",
"drizzle-orm": "^0.38.3", "drizzle-orm": "^0.38.3",
"fastify": "^5.1.0", "fastify": "^5.1.0",
"fastify-plugin": "^5.1.0",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"nanoid": "^5.0.9", "nanoid": "^5.0.9",
"postgres": "^3.4.5", "postgres": "^3.4.5",
......
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import fp from 'fastify-plugin';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import { UnauthorizedError } from '../shared/errors.js'; import { UnauthorizedError } from '../shared/errors.js';
import type { Env } from '../config/env.js'; import type { Env } from '../config/env.js';
declare module 'fastify' { declare module 'fastify' {
interface FastifyInstance {
authenticate: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
}
interface FastifyRequest { interface FastifyRequest {
userId: string; userId: string;
userEmail?: string; userEmail?: string;
} }
} }
export async function authPlugin(app: FastifyInstance) { export const authPlugin = fp(async function (app: FastifyInstance) {
const env = app.env as Env; const env = app.env as Env;
app.decorate('authenticate', async function (request: FastifyRequest, _reply: FastifyReply) { app.decorate('authenticate', async function (request: FastifyRequest, _reply: FastifyReply) {
...@@ -38,8 +42,8 @@ export async function authPlugin(app: FastifyInstance) { ...@@ -38,8 +42,8 @@ export async function authPlugin(app: FastifyInstance) {
throw new UnauthorizedError('Invalid or expired token'); throw new UnauthorizedError('Invalid or expired token');
} }
}); });
} }, { name: 'auth-plugin' });
export async function authenticate(request: FastifyRequest, reply: FastifyReply) { export async function authenticate(request: FastifyRequest, reply: FastifyReply) {
await (request.server as FastifyInstance & { authenticate: (req: FastifyRequest, rep: FastifyReply) => Promise<void> }).authenticate(request, reply); await request.server.authenticate(request, reply);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment