Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ndom91/authjs-prisma-edge-example

Example project for Auth.js with Prisma's Edge runtime support
https://github.com/ndom91/authjs-prisma-edge-example

authjs cloudflare-workers next-auth prisma vercel

Last synced: about 2 months ago
JSON representation

Example project for Auth.js with Prisma's Edge runtime support

Awesome Lists containing this project

README

        





Auth.js + Prisma Edge Example App



Open Source. Full Stack. Own Your Data.



npm
Bundle Size
TypeScript

## 🧭 Overview

This is an example application showing [Auth.js](https://authjs.dev) (`[email protected]`) and [Prisma](https://prisma.io) (`@prisma/[email protected]`) 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/[email protected]` 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](https://www.prisma.io/docs/orm/prisma-client/deployment/edge/overview#which-database-drivers-are-edge-compatible) 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!

## 🚀 Getting Started

### 1. Clone the repository and install dependencies

```bash
git clone https://github.com/ndom91/authjs-prisma-edge-example.git
cd authjs-prisma-edge-example
pnpm install
```

### 2. Configure your local environment

Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):

```bash
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](https://authjs.dev/reference/core/providers) for Auth.js login.

### 3. Database

This example is configured to use a [Neon Postgres](https://neon.tech) database ([Vercel Postgres](https://vercel.com/storage/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](https://www.prisma.io/docs/orm/prisma-client/deployment/edge/overview) for more details.

### 4. Start the application

To run your site locally, use:

```bash
pnpm dev
```

## 📝 License

MIT