templates2.Saas.Nuxt/app/composables/useSession.ts
w4c-templates d447340474
Some checks failed
CI / quality (push) Has been cancelled
chore(template): publish Saas.Nuxt
2026-10-11 23:25:14 +03:00

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' }) },
);
}