homebox/frontend/middleware/auth.ts

18 lines
336 B
TypeScript
Raw Normal View History

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