Skip to content

pnlinh-it/nestjs-session-based-auth

Repository files navigation

Nest Logo

Description

Session-based authentication with NestJS and PassportJS

Use file-session-store and redis-session-store

See: https://github.com/pnlinh-it/nestjs-session-based-auth/pulls/1

Reference

Installation

$ yarn install

Allow express DEBUG log

Or update package.json by add DEBUG=* before script

{
  "scripts": {
    "start:dev": "DEBUG=* nest start --watch",
    "start:debug": "DEBUG=* nest start --debug --watch",
  }
}

How express-session work

The content of session file

{
  "cookie": {
    "originalMaxAge": 60000,
    "expires": "2024-05-18T09:58:03.463Z",
    "httpOnly": true,
    "path": "/",
    "sameSite": true
  },
  // When we call request.login in local.guard.ts
  // Passport will save this value to session
  // https://github.com/jaredhanson/passport/blob/master/lib/sessionmanager.js#L44
  "passport": {
    "user": {
      "id": 1,
      "name": "Linh"
    }
  },
  "__lastAccess": 1716026223463
}

When we call request.login in local.guard.ts, Passport will save passport.user into to session: https://github.com/jaredhanson/passport/blob/master/lib/sessionmanager.js#L44

What happen when we send a request to /login endpoint

Running the app

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published