push update socket data dan token
This commit is contained in:
@@ -1,63 +1,91 @@
|
||||
// server/utils/sessionStore.ts
|
||||
// Simple in-memory session store (for development)
|
||||
// In production, use Redis or a database
|
||||
|
||||
import { getCookie } from 'h3'
|
||||
import { randomBytes } from 'crypto'
|
||||
// Lightweight in-memory session store with automatic cleanup
|
||||
|
||||
interface SessionData {
|
||||
user: any;
|
||||
accessToken: string;
|
||||
idToken: string;
|
||||
refreshToken: string;
|
||||
expiresAt: number;
|
||||
createdAt: number;
|
||||
user: any;
|
||||
accessToken: string;
|
||||
idToken: string;
|
||||
refreshToken: string;
|
||||
expiresAt: number;
|
||||
createdAt: number;
|
||||
scope?: string;
|
||||
status?: string;
|
||||
}
|
||||
|
||||
// In-memory session storage
|
||||
const sessions = new Map<string, SessionData>();
|
||||
|
||||
// Clean up expired sessions every 5 minutes
|
||||
// Cleanup expired sessions every 5 minutes
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [sessionId, session] of sessions.entries()) {
|
||||
if (session.expiresAt < now) {
|
||||
sessions.delete(sessionId);
|
||||
}
|
||||
const now = Date.now();
|
||||
let cleanedCount = 0;
|
||||
|
||||
for (const [sessionId, session] of sessions.entries()) {
|
||||
if (session.expiresAt < now) {
|
||||
sessions.delete(sessionId);
|
||||
cleanedCount++;
|
||||
}
|
||||
}, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
if (cleanedCount > 0) {
|
||||
console.log(`🧹 Cleaned up ${cleanedCount} expired sessions. Active sessions: ${sessions.size}`);
|
||||
}
|
||||
}, 5 * 60 * 1000); // Every 5 minutes
|
||||
|
||||
export function createSession(data: SessionData): string {
|
||||
// Generate a secure random session ID
|
||||
const sessionId = randomBytes(32).toString('hex');
|
||||
sessions.set(sessionId, data);
|
||||
return sessionId;
|
||||
// Generate random session ID
|
||||
const sessionId = Array.from({ length: 32 }, () =>
|
||||
Math.floor(Math.random() * 16).toString(16)
|
||||
).join('');
|
||||
|
||||
sessions.set(sessionId, data);
|
||||
console.log(`✅ Session created: ${sessionId.substring(0, 8)}... (Total active: ${sessions.size})`);
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
export function getSession(sessionId: string): SessionData | null {
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if expired
|
||||
if (session.expiresAt < Date.now()) {
|
||||
sessions.delete(sessionId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
const session = sessions.get(sessionId);
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if expired
|
||||
if (session.expiresAt < Date.now()) {
|
||||
sessions.delete(sessionId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export function deleteSession(sessionId: string): void {
|
||||
sessions.delete(sessionId);
|
||||
sessions.delete(sessionId);
|
||||
console.log(`🗑️ Session deleted: ${sessionId.substring(0, 8)}... (Remaining: ${sessions.size})`);
|
||||
}
|
||||
|
||||
// Helper function to get session from cookie (for use in API handlers)
|
||||
// Helper function to get session from cookie (for API handlers)
|
||||
export async function getSessionFromCookie(event: any): Promise<SessionData | null> {
|
||||
const sessionId = getCookie(event, 'user_session');
|
||||
if (!sessionId) {
|
||||
return null;
|
||||
}
|
||||
return getSession(sessionId);
|
||||
const { getCookie } = await import('h3');
|
||||
const sessionId = getCookie(event, 'user_session');
|
||||
|
||||
if (!sessionId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getSession(sessionId);
|
||||
}
|
||||
|
||||
// Get session stats
|
||||
export function getSessionStats() {
|
||||
return {
|
||||
totalSessions: sessions.size,
|
||||
sessions: Array.from(sessions.entries()).map(([id, session]) => ({
|
||||
id: id.substring(0, 8) + '...',
|
||||
user: session.user?.email || session.user?.name,
|
||||
expiresAt: new Date(session.expiresAt).toISOString(),
|
||||
isExpired: session.expiresAt < Date.now()
|
||||
}))
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user