-
Notifications
You must be signed in to change notification settings - Fork 0
/
other.ts
63 lines (54 loc) · 1.72 KB
/
other.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
63
/* eslint-disable no-console */
import session from 'express-session';
import DynamoDBStore from 'connect-dynamodb';
import * as dynamodb from '@aws-sdk/client-dynamodb';
import express from 'express';
const { TABLE_NAME = 'connect-dynamodb-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';
// }
// }
const DynamoDBStoreSession = DynamoDBStore(session);
app.use(
session({
store: new DynamoDBStoreSession({
client: dynamoDBClient,
hashKey: 'id',
table: TABLE_NAME,
}),
name: 'sid',
secret: 'yeah-dont-use-this',
cookie: {
maxAge: 60 * 60 * 1000, // one hour in milliseconds
// sameSite: 'none',
// 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';
// req.session.animal = 'cow';
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}`);
});