import { AuthenticationConfiguration } from '../types'; export const getCookieValue = (name: string): string | null => { try { const match = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)'); return match ? match.pop()! : null; } catch (error) { return null; } }; export const setCookieValue = (name: string, value: string): void => { document.cookie = `${name}=${value}; Path=/;`; }; export const logout = (): void => { setCookieValue('authToken', ''); setCookieValue('refreshToken', ''); }; export const refreshAccessToken = async (authConfig?: AuthenticationConfiguration): Promise => { if (!authConfig) { throw new Error('No auth configuration available'); } const refreshToken = getCookieValue('refreshToken'); if (!refreshToken) { throw new Error('You are not logged in'); } const response = await fetch(`${authConfig.redirectUrl}?grant_type=refresh_token&refresh_token=${refreshToken}`, { method: 'GET', credentials: 'include', }); if (!response.ok) { logout(); throw new Error('Failed to refresh token'); } const data = await response.json(); setCookieValue('authToken', data.access_token); return data.access_token; };