Add session driver

master
Dessa Simpson 2020-07-04 11:29:07 -07:00
parent 057ee6e56d
commit 94c1dd5782
6 changed files with 135 additions and 1 deletions

10
db/20-sessions.sql Normal file
View File

@ -0,0 +1,10 @@
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");

View File

@ -11,6 +11,7 @@ services:
PGHOST: db PGHOST: db
PGDATABASE: postgres PGDATABASE: postgres
PGUSER: postgres PGUSER: postgres
SESSION_SECRET: REPLACE_THIS_SECRET
db: db:
container_name: learn-request-queue-db container_name: learn-request-queue-db
image: postgres image: postgres

106
package-lock.json generated
View File

@ -44,6 +44,17 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/connect-pg-simple": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/@types/connect-pg-simple/-/connect-pg-simple-4.2.0.tgz",
"integrity": "sha512-Y+ptWW6q6Ll92Y0Zbqb+YiPIUd5ldNsovZJ22Oy5wc4tyl0QVqXPx+ksYCrMgUINXeBDQxJi7HcZ5iATZ62x8A==",
"dev": true,
"requires": {
"@types/express": "*",
"@types/express-session": "*",
"@types/pg": "*"
}
},
"@types/express": { "@types/express": {
"version": "4.17.6", "version": "4.17.6",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.6.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.6.tgz",
@ -67,6 +78,16 @@
"@types/range-parser": "*" "@types/range-parser": "*"
} }
}, },
"@types/express-session": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/@types/express-session/-/express-session-1.17.0.tgz",
"integrity": "sha512-OQEHeBFE1UhChVIBhRh9qElHUvTp4BzKKHxMDkGHT7WuYk5eL93hPG7D8YAIkoBSbhNEY0RjreF15zn+U0eLjA==",
"dev": true,
"requires": {
"@types/express": "*",
"@types/node": "*"
}
},
"@types/mime": { "@types/mime": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.2.tgz", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.2.tgz",
@ -408,6 +429,41 @@
"xdg-basedir": "^4.0.0" "xdg-basedir": "^4.0.0"
} }
}, },
"connect-pg-simple": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/connect-pg-simple/-/connect-pg-simple-6.1.0.tgz",
"integrity": "sha512-pWRuser61Opj/LtzrkuRkmBcCYY1dvZ7jLu83rR7vIsTzFpmQoe1KcmMalwlN3rCq1VVHssGjY42SCSe2vEizQ==",
"requires": {
"pg": "^7.4.3"
},
"dependencies": {
"pg": {
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.18.2.tgz",
"integrity": "sha512-Mvt0dGYMwvEADNKy5PMQGlzPudKcKKzJds/VbOeZJpb6f/pI3mmoXX0JksPgI3l3JPP/2Apq7F36O63J7mgveA==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-packet-stream": "^1.1.0",
"pg-pool": "^2.0.10",
"pg-types": "^2.1.0",
"pgpass": "1.x",
"semver": "4.3.2"
}
},
"pg-connection-string": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc="
},
"pg-pool": {
"version": "2.0.10",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.10.tgz",
"integrity": "sha512-qdwzY92bHf3nwzIUcj+zJ0Qo5lpG/YxchahxIN8+ZVmXqkahKXsnl2aiJPHLYN9o5mB/leG+Xh6XKxtP7e0sjg=="
}
}
},
"content-disposition": { "content-disposition": {
"version": "0.5.3", "version": "0.5.3",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
@ -575,6 +631,33 @@
"vary": "~1.1.2" "vary": "~1.1.2"
} }
}, },
"express-session": {
"version": "1.17.1",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz",
"integrity": "sha512-UbHwgqjxQZJiWRTMyhvWGvjBQduGCSBDhhZXYenziMFjxst5rMV+aJZ6hKPHZnPyHGsrqRICxtX8jtEbm/z36Q==",
"requires": {
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-headers": "~1.0.2",
"parseurl": "~1.3.3",
"safe-buffer": "5.2.0",
"uid-safe": "~2.1.5"
},
"dependencies": {
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
},
"safe-buffer": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
"integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg=="
}
}
},
"fill-range": { "fill-range": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -1011,6 +1094,11 @@
"ee-first": "1.1.1" "ee-first": "1.1.1"
} }
}, },
"on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
},
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@ -1086,6 +1174,11 @@
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
}, },
"pg-packet-stream": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/pg-packet-stream/-/pg-packet-stream-1.1.0.tgz",
"integrity": "sha512-kRBH0tDIW/8lfnnOyTwKD23ygJ/kexQVXZs7gEyBljw4FYqimZFxnMMx50ndZ8In77QgfGuItS5LLclC2TtjYg=="
},
"pg-pool": { "pg-pool": {
"version": "3.2.1", "version": "3.2.1",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.1.tgz", "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.1.tgz",
@ -1190,6 +1283,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
}, },
"random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs="
},
"range-parser": { "range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -1511,6 +1609,14 @@
"integrity": "sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==", "integrity": "sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==",
"dev": true "dev": true
}, },
"uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"requires": {
"random-bytes": "~1.0.0"
}
},
"undefsafe": { "undefsafe": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.3.tgz", "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.3.tgz",

View File

@ -4,11 +4,15 @@
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"connect-pg-simple": "^6.1.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-session": "^1.17.1",
"pg": "^8.2.1" "pg": "^8.2.1"
}, },
"devDependencies": { "devDependencies": {
"@types/connect-pg-simple": "^4.2.0",
"@types/express": "^4.17.6", "@types/express": "^4.17.6",
"@types/express-session": "^1.17.0",
"@types/node": "^14.0.14", "@types/node": "^14.0.14",
"@types/pg": "^7.14.3", "@types/pg": "^7.14.3",
"nodemon": "^2.0.4", "nodemon": "^2.0.4",

View File

@ -2,12 +2,20 @@ import * as config from "./config";
import * as requests from "./requests"; import * as requests from "./requests";
import { QueryResult } from "pg"; import { QueryResult } from "pg";
import express from "express"; import express from "express";
import session from "express-session";
import pgSessionStore from "connect-pg-simple";
import db from "./db"; import db from "./db";
import errorHandler from "./errors"; import errorHandler from "./errors";
const app = express(); const app = express();
app.use(express.static('public')); app.use(express.static('public'));
app.use(express.urlencoded({extended: false})); app.use(express.urlencoded({extended: false}));
app.use(session({
secret: config.sessionSecret,
saveUninitialized: false,
resave: false,
store: new (pgSessionStore(session))()
}));
app.get("/api/getRequests", async (request, response) => { app.get("/api/getRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );

View File

@ -2,5 +2,10 @@ if (!process.env.PORT) {
console.log("Missing environment variable PORT"); console.log("Missing environment variable PORT");
process.exit(1); process.exit(1);
} }
export const port: number = parseInt(process.env.PORT as string, 10); export const port: number = parseInt(process.env.PORT as string, 10);
if (!process.env.SESSION_SECRET) {
console.log("Missing environment variable SESSION_SECRET");
process.exit(1);
}
export const sessionSecret: string = process.env.SESSION_SECRET;