-
Notifications
You must be signed in to change notification settings - Fork 17
Description
Description
When using @adminjs/fastify with other Fastify plugins like @fastify/oauth2 that also manage cookies, I encountered a FastifyError: The decorator 'serializeCookie' has already been added! error. This happens because buildAuthenticatedRouter internally registers @fastify/cookie without checking if it’s already initialized in the Fastify instance.
In contrast, @fastify/oauth2 checks for existing cookie initialization and skips registration if the serializeCookie decorator is present, avoiding conflicts. This behavior difference forces users to carefully order plugin registrations (e.g., loading AdminJS before OAuth2) to avoid errors, which isn’t ideal.
Steps to Reproduce
- Set up a Fastify app with
@fastify/cookieand@fastify/sessionat the root level. - Register
@fastify/oauth2for Google/GitHub authentication. - Register
@adminjs/fastifywithbuildAuthenticatedRouter. - Run the app and observe the
serializeCookiedecorator conflict.
Expected Behavior
@adminjs/fastify’s buildAuthenticatedRouter should check if @fastify/cookie (or its decorators like serializeCookie) is already registered in the Fastify instance and skip registration if present, similar to @fastify/oauth2.
Actual Behavior
buildAuthenticatedRouter unconditionally registers @fastify/cookie, causing a decorator conflict if another plugin or the root app has already done so.
Suggested Fix
- Add a check in
buildAuthenticatedRouter(or its internal setup) to detect existingfastify.cookieorserializeCookiedecorator:// https://github.com/SoftwareBrothers/adminjs-fastify/blob/main/src/buildAuthenticatedRouter.ts#L76C2-L78C6 if (!fastify.hasReplyDecorator('serializeCookie')) { await fastifyApp.register(fastifyCookie, { secret: auth.cookiePassword, }); }