DynamoDB-backed authentication
Persist Auth.js users, accounts, sessions, and verification data in Amazon DynamoDB.
Auth.js · Const
DynamoDBAdapter creates an Auth.js database adapter backed by DynamoDB. It requires an AWS SDK v3 DynamoDBDocument client and optionally accepts configuration for table and key names.
DynamoDBAdapter accepts an already-created AWS SDK v3 DynamoDBDocument client and optional adapter settings, then returns an Auth.js Adapter that persists authentication data in DynamoDB. Without options, it uses the adapter's expected table and key layout; options can customize names and keys.
function DynamoDBAdapter(client, options?): Adapter| Name | Type | Description |
|---|---|---|
client | DynamoDBDocument | AWS SDK v3 DynamoDB document client used for adapter operations. |
options | DynamoDBAdapterOptions | Optional table-name and key/index configuration. |
Adapter
import NextAuth from "next-auth";
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
import { DynamoDBAdapter } from "@auth/dynamodb-adapter";
const client = DynamoDBDocument.from(new DynamoDB({
region: process.env.AUTH_DYNAMODB_REGION,
credentials: {
accessKeyId: process.env.AUTH_DYNAMODB_ID!,
secretAccessKey: process.env.AUTH_DYNAMODB_SECRET!,
},
}));
export const { handlers, auth } = NextAuth({
providers: [],
adapter: DynamoDBAdapter(client),
});Creates an AWS SDK v3 DynamoDBDocument client and supplies it to DynamoDBAdapter in Auth.js configuration.
Persist Auth.js users, accounts, sessions, and verification data in Amazon DynamoDB.
Use an existing DynamoDB table with customized names or keys.
Configure Auth.js with the official AWS DynamoDB adapter package.
DynamoDBAdapter is a factory function. It receives a DynamoDBDocument client plus optional DynamoDBAdapterOptions and returns an Auth.js Adapter.
Follow-up: How would you connect it to Auth.js? Pass the returned value as the adapter property in the NextAuth configuration.
The client parameter is typed as DynamoDBDocument. The documentation specifically requires the modular AWS SDK v3 document client.
Follow-up: Why use the document client? It provides the document-oriented interface expected by this adapter instead of requiring low-level attribute-value handling.
The adapter uses next-auth as its default table name and expects pk/sk keys plus the GSI1 index with GSI1PK and GSI1SK by default. Options can customize these table and key settings.
Follow-up: What should be checked before deployment? Verify that the deployed table and global secondary index match the adapter configuration.
DynamoDBAdapterOptionsDynamoDBDocumentAdapter@auth/dynamodb-adaptergenerateUpdateExpression