Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapters): add vanilla PostgreSQL adapter #4933

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/3_bug_adapter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ body:
- "@next-auth/mikro-orm-adapter"
- "@next-auth/mongodb-adapter"
- "@next-auth/neo4j-adapter"
- "@next-auth/pg-adapter"
- "@next-auth/pouchdb-adapter"
- "@next-auth/prisma-adapter"
- "@next-auth/sequelize-adapter"
Expand Down
3 changes: 3 additions & 0 deletions .github/issue-labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ mongodb:
neo4j:
- "@next-auth/neo4j-adapter"

pg:
- "@next-auth/pg-adapter"

pouchdb:
- "@next-auth/pouchdb-adapter"

Expand Down
95 changes: 70 additions & 25 deletions .github/pr-labeler.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,71 @@
# https://github.com/actions/labeler#create-githublabeleryml
adapters: ["packages/core/src/adapters.ts", "packages/adapter-*/**/*"]
core: ["packages/core/src/**/*"]
dgraph: ["packages/adapter-dgraph/**/*"]
documentation: ["packages/docs/docs/**/*"]
dynamodb: ["packages/adapter-dynamodb/**/*"]
examples: ["apps/examples/**/*"]
fauna: ["packages/adapter-fauna/**/*"]
firebase: ["packages/adapter-firebase/**/*"]
frameworks: ["packages/frameworks-*/**/*"]
legacy: ["packages/next-auth/**/*"]
mikro-orm: ["packages/adapter-mikro-orm/**/*"]
mongodb: ["packages/adapter-mongodb/**/*"]
neo4j: ["packages/adapter-neo4j/**/*"]
playgrounds: ["apps/playgrounds/**/*"]
pouchdb: ["packages/adapter-pouchdb/**/*"]
prisma: ["packages/adapter-prisma/**/*"]
providers: ["packages/core/src/providers/**/*"]
sequelize: ["packages/adapter-sequelize/**/*"]
solidjs: ["packages/frameworks-solid-start/**/*"]
supabase: ["packages/adapter-supabase/**/*"]
svelte: ["packages/frameworks-sveltekit/**/*"]
test: ["**test**/*"]
typeorm-legacy: ["packages/adapter-typeorm-legacy/**/*"]
upstash-redis: ["packages/adapter-upstash-redis/**/*"]
xata: ["packages/adapter-xata/**/*"]

test:
- test/**/*
- types/tests/**/*

providers:
- packages/next-auth/src/providers/**/*
- test/integration/**/*

adapters:
- packages/next-auth/src/adapters.ts
- packages/*-adapter/**

dgraph:
- packages/adapter-dgraph/**

dynamodb:
- packages/adapter-dynamodb/**

fauna:
- packages/adapter-fauna/**

firebase:
- packages/adapter-firebase/**

mikro-orm:
- packages/adapter-mikro-orm/**

mongodb:
- packages/adapter-mongodb/**

neo4j:
- packages/adapter-neo4j/**

pouchdb:
- packages/adapter-pouchdb/**

pg:
- "packages/adapter-pg/**"

prisma:
- packages/adapter-prisma/**

sequelize:
- packages/adapter-sequelize/**

typeorm-legacy:
- packages/adapter-typeorm-legacy/**

upstash-redis:
- packages/adapter-upstash-redis/**

core:
- packages/next-auth/src/**/*

style:
- packages/next-auth/src/css/**/*

client:
- packages/next-auth/src/client/**/*
- packages/next-auth/src/react/**/*

pages:
- packages/next-auth/src/core/pages/**/*

TypeScript:
- packages/next-auth/src/**/types.ts

documentation:
- packages/docs/docs/**/*
48 changes: 48 additions & 0 deletions packages/adapter-pg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Overview

This is the Postgres Adapter for [`next-auth`](https://next-auth.js.org). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package.

You can find the SQL schema in the docs at https://github.com/nextauthjs/next-auth/blob/main/packages/adapter-postgres/example-schema.sql

If you find any bugs please report them - this adapter is new!

## Getting Started

1. Install `next-auth` and `@next-auth/pg-adapter`. You'll also need the Postgres package `pg`
itself, though you'll likely have this already,

```js
npm install next-auth @next-auth/pg-adapter
npm install pg
```

2. Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object.

```js
import NextAuth from "next-auth"
import { PostgresAdapter } from "@next-auth/pg-adapter"
import { Pool } from "pg";

const connectionString = "postgresql://localhost/adapter-postgres-test"

const client = new Pool({
connectionString,
});

// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
// https://next-auth.js.org/configuration/providers
providers: [],
adapter: PostgresAdapter(client)
...
})
```

## Contributing

We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).

## License

ISC
12 changes: 12 additions & 0 deletions packages/adapter-pg/create-database-tables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -e

echo "Dropping DB adapter-postgres-test..."
dropdb adapter-postgres-test --if-exists
echo "Creating DB adapter-postgres-test..."
createdb adapter-postgres-test

echo "Creating tables in example-schema.sql..."
psql -d adapter-postgres-test -a -f ./example-schema.sql

echo "Done."
50 changes: 50 additions & 0 deletions packages/adapter-pg/example-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
\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 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)
);

45 changes: 45 additions & 0 deletions packages/adapter-pg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@next-auth/pg-adapter",
"description": "Postgres adapter for next-auth.",
"homepage": "https://next-auth.js.org",
"repository": "https://github.com/nextauthjs/next-auth",
"bugs": {
"url": "https://github.com/nextauthjs/next-auth/issues"
},
"author": "Jake Coppinger",
"main": "dist/index.js",
"license": "ISC",
"keywords": [
"next-auth",
"next.js",
"oauth",
"postgres"
],
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"init:default": "bash create-database-tables.sh",
"test": "pnpm init:default && jest",
"build": "tsc"
},
"files": [
"README.md",
"dist"
],
"peerDependencies": {
"next-auth": "^4"
},
"devDependencies": {
"@next-auth/adapter-test": "workspace:^0.0.0",
"@next-auth/tsconfig": "workspace:^0.0.0",
"@types/pg": "^8.6.5",
"jest": "^27.4.3",
"next-auth": "workspace:*",
"pg": "^8.7.1"
},
"jest": {
"preset": "@next-auth/adapter-test/jest"
}
}
Loading