26 lines
844 B
TypeScript
26 lines
844 B
TypeScript
import type { SessionUser } from '#shared/types';
|
|
|
|
export type Session = { user: SessionUser | null; plan: string };
|
|
|
|
/**
|
|
* Fetch the current session (user + plan) once per request and share it across components.
|
|
*
|
|
* Uses `useRequestFetch` so that during SSR the internal `/api/auth/me` call forwards the
|
|
* incoming request's cookies (a plain `$fetch` would send none and the guard would bounce an
|
|
* authenticated admin/dashboard page to /login). On the client it behaves like `$fetch`.
|
|
*/
|
|
export function useSession() {
|
|
const requestFetch = useRequestFetch();
|
|
return useAsyncData<Session>(
|
|
'session',
|
|
async () => {
|
|
try {
|
|
return await requestFetch<Session>('/api/auth/me');
|
|
} catch {
|
|
return { user: null, plan: 'free' };
|
|
}
|
|
},
|
|
{ default: () => ({ user: null, plan: 'free' }) },
|
|
);
|
|
}
|