Skip to content

Commit

Permalink
TiddlyWiki#8833 fix issue with multiple user sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
webplusai committed Dec 22, 2024
1 parent 02a9fdc commit 8214e7b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/tiddlywiki/multiwikiserver/auth/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
1 change: 1 addition & 0 deletions plugins/tiddlywiki/multiwikiserver/modules/mws-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
`,`
Expand Down Expand Up @@ -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(`
Expand Down

0 comments on commit 8214e7b

Please sign in to comment.