3.0.1 • Published 8 months ago

nestjs-session v3.0.1

Weekly downloads
3,148
License
MIT
Repository
github
Last release
8 months ago

This module implements a session with storing data in one of external stores and passing ID of session to client via Cookie/Set-Cookie headers.

If you want to store data directly in Cookie, you can look at nestjs-cookie-session.

Example

Register module:

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

@Module({
  imports: [
    // sync params:

    SessionModule.forRoot({
      session: { secret: 'keyboard cat' },
    }),

    // or async:

    SessionModule.forRootAsync({
      imports: [ConfigModule],
      inject: [Config],
      //              TIP: to get autocomplete in return object
      //                  add `NestSessionOptions` here ↓↓↓
      useFactory: async (config: Config): Promise<NestSessionOptions> => {
        return {
          session: { secret: config.secret },
        };
      },
    }),
  ],
  controllers: [ViewsController],
})
export class AppModule {}

In controllers use NestJS built-in Session decorator:

// views.controller.ts
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;
  }
}

BE AWARE THAT THIS EXAMPLE IS NOT FOR PRODUCTION! IT USES IN-MEMORY STORE, SO YOUR DATA WILL BE LOST ON RESTART. USE OTHER STORES


See redis-store example in examples folder.

To run examples:

git clone https://github.com/iamolegga/nestjs-session.git
cd nestjs-session
npm i
npm run build
cd examples/in-memory # or `cd examples/redis-store`
npm i
npm start

For Redis example, you should start Redis on localhost:6379. If you have Docker installed you can start Redis image by npm run redis from redis-store directory.

Install

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

API

SessionModule

SessionModule class has two static methods, that returns DynamicModule, that you need to import:

  • SessionModule.forRoot for sync configuration without dependencies
  • SessionModule.forRootAsync for sync/async configuration with dependencies

SessionModule.forRoot

Accept NestSessionOptions. Returns NestJS DynamicModule for import.

SessionModule.forRootAsync

Accept NestSessionAsyncOptions. Returns NestJS DynamicModule for import.

NestSessionOptions

NestSessionOptions is the interface of all options. It has next properties:

  • session - required - express-session options.
  • forRoutes - optional - same as NestJS buil-in MiddlewareConfigProxy['forRoutes'] See examples in official docs. Specify routes, that should have access to session. If forRoutes and exclude will not be set, then sessions will be set to all routes.
  • exclude - optional - same as NestJS buil-in MiddlewareConfigProxy['exclude'] See examples in official docs. Specify routes, that should not have access to session. If forRoutes and exclude will not be set, then sessions will be set to all routes.
  • retries - optional - number - by default if your session store lost connection to database it will return session as undefined, and no errors will be thrown, and then you need to check session in controller. But you can set this property how many times it should retry to get session, and on fail InternalServerErrorException will be thrown. If you don't want retries, but just want to InternalServerErrorException to be throw, then set to 0. Set this option, if you dont't want manualy check session inside controllers.
  • retriesStrategy - optional - (attempt: number) => number - function that returns number of ms to wait between next attempt. Not calls on first attempt.

NestSessionAsyncOptions

NestSessionAsyncOptions is the interface of options to create session module, that depends on other modules. It has next properties:

  • imports - optional - modules, that session module depends on. See official docs.
  • inject - optional - providers from imports-property modules, that will be passed as arguments to useFactory method.
  • useFactory - required - method, that returns NestSessionOptions.

Migration

v2

express-session and @types/express-session are moved to peer dependencies, so you can update them independently.

2.0.0-alpha.e52c2c

10 months ago

2.0.0-alpha.2dbff7

10 months ago

2.0.0-alpha.2a10ad

9 months ago

2.0.0-alpha.33f373

11 months ago

2.0.0-alpha.da0f09

12 months ago

2.0.0-alpha.88738a

9 months ago

3.0.1

8 months ago

3.0.0

8 months ago

2.0.0-alpha.b81b3b

10 months ago

2.0.0-alpha.614f67

11 months ago

2.0.0-alpha.5e0bda

11 months ago

2.0.0-alpha.ae0e89

9 months ago

2.0.0-alpha.46c040

10 months ago

3.0.0-alpha.e10644

8 months ago

2.0.0-alpha.0caea6

10 months ago

2.0.0-alpha.be15d7

10 months ago

2.0.0-alpha.a10a7b

9 months ago

2.0.0-alpha.3845ea

11 months ago

2.0.0-alpha.aa193a

9 months ago

2.0.0-alpha.20dee1

12 months ago

2.0.0-alpha.8a7235

9 months ago

2.0.0-alpha.442b6d

11 months ago

2.0.0-alpha.60b765

9 months ago

2.0.0-alpha.7e627d

10 months ago

2.0.0-alpha.92a4a2

10 months ago

2.0.0-alpha.e5c63b

11 months ago

2.0.0-alpha.8ce40e

10 months ago

2.0.0-alpha.4c7904

11 months ago

2.0.0-alpha.0c3c9f

9 months ago

2.0.0-alpha.896f8e

9 months ago

2.0.0-alpha.f00f31

9 months ago

2.0.0-alpha.d05a00

9 months ago

2.0.0-alpha.3107b4

9 months ago

2.0.0-alpha.04dd17

10 months ago

2.0.0-alpha.25ae7d

9 months ago

2.0.0-alpha.c92f7a

9 months ago

2.0.0-alpha.fbda0a

8 months ago

2.0.0-alpha.1b1c9b

9 months ago

2.0.0-alpha.5f8179

11 months ago

2.0.0-alpha.506c44

9 months ago

2.0.0-alpha.3ca913

9 months ago

2.0.0-alpha.f3caaf

11 months ago

2.0.0-alpha.0e8be1

12 months ago

2.0.0-alpha.b4075d

12 months ago

2.0.0-alpha.d0bdc4

12 months ago

2.0.0-alpha.3f915b

12 months ago

2.0.0-alpha.4a374c

12 months ago

2.0.0-alpha.94662

12 months ago

2.0.0-alpha.3dcffe

12 months ago

2.0.0-alpha.404

2 years ago

2.0.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago