|
| 1 | +export class SessionManager { |
| 2 | + private static instance: SessionManager; |
| 3 | + private timeoutId: number | null = null; |
| 4 | + private readonly TIMEOUT_MINUTES = 15; |
| 5 | + private readonly CHECK_INTERVAL = 60000; // Check every minute |
| 6 | + private lastActivity: number = Date.now(); |
| 7 | + private onSessionExpired?: () => void; |
| 8 | + |
| 9 | + private constructor() { |
| 10 | + this.setupActivityListeners(); |
| 11 | + this.startTimeoutCheck(); |
| 12 | + } |
| 13 | + |
| 14 | + public static getInstance(): SessionManager { |
| 15 | + if (!SessionManager.instance) { |
| 16 | + SessionManager.instance = new SessionManager(); |
| 17 | + } |
| 18 | + return SessionManager.instance; |
| 19 | + } |
| 20 | + |
| 21 | + public setSessionExpiredCallback(callback: () => void): void { |
| 22 | + this.onSessionExpired = callback; |
| 23 | + } |
| 24 | + |
| 25 | + public resetTimeout(): void { |
| 26 | + this.lastActivity = Date.now(); |
| 27 | + } |
| 28 | + |
| 29 | + public clearTimeout(): void { |
| 30 | + if (this.timeoutId) { |
| 31 | + clearInterval(this.timeoutId); |
| 32 | + this.timeoutId = null; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private setupActivityListeners(): void { |
| 37 | + const events = ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'click']; |
| 38 | + |
| 39 | + events.forEach(event => { |
| 40 | + document.addEventListener(event, () => { |
| 41 | + this.resetTimeout(); |
| 42 | + }, true); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private startTimeoutCheck(): void { |
| 47 | + this.timeoutId = setInterval(() => { |
| 48 | + const now = Date.now(); |
| 49 | + const timeSinceLastActivity = now - this.lastActivity; |
| 50 | + const timeoutMs = this.TIMEOUT_MINUTES * 60 * 1000; |
| 51 | + |
| 52 | + if (timeSinceLastActivity >= timeoutMs) { |
| 53 | + this.handleSessionExpired(); |
| 54 | + } |
| 55 | + }, this.CHECK_INTERVAL); |
| 56 | + } |
| 57 | + |
| 58 | + private handleSessionExpired(): void { |
| 59 | + this.clearTimeout(); |
| 60 | + if (this.onSessionExpired) { |
| 61 | + this.onSessionExpired(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public isTokenExpired(token: string): boolean { |
| 66 | + if (!token) return true; |
| 67 | + |
| 68 | + try { |
| 69 | + // Decode JWT token to check expiration |
| 70 | + const payload = JSON.parse(atob(token.split('.')[1])); |
| 71 | + const currentTime = Math.floor(Date.now() / 1000); |
| 72 | + |
| 73 | + // Check if token has expired |
| 74 | + if (payload.exp && payload.exp < currentTime) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + return false; |
| 79 | + } catch (error) { |
| 80 | + console.error('Error decoding token:', error); |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public checkServerRestart(token: string, currentServerStart: number): boolean { |
| 86 | + if (!token) return false; |
| 87 | + |
| 88 | + try { |
| 89 | + const payload = JSON.parse(atob(token.split('.')[1])); |
| 90 | + |
| 91 | + // Check if token was issued before server restart |
| 92 | + if (payload.server_start && payload.server_start !== currentServerStart) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } catch (error) { |
| 98 | + console.error('Error checking server restart:', error); |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments