Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWS: Allow user to login on multiple devices #8863

Open
wants to merge 1 commit into
base: multi-wiki-support
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,26 @@ SqlTiddlerDatabase.prototype.transaction = function(fn) {
return this.engine.transaction(fn);
};

SqlTiddlerDatabase.prototype.createTables = function() {
SqlTiddlerDatabase.prototype.createTables = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation?!

// Drop and recreate sessions table
this.engine.runStatement(`
DROP TABLE IF EXISTS sessions
`);

this.engine.runStatement(`
CREATE TABLE sessions (
session_id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL,
created_at TEXT NOT NULL,
last_accessed TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id)
)
`);

this.engine.runStatement(`
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id)
`);

this.engine.runStatements([`
-- Users table
CREATE TABLE IF NOT EXISTS users (
Expand Down Expand Up @@ -194,6 +213,19 @@ SqlTiddlerDatabase.prototype.createTables = function() {
`]);
};

SqlTiddlerDatabase.prototype.updateSessionTimestamp = function(sessionId) {
const currentTimestamp = new Date().toISOString();

this.engine.runStatement(`
UPDATE sessions
SET last_accessed = $timestamp
WHERE session_id = $sessionId
`, {
$sessionId: sessionId,
$timestamp: currentTimestamp
});
};

SqlTiddlerDatabase.prototype.listBags = function() {
const rows = this.engine.runStatementGetAll(`
SELECT bag_name, bag_id, accesscontrol, description
Expand Down Expand Up @@ -984,33 +1016,31 @@ SqlTiddlerDatabase.prototype.listUsers = function() {

SqlTiddlerDatabase.prototype.createOrUpdateUserSession = function(userId, sessionId) {
const currentTimestamp = new Date().toISOString();

// First, try to update an existing session
const updateResult = this.engine.runStatement(`
UPDATE sessions
SET session_id = $sessionId, last_accessed = $timestamp
WHERE user_id = $userId

this.engine.runStatement(`
INSERT INTO sessions (session_id, user_id, created_at, last_accessed)
VALUES ($sessionId, $userId, $timestamp, $timestamp)
`, {
$userId: userId,
$sessionId: sessionId,
$timestamp: currentTimestamp
$userId: userId,
$sessionId: sessionId,
$timestamp: currentTimestamp
});

// If no existing session was updated, create a new one
if (updateResult.changes === 0) {
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.deleteExpiredSessions = function() {
const expiryTime = new Date();
expiryTime.setHours(expiryTime.getHours() - 24); // 24 hour expiry

this.engine.runStatement(`
DELETE FROM sessions
WHERE last_accessed < $expiryTime
`, {
$expiryTime: expiryTime.toISOString()
});
};

SqlTiddlerDatabase.prototype.createUserSession = function(userId, sessionId) {
const currentTimestamp = new Date().toISOString();
this.engine.runStatement(`
Expand Down
Loading