Fastify Auth Prisma is a Fastify plugin that integrates with Prisma to provide a simple and secure authentication middleware solution. It handles token-based authentication, allowing developers to protect routes and manage user sessions by leveraging Prisma for database interactions. The current stable version is 1.2.444, indicating active development within the 1.x release line. While a specific release cadence isn't stated, the version numbering suggests frequent updates. Key differentiators include its direct integration with Prisma, simplifying the data layer for authentication, and its focus on being a Fastify-native solution for performance and developer experience within the Fastify ecosystem. It provides mechanisms for defining public routes and validating connected users using a Prisma client and JWT secrets.
npm install fastify-auth-prismaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `fastify-auth-prisma` with a basic Fastify server, including Prisma client integration, custom user validation, and defining public/protected routes. It shows how `connectedUser` is made available on the request object for authenticated users.
Ensure you add the `declare module 'fastify'` snippet as shown in the quickstart or documentation to extend the `FastifyRequest` interface with `connectedUser`.
Always store `process.env.JWT_ACCESS_SECRET` in production environments. Consider using environment variable managers or secret management services for deployment.
Refer to the `prisma.schema` example provided in the documentation to ensure your Prisma models for `User` and `Token` include the necessary fields like `id`, `refreshToken`, `accessToken`, and the relation `owner`/`ownerId`.
Either register `unify-fastify` if it's a required dependency, or check if `fastify-auth-prisma` can function correctly without it based on its internal implementation details or future documentation updates.
Add `declare module 'fastify' { interface FastifyRequest { connectedUser?: User; } }` to your project's global declaration file or a relevant TypeScript file.Ensure `fastify()` is called correctly and `server.register` is invoked within an `async` function with `await` if using top-level await or inside a setup function.
Provide a non-empty string for the `secret` option when registering `fastifyAuthPrismaPlugin`, ideally from `process.env`.