Files
ember-market-frontend/lib/auth-helpers.ts
2025-03-10 17:39:37 +00:00

33 lines
1.0 KiB
TypeScript

'use client';
// Helper function to verify authentication with the local API
export async function verifyAuth(token: string) {
try {
// Use a properly formed URL with origin
const origin = typeof window !== 'undefined' ? window.location.origin : '';
const authEndpoint = new URL('/api/auth/me', origin).toString();
console.log(`Verifying auth with ${authEndpoint} using token: ${token.substring(0, 10)}...`);
const response = await fetch(authEndpoint, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
// Prevent caching of auth requests
cache: 'no-store'
});
if (!response.ok) {
console.error(`Auth verification failed: ${response.status}`);
return false;
}
return await response.json();
} catch (error) {
console.error("Authentication verification failed:", error);
console.error("Error details:", error instanceof Error ? error.message : 'Unknown error');
return false;
}
}