next/lib/client/middleware.ts

24 lines
820 B
TypeScript
Raw Normal View History

import { SingleChapterResult } from "./types/slightlyDifferentStory";
import { IChapter } from "~/models/stories/chapter";
import { IStory } from "~/models/stories";
import { messages } from "~/lib/server/constants";
export const storyMiddleware = defineNuxtRouteMiddleware(async (to, from) => {
const { getSession } = useAuth();
await getSession({ force: true });
const { data } = useAuth();
console.log("to n from", to, from, data);
const { data: story, error } = await useApiFetch<SingleChapterResult>(
to.path,
);
if (error.value) {
return showError(error.value);
} else if (!story.value) {
return showError({ statusCode: 404, message: messages[404] });
}
console.log("fns", story, error);
if (!data?.value?.user && story.value?.currentChapter.loggedInOnly) {
return navigateTo("/login");
}
});