NestJS-Session

Mar 4, 2024 | Programming

npm npm GitHub branch checks state Known Vulnerabilities Libraries.io Dependabot Supported platforms: Express

Idiomatic Session Module for NestJS. Built on top of express-session.

This module implements a session by storing data in one of external stores and passing the session ID to the client via CookieSet-Cookie headers. If you want to store data directly in Cookies, you can look at nestjs-cookie-session.

Getting Started with NestJS-Session

This guide will walk you through setting up a basic session module using NestJS. It will be as easy as pie!

Step 1: Set Up Your Project

Start by installing the necessary packages:

sh
npm i nestjs-session express-session @types/express-session

Step 2: Register the Session Module

In your `app.module.ts`, import the `SessionModule` like this:

typescript
import { Module } from '@nestjs/common';
import { NestSessionOptions, SessionModule } from 'nestjs-session';
import { ViewsController } from './views.controller';

@Module({
  imports: [
    SessionModule.forRoot({
      session: { secret: 'keyboard cat' },
    }),
  ],
  controllers: [ViewsController],
})
export class AppModule {}

Step 3: Create a Views Controller

Next, use the built-in `Session` decorator in your controller:

typescript
import { Controller, Get, Session } from '@nestjs/common';

@Controller('views')
export class ViewsController {
  @Get()
  getViews(@Session() session: { views?: number }) {
    session.views = (session.views || 0) + 1;
    return session.views;
  }
}

Important Note!

**Be aware that this example is not for production! It uses an in-memory store, so your data will be lost on restart. Use other compatible session stores**.

Running Examples

To run the examples, execute the following commands:

sh
git clone https://github.com/iamoleggan/nestjs-session.git
cd nestjs-session
npm install
npm run build
cd examples/in-memory
npm start

API Overview

The SessionModule class has two static methods for dynamic configuration:

  • SessionModule.forRoot – for synchronous configuration without dependencies
  • SessionModule.forRootAsync – for synchronous/asynchronous configuration with dependencies

Debugging Options

If you encounter issues, consider the following troubleshooting tips:

  • Ensure session dependencies are correctly installed.
  • Check that the session store is operational and compatible.
  • Confirm that your session secret is secure and not exposed in your code.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Feeling Bold? Give it a Star!

Do you use this library? Don’t be shy to give it a star!

Explore More NestJS Libraries

If you’re into NestJS, you might be interested in one of my NestJS libraries.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox