76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
export interface ISessionBroker {
|
|
|
|
// TODO: change in init()
|
|
createTable(): void
|
|
createSessionFromUserID(userID: number): Session
|
|
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
|
|
}
|
|
|
|
public toString() {
|
|
return this.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!")
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|