-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.ts
62 lines (53 loc) · 1.79 KB
/
express.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable no-console */
import session from 'express-session';
import { DynamoDBStore } from '@pwrdrvr/dynamodb-session-store';
import * as dynamodb from '@aws-sdk/client-dynamodb';
import express from 'express';
const { TABLE_NAME = 'dynamodb-session-store-test', PORT = '3001' } = process.env;
const dynamoDBClient = new dynamodb.DynamoDBClient({});
const app = express();
const port = 3001;
// Augment the session data with our own properties
// Note: we can't do this because it changes the type in all files of the project
// declare module 'express-session' {
// interface SessionData {
// user: string;
// animal: 'cow' | 'pig';
// }
// }
app.use(
session({
store: new DynamoDBStore({
tableName: TABLE_NAME,
dynamoDBClient,
touchAfter: 60 * 5, // 5 minutes in seconds
}),
secret: 'yeah-dont-use-this',
cookie: {
maxAge: 60 * 60 * 1000, // one hour in milliseconds
// sameSite: 'none',
// If you set this to `true` then http://localhost will not work
// there will be no Set-Cookie response header
// secure: true,
},
// We implement `touch` to update the TTL on the session store
// We do not want unmodified sessions to be saved as that will cause a
// potentially massive cost issue on DynamoDB
resave: false,
saveUninitialized: false,
}),
);
// Add a fake login route that will set a session cookie
app.get('/login', (req, res) => {
console.log(`Session ID: ${req.session?.id}`);
// @ts-expect-error user is defined
req.session.user = 'test';
res.send('Logged in');
});
// Return a 200 response for all routes
app.get('/*', (req, res) => {
res.status(200).send('Hello world');
});
app.listen(Number.parseInt(PORT, 10), () => {
console.log(`Example app listening on port ${port}`);
});