WorkshopTasker/middleware/auth.ts
2023-05-11 06:03:22 +02:00

10 lines
448 B
TypeScript

import { defineNuxtRouteMiddleware, navigateTo, useFetch } from "nuxt/app";
export default defineNuxtRouteMiddleware(async (to, from) => {
// FIXME: Check authorisation in another way (401 unauthorised) instead querying api directly
const me = await useFetch("/api/users/me", {});
if (!me.error.value) return;
if (process.client) console.log(me.error.value);
return navigateTo({ path: "/login", query: { redirect: to.fullPath } });
});