import { BetterFetchPlugin, createFetch } from '@better-fetch/fetch';
import { auth, signOut } from './auth';
import { BACKEND_URL } from './constants';
const authPlugin: BetterFetchPlugin = {
id: 'auth',
name: 'Auth Plugin',
init: async (url, options = {}) => {
try {
const session = await auth();
const token = session?.user?.accessToken;
if (!token) {
console.log('Token is missing, signing out');
await signOut();
throw new Error('Authentication token is missing');
}
options.headers = {
...(options.headers ?? {}),
Authorization: `Bearer ${token}`
};
return { url, options };
} catch (error) {
console.error('Error in auth plugin:', error);
throw error;
}
}
};
export const adminFetch = createFetch({
baseURL: `${BACKEND_URL}/admin`,
plugins: [authPlugin],
timeout: 10000,
throw: false
});
import { BetterFetchPlugin, createFetch } from '@better-fetch/fetch';
import { auth, signOut } from './auth';
import { BACKEND_URL } from './constants';
const authPlugin: BetterFetchPlugin = {
id: 'auth',
name: 'Auth Plugin',
init: async (url, options = {}) => {
try {
const session = await auth();
const token = session?.user?.accessToken;
if (!token) {
console.log('Token is missing, signing out');
await signOut();
throw new Error('Authentication token is missing');
}
options.headers = {
...(options.headers ?? {}),
Authorization: `Bearer ${token}`
};
return { url, options };
} catch (error) {
console.error('Error in auth plugin:', error);
throw error;
}
}
};
export const adminFetch = createFetch({
baseURL: `${BACKEND_URL}/admin`,
plugins: [authPlugin],
timeout: 10000,
throw: false
});