Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@next-auth/dynamodb-adapter

Package Overview
Dependencies
Maintainers
4
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@next-auth/dynamodb-adapter

AWS DynamoDB adapter for next-auth.

  • 2.0.0-experimental.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-25.82%
Maintainers
4
Weekly downloads
 
Created
Source


    

DynamoDB Adapter - NextAuth.js

Open Source. Full Stack. Own Your Data.

Build Test Bundle Size @next-auth/dynamodb-adapter Version

Overview

This is the AWS DynamoDB Adapter for next-auth. This package can only be used in conjunction with the primary next-auth package. It is not a standalone package.

You need a table with a partition key pk and a sort key sk. Your table also needs a global secondary index named GSI1 with GSI1PK as partition key and GSI1SK as sorting key. You can set whatever you want as the table name and the billing method.

If you want sessions and verification tokens to get automatically removed from your table you need to activate TTL on your table with the TTL attribute name set to expires

You can find the DynamoDB schema in the docs at authjs.dev/reference/adapters/dynamodb.

Getting Started

  1. Install next-auth and @next-auth/dynamodb-adapter
npm install next-auth @next-auth/dynamodb-adapter
  1. Add this adapter to your pages/api/[...nextauth].js next-auth configuration object.

You need to pass DocumentClient instance from aws-sdk to the adapter. The default table name is next-auth, but you can customise that by passing { tableName: 'your-table-name' } as the second parameter in the adapter.

import { DynamoDB } from "@aws-sdk/client-dynamodb"
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { DynamoDBAdapter } from "@next-auth/dynamodb-adapter"

const config: DynamoDBClientConfig = {
  credentials: {
    accessKeyId: process.env.NEXT_AUTH_AWS_ACCESS_KEY as string,
    secretAccessKey: process.env.NEXT_AUTH_AWS_SECRET_KEY as string,
  },
  region: process.env.NEXT_AUTH_AWS_REGION,
};

const client = DynamoDBDocument.from(new DynamoDB(config), {
  marshallOptions: {
    convertEmptyValues: true,
    removeUndefinedValues: true,
    convertClassInstanceToMap: true,
  },
})

export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    Providers.GitHub({
      clientId: process.env.GITHUB_ID,
      clientSecret: process.env.GITHUB_SECRET,
    }),
    Providers.Email({
      server: process.env.EMAIL_SERVER,
      from: process.env.EMAIL_FROM,
    }),
    // ...add more providers here
  ],
  adapter: DynamoDBAdapter(
    client
  ),
  ...
});

(AWS secrets start with NEXT_AUTH_ in order to not conflict with Vercel's reserved environment variables.)

Table structure

The table respects the single table design pattern. This has many advantages:

  • Only one table to manage, monitor and provision.
  • Querying relations is faster than with multi-table schemas (for eg. retreiving all sessions for a user).
  • Only one table needs to be replicated, if you want to go multi-region.

Here is a schema of the table :

Contributing

We're open to all community contributions! If you'd like to contribute in any way, please read our Contributing Guide.

License

ISC

Keywords

FAQs

Package last updated on 03 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc