storing sessions
This commit is contained in:
parent
1b6f140b25
commit
b1ef0821de
7 changed files with 1576 additions and 1 deletions
BIN
container-data/database.sqlite
Normal file
BIN
container-data/database.sqlite
Normal file
Binary file not shown.
11
database.js
Normal file
11
database.js
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
const { Sequelize } = require('sequelize');
|
||||||
|
|
||||||
|
const sequelize = new Sequelize({
|
||||||
|
dialect: 'sqlite',
|
||||||
|
storage: './data/database.sqlite',
|
||||||
|
define: {
|
||||||
|
timestamps: false
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = sequelize;
|
||||||
|
|
@ -5,6 +5,8 @@ services:
|
||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
|
volumes:
|
||||||
|
- ./container-data:/app/data
|
||||||
|
|
||||||
caddy:
|
caddy:
|
||||||
image: caddy:2
|
image: caddy:2
|
||||||
|
|
|
||||||
17
index.js
17
index.js
|
|
@ -2,18 +2,33 @@ const express = require('express');
|
||||||
const uuid = require("uuid");
|
const uuid = require("uuid");
|
||||||
const cookieParser = require('cookie-parser');
|
const cookieParser = require('cookie-parser');
|
||||||
|
|
||||||
|
const sequelize = require('./database');
|
||||||
|
const Session = require('./models/Session');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
|
sequelize.sync()
|
||||||
|
.then(() => {
|
||||||
|
console.log('Database synced');
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('Error syncing the database:', err);
|
||||||
|
});
|
||||||
|
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
app.set('views', './views');
|
app.set('views', './views');
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use(async (req, res, next) => {
|
||||||
if (!req.cookies.sessionUuid) {
|
if (!req.cookies.sessionUuid) {
|
||||||
const sessionUuid = uuid.v7();
|
const sessionUuid = uuid.v7();
|
||||||
res.cookie('sessionUuid', sessionUuid, { httpOnly: true, maxAge: 86400000 });
|
res.cookie('sessionUuid', sessionUuid, { httpOnly: true, maxAge: 86400000 });
|
||||||
|
const session = await Session.create({
|
||||||
|
uuid: sessionUuid,
|
||||||
|
created_at: new Date().toISOString()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
17
models/Session.js
Normal file
17
models/Session.js
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
const { DataTypes } = require('sequelize');
|
||||||
|
const sequelize = require('../database');
|
||||||
|
|
||||||
|
const Session = sequelize.define('Session', {
|
||||||
|
uuid: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
allowNull: false,
|
||||||
|
unique: true,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = Session;
|
||||||
1528
package-lock.json
generated
1528
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,6 +5,8 @@
|
||||||
"cookie-parser": "^1.4.7",
|
"cookie-parser": "^1.4.7",
|
||||||
"ejs": "^3.1.10",
|
"ejs": "^3.1.10",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
"sequelize": "^6.37.5",
|
||||||
|
"sqlite3": "^5.1.7",
|
||||||
"uuid": "^11.0.5"
|
"uuid": "^11.0.5"
|
||||||
},
|
},
|
||||||
"description": "",
|
"description": "",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue