forked from SeifYounis/Medical-Imaging-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.js
More file actions
48 lines (37 loc) · 1.29 KB
/
session.js
File metadata and controls
48 lines (37 loc) · 1.29 KB
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
43
44
45
/**
* Code for tracking user sessions
*/
// Query I used to make session database table
// CREATE TABLE IF NOT EXISTS myschema.session (
// sid varchar NOT NULL COLLATE "default",
// sess json NOT NULL,
// expire timestamp(6) NOT NULL,
// CONSTRAINT "session_pkey" PRIMARY KEY ("sid")
// );
// CREATE INDEX IF NOT EXISTS "IDX_session_expire" ON myschema.session ("expire");
// Query obtained here: https://medium.com/developer-rants/how-to-handle-sessions-properly-in-express-js-with-heroku-c35ea8c0e500
require('dotenv').config()
const pool = require('./db')
const session = require('express-session');
const PostgreSQLStore = require('connect-pg-simple')(session);
let sess;
function initializeSession() {
if (!sess) {
sess = session({
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: false,
store: new PostgreSQLStore({
conString: process.env.DATABASE_URL,
pool: pool,
schemaName: 'public',
tableName: 'session'
}),
// cookie: {
// maxAge: 60 * 60 * 1000, // Cookie lasts 1 hour, after which time the user must log in again
// }
})
}
return sess;
}
module.exports = initializeSession()