Fixes response for protected API routes

This commit is contained in:
Christian Beutel
2025-09-19 15:11:09 +02:00
parent 5580832675
commit cf74a711e9
2 changed files with 10 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ import type { Settings } from '$lib/models/settings'
import PocketBase from 'pocketbase'
import { isRouteProtected } from '$lib/util/authorization_util'
import { json, redirect, text, type Handle } from '@sveltejs/kit'
import { error, json, redirect, text, type Handle } from '@sveltejs/kit'
import { sequence } from '@sveltejs/kit/hooks'
import { MeiliSearch } from 'meilisearch'
import { locale } from 'svelte-i18n'
@@ -60,6 +60,9 @@ const auth: Handle = async ({ event, resolve }) => {
// validate the user existence and if the path is acceesible
if (!pb.authStore.record && isRouteProtected(url)) {
if (url.pathname.startsWith("/api")) {
return json({ message: "Unauthorized" }, { status: 401 })
}
throw redirect(302, '/login?r=' + url.pathname);
} else if (pb.authStore.record && url.pathname === "/login") {
throw redirect(302, '/');

View File

@@ -2,9 +2,9 @@ import type { Trail } from "$lib/models/trail";
import { categories_index } from "$lib/stores/category_store";
import { feed_index } from "$lib/stores/feed_store";
import { trails_recommend } from "$lib/stores/trail_store";
import type { ServerLoad } from "@sveltejs/kit";
import type { Load } from "@sveltejs/kit";
export const load: ServerLoad = async ({ params, locals, fetch }) => {
export const load: Load = async ({ fetch }) => {
try {
await categories_index(fetch)
@@ -14,7 +14,9 @@ export const load: ServerLoad = async ({ params, locals, fetch }) => {
return { trails: trails ?? [], feed }
} catch (e) {
console.error(e)
if (!(e instanceof Error) || e.message !== "Unauthorized") {
console.error(e)
}
}
return { trails: [], feed: {items: [], page: 1, perPage: 1, totalItems: 0, totalPages: 0} }
return { trails: [], feed: { items: [], page: 1, perPage: 1, totalItems: 0, totalPages: 0 } }
};