Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference
@auth/d1-adapter

@auth/d1-adapter

An official Cloudflare D1 adapter for Auth.js / NextAuth.js.

Warning

This adapter is not developed or maintained by Cloudflare and they haven’t declared the D1 api stable. The author will make an effort to keep this adapter up to date. The adapter is compatible with the D1 api as of March 22, 2023.

Installation

npm install next-auth @auth/d1-adapter

D1Database

type D1Database: WorkerDatabase | MiniflareD1Database;

CREATE_ACCOUNT_SQL

const CREATE_ACCOUNT_SQL: "\n  INSERT INTO accounts (\n    id, userId, type, provider, \n    providerAccountId, refresh_token, access_token, \n    expires_at, token_type, scope, id_token, session_state,\n    oauth_token, oauth_token_secret\n  ) \n  VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

CREATE_SESSION_SQL

const CREATE_SESSION_SQL: "INSERT INTO sessions (id, sessionToken, userId, expires) VALUES (?,?,?,?)" = "INSERT INTO sessions (id, sessionToken, userId, expires) VALUES (?,?,?,?)";

CREATE_USER_SQL

const CREATE_USER_SQL: "INSERT INTO users (id, name, email, emailVerified, image) VALUES (?, ?, ?, ?, ?)";

CREATE_VERIFICATION_TOKEN_SQL

const CREATE_VERIFICATION_TOKEN_SQL: "INSERT INTO verification_tokens (identifier, expires, token) VALUES (?,?,?)";

DELETE_ACCOUNT_BY_PROVIDER_AND_PROVIDER_ACCOUNT_ID_SQL

const DELETE_ACCOUNT_BY_PROVIDER_AND_PROVIDER_ACCOUNT_ID_SQL: "DELETE FROM accounts WHERE provider = ? AND providerAccountId = ?";

DELETE_ACCOUNT_BY_USER_ID_SQL

const DELETE_ACCOUNT_BY_USER_ID_SQL: "DELETE FROM accounts WHERE userId = ?";

DELETE_SESSION_BY_USER_ID_SQL

const DELETE_SESSION_BY_USER_ID_SQL: "DELETE FROM sessions WHERE userId = ?";

DELETE_SESSION_SQL

const DELETE_SESSION_SQL: "DELETE FROM sessions WHERE sessionToken = ?";

DELETE_USER_SQL

const DELETE_USER_SQL: "DELETE FROM users WHERE id = ?";

DELETE_VERIFICATION_TOKEN_SQL

const DELETE_VERIFICATION_TOKEN_SQL: "DELETE FROM verification_tokens WHERE identifier = ? and token = ?";

GET_ACCOUNT_BY_ID_SQL

const GET_ACCOUNT_BY_ID_SQL: "SELECT * FROM accounts WHERE id = ? ";

GET_ACCOUNT_BY_PROVIDER_AND_PROVIDER_ACCOUNT_ID_SQL

const GET_ACCOUNT_BY_PROVIDER_AND_PROVIDER_ACCOUNT_ID_SQL: "SELECT * FROM accounts WHERE provider = ? AND providerAccountId = ?";

GET_SESSION_BY_TOKEN_SQL

const GET_SESSION_BY_TOKEN_SQL: "\n  SELECT id, sessionToken, userId, expires\n  FROM sessions\n  WHERE sessionToken = ?";

GET_USER_BY_ACCOUNTL_SQL

const GET_USER_BY_ACCOUNTL_SQL: "\n  SELECT u.*\n  FROM users u JOIN accounts a ON a.userId = u.id\n  WHERE a.providerAccountId = ? AND a.provider = ?";

GET_USER_BY_EMAIL_SQL

const GET_USER_BY_EMAIL_SQL: "SELECT * FROM users WHERE email = ?";

GET_USER_BY_ID_SQL

const GET_USER_BY_ID_SQL: "SELECT * FROM users WHERE id = ?";

GET_VERIFICATION_TOKEN_BY_IDENTIFIER_AND_TOKEN_SQL

const GET_VERIFICATION_TOKEN_BY_IDENTIFIER_AND_TOKEN_SQL: "SELECT * FROM verification_tokens WHERE identifier = ? AND token = ?";

UPDATE_SESSION_BY_SESSION_TOKEN_SQL

const UPDATE_SESSION_BY_SESSION_TOKEN_SQL: "UPDATE sessions SET expires = ? WHERE sessionToken = ?";

UPDATE_USER_BY_ID_SQL

const UPDATE_USER_BY_ID_SQL: "\n  UPDATE users \n  SET name = ?, email = ?, emailVerified = ?, image = ?\n  WHERE id = ? ";

D1Adapter()

D1Adapter(db): Adapter

Parameters

ParameterType
dbD1Database

Returns

Adapter


createRecord()

createRecord<RecordType>(
   db, 
   CREATE_SQL, 
   bindings, 
   GET_SQL, 
getBindings): Promise<null | RecordType>

Type parameters

Type parameter
RecordType

Parameters

ParameterType
dbD1Database
CREATE_SQLstring
bindingsany[]
GET_SQLstring
getBindingsany[]

Returns

Promise<null | RecordType>


deleteRecord()

deleteRecord(
   db, 
   SQL, 
bindings): Promise<void>

Parameters

ParameterType
dbD1Database
SQLstring
bindingsany[]

Returns

Promise<void>


getRecord()

getRecord<RecordType>(
   db, 
   SQL, 
bindings): Promise<RecordType | null>

Type parameters

Type parameter
RecordType

Parameters

ParameterType
dbD1Database
SQLstring
bindingsany[]

Returns

Promise<RecordType | null>


up()

up(db): Promise<void>

Parameters

ParameterType
dbD1Database

Returns

Promise<void>


updateRecord()

updateRecord(
   db, 
   SQL, 
bindings): Promise<D1Result<unknown> | D1Result<unknown>>

Parameters

ParameterType
dbD1Database
SQLstring
bindingsany[]

Returns

Promise<D1Result<unknown> | D1Result<unknown>>

Auth.js © Balázs Orbán and Team - 2024