Fixed bugs and redid interface

This commit is contained in:
Christian Risi 2025-06-30 11:54:43 +00:00
parent 363c25045c
commit 125409fda3
3 changed files with 59 additions and 7 deletions

View File

@ -2,22 +2,69 @@ export interface ISessionBroker {
createTable(): void
createSessionFromUserID(userID: number): Session
getSessionFromUserID(userID: number) : Session|null
getSessionFromToken(token: string) : Session|null
getSessionFromUserID(userID: number): Session | null
getSessionFromToken(token: string): Session | null
}
export class Session {
public sessionID: number
public userID: number
public sessionToken: string
constructor(
sessionID: number,
userID: number,
sessionToken: string
) {
this.sessionID = sessionID
this.userID = userID
this.sessionToken = sessionToken
}
}
}
export class SessionApp {
private static broker: ISessionBroker
private static initialized: boolean = false
public static init(broker: ISessionBroker) {
if (SessionApp.initialized) {
// UGLY: make this Error more specific
throw Error("SessionApp has already been initialized")
}
SessionApp.initialized = true
SessionApp.broker = broker
SessionApp.broker.createTable()
}
public static createSessionFromUserID(userID: number): Session {
SessionApp.assertInitialized()
return SessionApp.broker.createSessionFromUserID(userID)
}
public static getSessionFromUserID(userID: number): Session | null {
SessionApp.assertInitialized()
return SessionApp.broker.getSessionFromUserID(userID)
}
public static getSessionFromToken(token: string): Session | null {
SessionApp.assertInitialized()
return SessionApp.broker.getSessionFromToken(token)
}
private static assertInitialized() {
if (!SessionApp.initialized) {
// UGLY: make more specific
throw Error("SessionApp has't been Initialized yet!")
}
}
}

View File

@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER UNIQUE NOT NULL
session_token TEXT UNIQUE NOT NULL ,
user_id INTEGER UNIQUE NOT NULL,
session_token TEXT UNIQUE NOT NULL,
FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE
);

View File

@ -1,4 +1,5 @@
import { Session, type ISessionBroker } from "$lib/classes/sessions"
import { logger } from "$lib/utils/logger"
import { SSLSnifferApp } from "./sqlite"
class SessionDB {
@ -32,6 +33,8 @@ export class SessionDBBroker implements ISessionBroker {
// UGLY: make more specific
throw Error("SessionBroker has already been initialized")
}
logger.debug("Correctly initialized", "SessionDBBroker")
}
@ -41,8 +44,8 @@ export class SessionDBBroker implements ISessionBroker {
`
CREATE TABLE IF NOT EXISTS sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER UNIQUE NOT NULL
session_token TEXT UNIQUE NOT NULL ,
user_id INTEGER UNIQUE NOT NULL,
session_token TEXT UNIQUE NOT NULL,
FOREIGN KEY (user_id) references users(user_id) ON DELETE CASCADE
);
`
@ -101,6 +104,7 @@ export class SessionDBBroker implements ISessionBroker {
return new Session(
candidateSession.session_id,
candidateSession.user_id,
candidateSession.session_token
)
}
@ -114,6 +118,7 @@ export class SessionDBBroker implements ISessionBroker {
return new Session(
candidateSession.session_id,
candidateSession.user_id,
candidateSession.session_token
)