Skip to content
stoic man

Redis session in NestJS

Using redis as session storage

main.ts
import {ConfigService} from '@nestjs/config';
import {Logger} from '@nestjs/common';
 
import * as createRedisStore from 'connect-redis';
import * as cookieParser from 'cookie-parser';
import * as csurf from 'csurf';
import * as passport from 'passport';
import * as session from 'express-session';
import {createClient} from 'redis';
 
async function bootstrap() {
  const logger = new Logger();
  const app = await NestFactory.create(AppModule);
 
  const configService = app.get(ConfigService);
 
  // ... other stuff
 
  // Session
  const RedisStore = createRedisStore(session);
  const redisHost: string = configService.get('REDIS_HOST');
  const redisPort: number = configService.get('REDIS_PORT');
  const redisClient = createClient({
    host: redisHost,
    port: redisPort,
  });
 
  redisClient.on('error', (err) =>
    Logger.error('Could not establish a connection with redis. ' + err)
  );
  redisClient.on('connect', () =>
    Logger.verbose('Connected to redis successfully')
  );
 
  app.use(
    session({
      store: new RedisStore({client: redisClient as any}),
      secret: configService.get('SESSION_SECRET'),
      resave: false,
      saveUninitialized: false,
      cookie: {
        httpOnly: true,
        sameSite: 'strict',
        secure: isProdEnv(env),
      },
    })
  );
 
  app.use(passport.initialize());
  app.use(passport.session());
  // ---- Session
 
  // CSURF must be after cookie-parser & session
  app.use(csurf());
}
package.json
{
  // using version 3 instead for now
  "redis": "^3.1.2"
}

All snippets