Skip to content

Session Module

The SessionModule provides cookie-based session management with pluggable storage backends.

Setup

typescript
import { Module, SessionModule } from 'next-js-backend';

@Module({
  imports: [
    SessionModule.register({
      secret: process.env.SESSION_SECRET!,
      cookieName: 'sid',
      ttl: 86400,        // 24 hours
      // store: new RedisSessionStore(...), // optional
    }),
  ],
})
export class AppModule {}

Reading Sessions

Use @Session() in controllers:

typescript
import { Session } from 'next-js-backend';

@Controller('/auth')
export class AuthController {
  constructor(private readonly sessionService: SessionService) {}

  @Post('/login')
  async login(@Body() dto: LoginDto, @Session() session: any) {
    const user = await this.authService.validate(dto);
    
    // Save to session
    await this.sessionService.setSession(session.id, { userId: user.id, role: user.role });
    
    return { message: 'Logged in' };
  }

  @Get('/me')
  async me(@Session() session: Record<string, unknown>) {
    return session; // { userId: 'u_123', role: 'admin' }
  }

  @Post('/logout')
  async logout(@Session() session: any) {
    await this.sessionService.destroy(session.id);
    return { message: 'Logged out' };
  }
}

Session Store

InMemory (default)

typescript
SessionModule.register({
  secret: 'secret',
  // store not provided → uses InMemorySessionStore
})

Periodically purges expired sessions every 60 seconds.

Redis

typescript
import { RedisSessionStore } from 'next-js-backend';

SessionModule.register({
  secret: process.env.SESSION_SECRET!,
  store: new RedisSessionStore({ client: redisClient }),
})

Custom Store

Implement SessionStore:

typescript
import { SessionStore } from 'next-js-backend';

export class PrismaSessionStore extends SessionStore {
  async get(sessionId: string) { ... }
  async set(sessionId: string, data, ttl) { ... }
  async destroy(sessionId: string) { ... }
  async touch(sessionId: string, ttl) { ... }
}

Configuration Options

OptionTypeRequiredDescription
secretstringSecret for cookie signing
cookieNamestringCookie name (default: 'sid')
ttlnumberSession TTL in seconds (default: 86400)
storeSessionStoreCustom storage backend
securebooleanHTTPS-only cookie
httpOnlybooleanPrevent JS access

Released under the MIT License.