Spaces:
Sleeping
Sleeping
File size: 1,142 Bytes
6218385 38dd789 6218385 38dd789 6218385 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | /**
* Authentication utilities and route protection
*/
import { API_BASE_URL } from '../config/api';
export const isAuthenticated = (): boolean => {
const token = localStorage.getItem('access_token');
return !!token;
};
export const clearAuth = (): void => {
localStorage.removeItem('access_token');
// Clear any other auth-related data
sessionStorage.clear();
};
export const getAuthToken = (): string | null => {
return localStorage.getItem('access_token');
};
export const setAuthToken = (token: string): void => {
localStorage.setItem('access_token', token);
};
export const logout = async (): Promise<void> => {
const token = getAuthToken();
if (token) {
try {
// Call backend logout endpoint
await fetch(`${API_BASE_URL}/api/auth/logout`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
} catch (error) {
console.error('Logout API call failed:', error);
// Continue with local cleanup even if API call fails
}
}
// Clear local auth data
clearAuth();
};
|