mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-30 23:23:02 +00:00 
			
		
		
		
	Allow same user to login on multiple devices
This commit is contained in:
		| @@ -47,6 +47,25 @@ SqlTiddlerDatabase.prototype.transaction = function(fn) { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| 	SqlTiddlerDatabase.prototype.createTables = function () { | 	SqlTiddlerDatabase.prototype.createTables = function () { | ||||||
|  | 		// 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([` | 	this.engine.runStatements([` | ||||||
| 		-- Users table | 		-- Users table | ||||||
| 		CREATE TABLE IF NOT EXISTS users ( | 		CREATE TABLE IF NOT EXISTS users ( | ||||||
| @@ -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() { | SqlTiddlerDatabase.prototype.listBags = function() { | ||||||
| 	const rows = this.engine.runStatementGetAll(` | 	const rows = this.engine.runStatementGetAll(` | ||||||
| 		SELECT bag_name, bag_id, accesscontrol, description | 		SELECT bag_name, bag_id, accesscontrol, description | ||||||
| @@ -985,32 +1017,30 @@ SqlTiddlerDatabase.prototype.listUsers = function() { | |||||||
| SqlTiddlerDatabase.prototype.createOrUpdateUserSession = function(userId, sessionId) { | SqlTiddlerDatabase.prototype.createOrUpdateUserSession = function(userId, sessionId) { | ||||||
| 	const currentTimestamp = new Date().toISOString(); | 	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 |  | ||||||
| 	`, { |  | ||||||
| 			$userId: userId, |  | ||||||
| 			$sessionId: sessionId, |  | ||||||
| 			$timestamp: currentTimestamp |  | ||||||
| 	}); |  | ||||||
|  |  | ||||||
| 	// If no existing session was updated, create a new one |  | ||||||
| 	if (updateResult.changes === 0) { |  | ||||||
| 	this.engine.runStatement(` | 	this.engine.runStatement(` | ||||||
| 					INSERT INTO sessions (user_id, session_id, created_at, last_accessed) | 		INSERT INTO sessions (session_id, user_id, created_at, last_accessed) | ||||||
| 					VALUES ($userId, $sessionId, $timestamp, $timestamp) | 		VALUES ($sessionId, $userId, $timestamp, $timestamp) | ||||||
| 	`, { | 	`, { | ||||||
| 		$userId: userId, | 		$userId: userId, | ||||||
| 		$sessionId: sessionId, | 		$sessionId: sessionId, | ||||||
| 		$timestamp: currentTimestamp | 		$timestamp: currentTimestamp | ||||||
| 	}); | 	}); | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return sessionId; | 	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) { | SqlTiddlerDatabase.prototype.createUserSession = function(userId, sessionId) { | ||||||
| 	const currentTimestamp = new Date().toISOString(); | 	const currentTimestamp = new Date().toISOString(); | ||||||
| 	this.engine.runStatement(` | 	this.engine.runStatement(` | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Thomas E Tuoti
					Thomas E Tuoti