This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / webapp / services / auth / CookieAuthService.server.ts
1.3 kB 48 lines
1import { cookies } from 'next/headers'; 2 3const ENABLE_AUTH_LOGGING = true; 4 5export interface AuthTokens { 6 accessToken: string | null; 7 refreshToken: string | null; 8} 9 10export class ServerCookieAuthService { 11 // Server-side: read from Next.js cookies 12 static async getTokens(): Promise<AuthTokens> { 13 const cookieStore = await cookies(); 14 return { 15 accessToken: cookieStore.get('accessToken')?.value || null, 16 refreshToken: cookieStore.get('refreshToken')?.value || null, 17 }; 18 } 19 20 // Check if token is expired (same logic as client) 21 static isTokenExpired( 22 token: string | null, 23 bufferMinutes: number = 5, 24 ): boolean { 25 if (!token) return true; 26 27 try { 28 const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()); 29 const userDid = payload.did || 'unknown'; 30 const expiry = payload.exp * 1000; 31 const bufferTime = bufferMinutes * 60 * 1000; 32 const isExpired = Date.now() >= expiry - bufferTime; 33 34 if (isExpired && ENABLE_AUTH_LOGGING) { 35 console.log( 36 `[ServerCookieAuthService] Token expired for user: ${userDid}`, 37 ); 38 } 39 40 return isExpired; 41 } catch { 42 if (ENABLE_AUTH_LOGGING) { 43 console.log(`[ServerCookieAuthService] Invalid token format`); 44 } 45 return true; 46 } 47 } 48}