Skip to main content

In-Memory Store

The in-memory store is ideal for testing, prototyping, and development. It's included in the examples/ directory.

Usage

import { InMemoryUserStore } from './examples/in-memory-user-store';

const userStore = new InMemoryUserStore();
const auth = new AuthConfigurator(config, userStore);

Example Implementation

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

export class InMemoryUserStore implements IUserStore {
private users: BaseUser[] = [];
private nextId = 1;

async findByEmail(email: string) {
return this.users.find(u => u.email === email) ?? null;
}

async findById(id: string) {
return this.users.find(u => u.id === id) ?? null;
}

async create(data: Partial<BaseUser>): Promise<BaseUser> {
const user: BaseUser = {
id: String(this.nextId++),
email: data.email!,
...data,
};
this.users.push(user);
return user;
}

async updateRefreshToken(userId: string, token: string | null, expiry: Date | null) {
const user = this.users.find(u => u.id === userId);
if (user) {
user.refreshToken = token;
user.refreshTokenExpiry = expiry;
}
}

// ... other methods
}