Skip to content

Commit

Permalink
feat: introduce @auth/neon-adapter (#12295)
Browse files Browse the repository at this point in the history
Co-authored-by: Nico Domino <[email protected]>
  • Loading branch information
rishi-raj-jain and ndom91 authored Jan 7, 2025
1 parent 590f01e commit 35215f8
Show file tree
Hide file tree
Showing 13 changed files with 743 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/pr-labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mongodb: ["packages/adapter-mongodb/**/*"]
neo4j: ["packages/adapter-neo4j/**/*"]
next-auth: ["packages/next-auth/**/*"]
pg: ["packages/adapter-pg/**/*"]
neon: ["packages/adapter-neon/**/*"]
playgrounds: ["apps/playgrounds/**/*"]
pouchdb: ["packages/adapter-pouchdb/**/*"]
prisma: ["packages/adapter-prisma/**/*"]
Expand Down
1 change: 1 addition & 0 deletions docs/pages/data/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"adapters": {
"prisma": "Prisma",
"drizzle": "Drizzle ORM",
"neon": "Neon",
"supabase": "Supabase",
"firebase": "Firebase",
"typeorm": "TypeORM",
Expand Down
168 changes: 168 additions & 0 deletions docs/pages/getting-started/adapters/neon.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { Code } from "@/components/Code"

<img align="right" src="/img/adapters/neon.svg" height="64" width="64" />

# Neon Adapter

## Resources

- [Neon documentation](https://neon.tech/docs/)

## Setup

### Installation

```bash npm2yarn
npm install @auth/neon-adapter @neondatabase/serverless
```

### Environment Variables

```sh
DATABASE_URL=
```

### Configuration

<Code>
<Code.Next>

```ts filename="./auth.ts"
import NextAuth from "next-auth"
import NeonAdapter from "@auth/neon-adapter"
import { Pool } from "@neondatabase/serverless"

// *DO NOT* create a `Pool` here, outside the request handler.

export const { handlers, auth, signIn, signOut } = NextAuth(() => {
// Create a `Pool` inside the request handler.
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
return {
adapter: NeonAdapter(pool),
providers: [],
}
})
```

</Code.Next>
<Code.Qwik>

```ts filename="/src/routes/[email protected]"
import { QwikAuth$ } from "@auth/qwik"
import NeonAdapter from "@auth/neon-adapter"
import { Pool } from "@neondatabase/serverless"

// *DO NOT* create a `Pool` here, outside the request handler.

export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
() => {
// Create a `Pool` inside the request handler.
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
return {
adapter: NeonAdapter(pool),
providers: [],
}
}
)
```

</Code.Qwik>
<Code.Svelte>

```ts filename="./src/auth.ts"
import { SvelteKitAuth } from "@auth/sveltekit"
import NeonAdapter from "@auth/neon-adapter"
import { Pool } from "@neondatabase/serverless"

// *DO NOT* create a `Pool` here, outside the request handler.

export const { handle, signIn, signOut } = SvelteKitAuth(() => {
// Create a `Pool` inside the request handler.
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
return {
adapter: NeonAdapter(pool),
providers: [],
}
})
```

</Code.Svelte>
<Code.Express>

```ts filename="./src/routes/auth.route.ts"
import { ExpressAuth } from "@auth/express"
import NeonAdapter from "@auth/neon-adapter"
import { Pool } from "@neondatabase/serverless"

const pool = new Pool({ connectionString: process.env.DATABASE_URL })

const app = express()

app.set("trust proxy", true)
app.use(
"/auth/*",
ExpressAuth({
providers: [],
adapter: NeonAdapter(pool),
})
)
```

</Code.Express>
</Code>

### Schema

The SQL schema for the tables used by this adapter is as follows. Learn more about the models at our
doc page on [Database Models](/guides/creating-a-database-adapter).

```sql
CREATE TABLE verification_token
(
identifier TEXT NOT NULL,
expires TIMESTAMPTZ NOT NULL,
token TEXT NOT NULL,

PRIMARY KEY (identifier, token)
);

CREATE TABLE accounts
(
id SERIAL,
"userId" INTEGER NOT NULL,
type VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
"providerAccountId" VARCHAR(255) NOT NULL,
refresh_token TEXT,
access_token TEXT,
expires_at BIGINT,
id_token TEXT,
scope TEXT,
session_state TEXT,
token_type TEXT,

PRIMARY KEY (id)
);

CREATE TABLE sessions
(
id SERIAL,
"userId" INTEGER NOT NULL,
expires TIMESTAMPTZ NOT NULL,
"sessionToken" VARCHAR(255) NOT NULL,

PRIMARY KEY (id)
);

CREATE TABLE users
(
id SERIAL,
name VARCHAR(255),
email VARCHAR(255),
"emailVerified" TIMESTAMPTZ,
image TEXT,

PRIMARY KEY (id)
);

```
16 changes: 16 additions & 0 deletions docs/public/img/adapters/neon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions packages/adapter-neon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<p align="center">
<br/>
<a href="https://authjs.dev" target="_blank">
<img height="64px" src="https://authjs.dev/img/logo-sm.png" />
</a>
<a href="https://neon.tech/" target="_blank">
<img height="64px" src="https://neon.tech/brand/neon-logo-dark-color.svg" />
</a>
<h3 align="center"><b>Neon Adapter</b> - NextAuth.js / Auth.js</a></h3>
<p align="center" style="align: center;">
<a href="https://npm.im/@auth/neon-adapter">
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
</a>
<a href="https://npm.im/@auth/neon-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@auth/neon-adapter?color=green&label=@auth/neon-adapter&style=flat-square">
</a>
<a href="https://www.npmtrends.com/@auth/neon-adapter">
<img src="https://img.shields.io/npm/dm/@auth/neon-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
</a>
<a href="https://github.com/nextauthjs/next-auth/stargazers">
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="GitHub Stars" />
</a>
</p>
</p>

---

Check out the documentation at [authjs.dev](https://authjs.dev/reference/adapter/pg).
52 changes: 52 additions & 0 deletions packages/adapter-neon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@auth/neon-adapter",
"version": "1.7.4",
"description": "Neon Postgres adapter for next-auth.",
"homepage": "https://authjs.dev",
"repository": "https://github.com/nextauthjs/next-auth",
"bugs": {
"url": "https://github.com/nextauthjs/next-auth/issues"
},
"author": "Jake Coppinger",
"contributors": [
"Thang Huu Vu <[email protected]>"
],
"license": "ISC",
"keywords": [
"next-auth",
"@auth",
"Auth.js",
"next.js",
"oauth",
"postgres"
],
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"files": [
"*.d.ts*",
"*.js",
"src"
],
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "./test/test.sh",
"build": "tsc",
"clean": "rm -rf *.js *.d.ts*"
},
"dependencies": {
"@auth/core": "workspace:*",
"@neondatabase/serverless": "^0.10.4"
},
"devDependencies": {
"@types/ws": "^8.5.13",
"ws": "^8.18.0"
}
}
49 changes: 49 additions & 0 deletions packages/adapter-neon/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
\set ON_ERROR_STOP true

CREATE TABLE verification_token
(
identifier TEXT NOT NULL,
expires TIMESTAMPTZ NOT NULL,
token TEXT NOT NULL,

PRIMARY KEY (identifier, token)
);

CREATE TABLE accounts
(
id SERIAL,
"userId" INTEGER NOT NULL,
type VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
"providerAccountId" VARCHAR(255) NOT NULL,
refresh_token TEXT,
access_token TEXT,
expires_at INTEGER,
id_token TEXT,
scope TEXT,
session_state TEXT,
token_type TEXT,

PRIMARY KEY (id)
);

CREATE TABLE sessions
(
id SERIAL,
"userId" INTEGER NOT NULL,
expires TIMESTAMPTZ NOT NULL,
"sessionToken" VARCHAR(255) NOT NULL,

PRIMARY KEY (id)
);

CREATE TABLE users
(
id SERIAL,
name VARCHAR(255),
email VARCHAR(255) UNIQUE,
"emailVerified" TIMESTAMPTZ,
image TEXT,

PRIMARY KEY (id)
);
Loading

0 comments on commit 35215f8

Please sign in to comment.