Last active
May 20, 2020 04:27
-
-
Save kilgarenone/c762ac08a5a5d80782d2fb37114860f3 to your computer and use it in GitHub Desktop.
express session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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