Skip to content

Instantly share code, notes, and snippets.

@kilgarenone
Last active May 20, 2020 04:27
Show Gist options
  • Save kilgarenone/c762ac08a5a5d80782d2fb37114860f3 to your computer and use it in GitHub Desktop.
Save kilgarenone/c762ac08a5a5d80782d2fb37114860f3 to your computer and use it in GitHub Desktop.
express session
// app.js
const express = require("express");
const session = require("express-session");
const redisStore = require("connect-redis")(session);
const redis = require("redis");
const redisClient = redis.createClient();
const app = express();
const isProduction = app.get("env") === "production";
// a middleware at global level
app.use(
session({
name: "sametable_sid",
secret: 'randomkeyhere', // I would make this as a env var
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 7 * 86400 * 1000, // a session cookie will last for 7 days
secure: isProduction,
httpOnly: true,
sameSite: "none",
},
store: new redisStore({
host: "localhost",
port: 6379,
client: redisClient,
}),
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment