homebox/frontend/middleware/auth.ts

14 lines
267 B
TypeScript
Raw Permalink Normal View History

export default defineNuxtRouteMiddleware(async () => {
const ctx = useAuthContext();
const api = useUserApi();
if (!ctx.user) {
const { data, error } = await api.user.self();
if (error) {
navigateTo("/");
}
ctx.user = data.item;
}
});