@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
Parameter | Type |
---|---|
db | D1Database |
Returns
createRecord()
createRecord<RecordType>(
db,
CREATE_SQL,
bindings,
GET_SQL,
getBindings): Promise<null | RecordType>
Type parameters
Type parameter |
---|
RecordType |
Parameters
Parameter | Type |
---|---|
db | D1Database |
CREATE_SQL | string |
bindings | any [] |
GET_SQL | string |
getBindings | any [] |
Returns
Promise
<null
| RecordType
>
deleteRecord()
deleteRecord(
db,
SQL,
bindings): Promise<void>
Parameters
Parameter | Type |
---|---|
db | D1Database |
SQL | string |
bindings | any [] |
Returns
Promise
<void
>
getRecord()
getRecord<RecordType>(
db,
SQL,
bindings): Promise<RecordType | null>
Type parameters
Type parameter |
---|
RecordType |
Parameters
Parameter | Type |
---|---|
db | D1Database |
SQL | string |
bindings | any [] |
Returns
Promise
<RecordType
| null
>
up()
up(db): Promise<void>
Parameters
Parameter | Type |
---|---|
db | D1Database |
Returns
Promise
<void
>
updateRecord()
updateRecord(
db,
SQL,
bindings): Promise<D1Result<unknown> | D1Result<unknown>>
Parameters
Parameter | Type |
---|---|
db | D1Database |
SQL | string |
bindings | any [] |
Returns
Promise
<D1Result
<unknown
> | D1Result
<unknown
>>