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
| Method | Required For |
|---|---|
findByEmail | Login, magic link, password reset |
findById | Token refresh, 2FA, SMS |
create | OAuth strategies, registration |
updateRefreshToken | Login, logout, refresh |
updateResetToken | Password reset flow |
updatePassword | Change/reset password |
updateTotpSecret | TOTP setup |
updateMagicLinkToken | Magic link flow |
updateSmsCode | SMS OTP flow |
findByResetToken | POST /auth/reset-password |
findByMagicLinkToken | POST /auth/magic-link/verify |
findByProviderAccount | OAuth (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
- In-Memory
- SQLite
- MySQL
- MongoDB
- PostgreSQL
Perfect for testing and prototyping. No setup required.
Uses better-sqlite3. Great for small apps and local development.
Uses mysql2. Production-ready relational database.
Uses the mongodb driver. Flexible document storage.
Uses pg. Robust SQL with full ACID compliance.