WorkshopTasker/middleware/auth.ts

11 lines
448 B
TypeScript
Raw Permalink Normal View History

2023-05-11 06:03:22 +02:00
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 } });
});