Skip to main content

Database Integration

node-auth is completely database-agnostic. Implement the IUserStore interface for your database and pass the instance to AuthConfigurator.

Any database

As long as you implement IUserStore, node-auth works with any storage backend — SQL, NoSQL, or even in-memory for tests.

IUserStore Interface

import { IUserStore, BaseUser } from '@nik2208/node-auth';

export class MyUserStore implements IUserStore {
// Core CRUD
async findByEmail(email: string): Promise<BaseUser | null> { /* ... */ }
async findById(id: string): Promise<BaseUser | null> { /* ... */ }
async create(data: Partial<BaseUser>): Promise<BaseUser> { /* ... */ }

// Token field updates
async updateRefreshToken(userId: string, token: string | null, expiry: Date | null): Promise<void> { /* ... */ }
async updateResetToken(userId: string, token: string | null, expiry: Date | null): Promise<void> { /* ... */ }
async updatePassword(userId: string, hashedPassword: string): Promise<void> { /* ... */ }
async updateTotpSecret(userId: string, secret: string | null): Promise<void> { /* ... */ }
async updateMagicLinkToken(userId: string, token: string | null, expiry: Date | null): Promise<void> { /* ... */ }
async updateSmsCode(userId: string, code: string | null, expiry: Date | null): Promise<void> { /* ... */ }

// Token look-ups (optional, required for specific features)
async findByResetToken(token: string): Promise<BaseUser | null> { /* ... */ }
async findByMagicLinkToken(token: string): Promise<BaseUser | null> { /* ... */ }
async findByProviderAccount(provider: string, providerAccountId: string): Promise<BaseUser | null> { /* ... */ }
}

Required Methods by Feature

MethodRequired For
findByEmailLogin, magic link, password reset
findByIdToken refresh, 2FA, SMS
createOAuth strategies, registration
updateRefreshTokenLogin, logout, refresh
updateResetTokenPassword reset flow
updatePasswordChange/reset password
updateTotpSecretTOTP setup
updateMagicLinkTokenMagic link flow
updateSmsCodeSMS OTP flow
findByResetTokenPOST /auth/reset-password
findByMagicLinkTokenPOST /auth/magic-link/verify
findByProviderAccountOAuth (recommended)

BaseUser

interface BaseUser {
id: string;
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;
}

Available Implementations

Perfect for testing and prototyping. No setup required.

View docs →