-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession-config.js
42 lines (38 loc) · 1.27 KB
/
session-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require("dotenv").config();
const session = require("express-session");
const genFunc = require("connect-pg-simple");
const PostgresqlStore = genFunc(session);
const sessionStore = new PostgresqlStore({
conString: `postgres://${process.env.PG_USER}:${process.env.PG_PASSWORD}@${process.env.PG_HOST}:${process.env.PG_PORT}/${process.env.PG_DB}`
});
function applySession(app) {
//init session
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 86400000 * 1, // 86400000 = 1 day
secure: true,
sameSite: "none",
},
store: sessionStore
}));
app.use(function (req, res, next) {
//session middleware adding language
if (!req.session.language) {
req.session.language = "english.json"
}
//adding ip address
if (!req.session.ipAddress) {
req.session.ipAddress = req.socket.remoteAddress;
}
next();
})
}
module.exports = applySession;
/* sql seeding
CREATE TABLE session (sid varchar NOT NULL COLLATE "default", sess json NOT NULL, expire timestamp(6) NOT NULL ) WITH (OIDS=FALSE);
ALTER TABLE session ADD CONSTRAINT session_pkey PRIMARY KEY (sid) NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX idx_session_expire ON session (expire);
*/