Skip to main content

MongoDB

Install

npm install mongodb

Setup

import { MongoClient } from 'mongodb';
import { MongoDbUserStore } from './examples/mongodb-user-store.example';

const client = new MongoClient(process.env.MONGODB_URI!);
await client.connect();

const userStore = new MongoDbUserStore(client.db('myapp'));
await userStore.init(); // creates indexes automatically
const auth = new AuthConfigurator(config, userStore);

Collection Schema

interface UserDocument {
_id: string; // same as BaseUser.id
email: string;
password?: string;
name?: string;
role: string;
loginProvider: string;
providerAccountId?: string;
refreshToken?: string | null;
refreshTokenExpiry?: Date | null;
resetToken?: string | null;
resetTokenExpiry?: Date | null;
totpSecret?: string | null;
isTotpEnabled: boolean;
magicLinkToken?: string | null;
magicLinkTokenExpiry?: Date | null;
smsCode?: string | null;
smsCodeExpiry?: Date | null;
isEmailVerified: boolean;
phoneNumber?: string;
createdAt: Date;
}

Indexes

The init() method creates:

  • Unique index on email
  • Index on refreshToken
  • Index on resetToken
  • Index on magicLinkToken