/** * Auth utilities for managing authentication state */ /** * Get the authentication token from cookies or localStorage */ export function getAuthToken(): string | null { return document.cookie .split('; ') .find(row => row.startsWith('Authorization=')) ?.split('=')[1] || localStorage.getItem('Authorization'); } /** * Check if the user is logged in */ export function isLoggedIn(): boolean { return !!getAuthToken(); } /** * Logout the user by removing auth tokens and redirecting */ export async function logoutUser(): Promise { const token = getAuthToken(); if (token) { try { await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/logout`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }).catch(err => { console.warn('Server logout failed:', err); }); } catch (error) { console.warn('Error during server logout:', error); } } // Remove the auth token from cookies document.cookie = 'Authorization=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; SameSite=Strict'; // Remove from localStorage as backup localStorage.removeItem('Authorization'); // Redirect to login page window.location.href = '/auth/login'; }