This repository has no description
0

Configure Feed

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

semble / src / webapp / lib / auth / token.ts
1.0 kB 38 lines
1import { ENABLE_AUTH_LOGGING } from '@/lib/auth/constants'; 2 3export const isTokenExpiringSoon = ( 4 token: string | null | undefined, 5): boolean => { 6 if (!token) return true; 7 8 const bufferSeconds = parseInt( 9 process.env.NEXT_PUBLIC_ACCESS_TOKEN_EXPIRY_BUFFER_SECONDS || '300', 10 10, 11 ); 12 13 try { 14 // Validate JWT structure first 15 const parts = token.split('.'); 16 if (parts.length !== 3) return true; 17 18 const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString()); 19 const userDid = payload.did || 'unknown'; 20 21 // Ensure exp claim exists and is a number 22 if (!payload.exp || typeof payload.exp !== 'number') return true; 23 24 const expiry = payload.exp * 1000; 25 const bufferTime = bufferSeconds * 1000; 26 const isExpiring = Date.now() >= expiry - bufferTime; 27 28 if (isExpiring && ENABLE_AUTH_LOGGING) { 29 console.log( 30 `[isTokenExpiringSoon] Token expiring soon for user: ${userDid}`, 31 ); 32 } 33 34 return isExpiring; 35 } catch { 36 return true; 37 } 38};