Open Source. Full Stack. Own Your Data.
This is an example application showing Auth.js (next-auth@5.0.0-beta.15
) and Prisma (@prisma/client@5.12.0
) working together in edge runtimes, like Vercel's middleware or Cloudflare Workers.
This had previously only been possible with significant workarounds and limitations. As of @prisma/client@5.9.1
Prisma began making changes to their client, for example, to error out at query-time, not instantiation. So you could begin using Prisma with next-auth
in Edge runtimes, as long as you didn't actually execute any queries on the edge. This implied using the Auth.js setting session: { strategy: 'jwt' }
, as the strategy: 'database'
didn't work because we couldn't update the expiry time of a database-based session in the middleware
handler.
Prisma has now rolled out edge-compatible clients and adapters which communicate via HTTP, making them much more straightforward to run under any JavaScript runtime. You can check out their edge deployment page for a current list of supported adapters and platforms.
As this example shows, however, using Prisma + Auth.js with an Edge-compatible database provider and adapter like Neon (Vercel Postgres), PlanetScale or Turso is beginning to become much more straightforward!
git clone https://github.com/ndom91/authjs-prisma-edge-example.git
cd authjs-prisma-edge-example
pnpm install
Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):
cp .env.example .env
Make sure to fill out the AUTH_SECRET
env var as well as your Prisma database connection string(s). Also, don't forget to add environment variables to configure any of the supported providers for Auth.js login.
This example is configured to use a Neon Postgres database (Vercel Postgres also works). Any Prisma Edge compatible database driver should work with a bit of tweaking though, these currently include:
- PlanetScale serverless driver with
@prisma/adapter-planetscale
- Neon serverless driver with
@prisma/adapter-neon
- Turso with
@prisma/adapter-libsql
- Cloudflare D1 with
@prisma/adapter-d1
- PostgreSQL with
@prisma/adapter-pg
See Prisma's Edge compatible driver documentation for more details.
To run your site locally, use:
pnpm dev
MIT