1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-08 16:30:26 +00:00

#8833 fix issue with multiple user sessions (#8845)

This commit is contained in:
webplusai 2024-12-22 20:59:01 +00:00 committed by GitHub
parent 02a9fdcd06
commit 1c5648826e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View File

@ -34,7 +34,7 @@ Authenticator.prototype.hashPassword = function(password) {
Authenticator.prototype.createSession = function(userId) {
var sessionId = crypto.randomBytes(16).toString("hex");
// Store the session in your database or in-memory store
this.sqlTiddlerDatabase.createOrUpdateUserSession(userId, sessionId);
this.sqlTiddlerDatabase.createUserSession(userId, sessionId);
return sessionId;
};

View File

@ -397,6 +397,7 @@ Server.prototype.authenticateUser = function(request, response) {
delete user.password;
const userRole = this.sqlTiddlerDatabase.getUserRoles(user.user_id);
user['isAdmin'] = userRole?.role_name?.toLowerCase() === 'admin'
user['sessionId'] = session_id
return user
};

View File

@ -64,7 +64,7 @@ SqlTiddlerDatabase.prototype.createTables = function() {
session_id TEXT NOT NULL,
created_at TEXT NOT NULL,
last_accessed TEXT NOT NULL,
PRIMARY KEY (user_id),
PRIMARY KEY (session_id),
FOREIGN KEY (user_id) REFERENCES users(user_id)
)
`,`
@ -994,6 +994,20 @@ SqlTiddlerDatabase.prototype.createOrUpdateUserSession = function(userId, sessio
return sessionId;
};
SqlTiddlerDatabase.prototype.createUserSession = function(userId, sessionId) {
const currentTimestamp = new Date().toISOString();
this.engine.runStatement(`
INSERT INTO sessions (user_id, session_id, created_at, last_accessed)
VALUES ($userId, $sessionId, $timestamp, $timestamp)
`, {
$userId: userId,
$sessionId: sessionId,
$timestamp: currentTimestamp
});
return sessionId;
};
SqlTiddlerDatabase.prototype.findUserBySessionId = function(sessionId) {
// First, get the user_id from the sessions table
const sessionResult = this.engine.runStatementGet(`