2023-12-06 22:19:05 -05:00
|
|
|
import { SingleChapterResult } from "./types/slightlyDifferentStory";
|
2023-12-09 17:12:46 -05:00
|
|
|
import { IChapter } from "~/models/stories/chapter";
|
|
|
|
import { IStory } from "~/models/stories";
|
|
|
|
import { messages } from "~/lib/server/constants";
|
2023-12-06 22:19:05 -05:00
|
|
|
|
|
|
|
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,
|
|
|
|
);
|
2023-12-09 17:12:46 -05:00
|
|
|
if (error.value) {
|
|
|
|
return showError(error.value);
|
|
|
|
} else if (!story.value) {
|
|
|
|
return showError({ statusCode: 404, message: messages[404] });
|
|
|
|
}
|
2023-12-06 22:19:05 -05:00
|
|
|
console.log("fns", story, error);
|
|
|
|
if (!data?.value?.user && story.value?.currentChapter.loggedInOnly) {
|
|
|
|
return navigateTo("/login");
|
|
|
|
}
|
|
|
|
});
|