From e528f1351a74068efbd4715867d1e28189e52cdd Mon Sep 17 00:00:00 2001 From: counterweight Date: Mon, 10 Feb 2025 12:17:15 +0100 Subject: [PATCH] sessions hav expiry dates --- src/constants.js | 5 +++++ src/models/Session.js | 4 ++++ src/services/sessionService.js | 20 +++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/constants.js diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..f296e0b --- /dev/null +++ b/src/constants.js @@ -0,0 +1,5 @@ +const DEFAULT_SESSION_DURATION_SECONDS = 60 * 60 * 24 * 30; + +module.exports = { + DEFAULT_SESSION_DURATION_SECONDS +} \ No newline at end of file diff --git a/src/models/Session.js b/src/models/Session.js index 136cabd..04a9bd2 100644 --- a/src/models/Session.js +++ b/src/models/Session.js @@ -11,6 +11,10 @@ const Session = sequelize.define('Session', { created_at: { type: DataTypes.DATE, allowNull: false + }, + expires_at: { + type: DataTypes.DATE, + allowNull: false } }, { tableName: 'session' diff --git a/src/services/sessionService.js b/src/services/sessionService.js index a3b652d..9474f04 100644 --- a/src/services/sessionService.js +++ b/src/services/sessionService.js @@ -3,10 +3,18 @@ const uuid = require("uuid"); const Session = require('../models/Session'); const SessionNpubbed = require('../models/SessionNpubbed'); +const constants = require('../constants'); + async function createSession(sessionUuid) { + + const currentTimestamp = new Date(); + const expiryTimestamp = new Date(currentTimestamp.getTime()); + expiryTimestamp.setSeconds(expiryTimestamp.getSeconds() + constants.DEFAULT_SESSION_DURATION_SECONDS); + await Session.create({ uuid: sessionUuid, - created_at: new Date().toISOString() + created_at: currentTimestamp.toISOString(), + expires_at: expiryTimestamp.toISOString() }); } @@ -26,6 +34,16 @@ async function isSessionAlreadyRelatedToNpub(sessionUuid, npub) { return false; } +/* async function getNpubRelatedToSession(sessionUuid) { + if (SessionNpubbed.findOne({ + where: { 'sessionUuid': sessionUuid } + })) +} + +async function isSessionAuthorized(sessionUuid) { + +} */ + exports.createSession = createSession; exports.relateSessionToNpub = relateSessionToNpub; exports.isSessionAlreadyRelatedToNpub = isSessionAlreadyRelatedToNpub;