All Projects → iamolegga → nestjs-cookie-session

iamolegga / nestjs-cookie-session

Licence: MIT License
Idiomatic Cookie Session Module for NestJS. Built on top of `cookie-session` 😻

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nestjs-cookie-session

nestjs-session
Idiomatic Session Module for NestJS. Built on top of `express-session` 😎
Stars: ✭ 150 (+328.57%)
Mutual labels:  expressjs, session, nest, nestjs
Cookie Session
Simple cookie-based session middleware
Stars: ✭ 928 (+2551.43%)
Mutual labels:  cookie, expressjs, session
node-nestjs-structure
Node.js framework NestJS project structure
Stars: ✭ 258 (+637.14%)
Mutual labels:  nest, nestjs
server-next
😎 The next generation of RESTful API service and more for Mix Space, powered by @nestjs.
Stars: ✭ 43 (+22.86%)
Mutual labels:  nest, nestjs
OSAPI
👋 OSAPI 是依靠通用性后台管理平台搭建的API管理平台,基于 vue3、Nestjs 技术栈实现,包含 RBAC 角色权限模块、数据展示、编辑等模块。
Stars: ✭ 32 (-8.57%)
Mutual labels:  session, nestjs
postgres-nest-react-typescript-boilerplate
No description or website provided.
Stars: ✭ 93 (+165.71%)
Mutual labels:  expressjs, nestjs
axios
Axios module for Nest framework (node.js) 🗂
Stars: ✭ 95 (+171.43%)
Mutual labels:  nest, nestjs
serverless-core-deprecated
[Deprecated] Serverless Core module for Nest framework (node.js) 🦊
Stars: ✭ 169 (+382.86%)
Mutual labels:  nest, nestjs
azure-storage
Azure Storage module for Nest framework (node.js) ☁️
Stars: ✭ 71 (+102.86%)
Mutual labels:  nest, nestjs
nx-ng-nest-universal
Nx Workspace with a seperated Nest App for Angular Universal SSR.
Stars: ✭ 32 (-8.57%)
Mutual labels:  nest, nestjs
nestlogger
Logger library for NestJs services
Stars: ✭ 28 (-20%)
Mutual labels:  nest, nestjs
mapped-types
Configuration module for Nest framework (node.js) 🐺
Stars: ✭ 192 (+448.57%)
Mutual labels:  nest, nestjs
necord
🤖 A module for creating Discord bots using NestJS, based on Discord.js
Stars: ✭ 77 (+120%)
Mutual labels:  nest, nestjs
Firstsight
前后端分离,服务端渲染的个人博客,基于 Nodejs、 Vue、 Nuxt、Nestjs、PostgreSQL、Apollo
Stars: ✭ 19 (-45.71%)
Mutual labels:  nest, nestjs
nestjs-toolbox
The repository contains a suite of components and modules for Nest.js
Stars: ✭ 166 (+374.29%)
Mutual labels:  nest, nestjs
laravel-localizer
Automatically detect and set an app locale that matches your visitor's preference.
Stars: ✭ 34 (-2.86%)
Mutual labels:  cookie, session
dothq.co
This repository has moved to:
Stars: ✭ 17 (-51.43%)
Mutual labels:  nest, nestjs
mom
Proof of concept for Message-Oriented-Middleware based architecture.
Stars: ✭ 39 (+11.43%)
Mutual labels:  nest, nestjs
ng-nest-cnode
Angular 10 Front-End and Nestjs 7 framework Back-End build Fullstack CNode
Stars: ✭ 17 (-51.43%)
Mutual labels:  expressjs, nestjs
aws-nestjs-starter
Serverless, AWS, NestJS, GraphQL and DynamoDB starter
Stars: ✭ 200 (+471.43%)
Mutual labels:  nest, nestjs

nestjs-cookie-session

npm GitHub branch checks state Supported platforms: Express

Snyk Vulnerabilities for npm package Dependencies status Dependabot Maintainability

Idiomatic Cookie Session Module for NestJS. Built on top of cookie-session 😎

This module implements a session with storing data directly in Cookie.

If you want to store data in one of external stores and passing ID of session to client via Cookie/Set-Cookie headers, you can look at nestjs-session.

Example

Register module:

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

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

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

    // or async:

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

Use in controllers with 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;
  }
}

To run examples:

git clone https://github.com/iamolegga/nestjs-cookie-session.git
cd nestjs-cookie-session
npm i
npm run build
cd example
npm i
npm start

Install

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

API

CookieSessionModule

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

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

CookieSessionModule.forRoot

Accept NestCookieSessionOptions. Returns NestJS DynamicModule for import.

CookieSessionModule.forRootAsync

Accept NestCookieSessionAsyncOptions. Returns NestJS DynamicModule for import.

NestCookieSessionOptions

NestCookieSessionOptions is the interface of all options, has next properties:

  • session - required - cookie-session options.
  • forRoutes - optional - same as NestJS built-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 built-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.

NestCookieSessionAsyncOptions

NestCookieSessionOptions is the interface of options to create cookie session module, that depends on other modules, has next properties:

  • imports - optional - modules, that cookie 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 NestCookieSessionOptions.

Migration

v2

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


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

Also if you are into NestJS ecosystem you may be interested in one of my other libs:

nestjs-pino

GitHub stars npm

Platform agnostic logger for NestJS based on pino with request context in every log


nestjs-session

GitHub stars npm

Idiomatic session module for NestJS. Built on top of express-session


nestjs-cookie-session

GitHub stars npm

Idiomatic cookie session module for NestJS. Built on top of cookie-session


nestjs-roles

GitHub stars npm

Type safe roles guard and decorator made easy


nestjs-injectable

GitHub stars npm

@Injectable() on steroids that simplifies work with inversion of control in your hexagonal architecture


nest-ratelimiter

GitHub stars npm

Distributed consistent flexible NestJS rate limiter based on Redis


create-nestjs-middleware-module

GitHub stars npm

Create simple idiomatic NestJS module based on Express/Fastify middleware in just a few lines of code with routing out of the box


nestjs-configure-after

GitHub stars npm

Declarative configuration of NestJS middleware order

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].