mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Use util fetch for hook as well
This commit is contained in:
@@ -1,30 +1,25 @@
|
|||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from "@sveltejs/kit";
|
||||||
import { API_BASE_PREFIX } from "$lib/server/utils";
|
import { API_BASE_PREFIX, apiRequest } from "$lib/server/utils";
|
||||||
import type { User } from "$lib/db-schema";
|
|
||||||
|
|
||||||
export const handle = async ({ event, resolve }) => {
|
export const handle = async ({ event, resolve }) => {
|
||||||
if (!event.url.pathname.startsWith("/api/")) {
|
if (!event.url.pathname.startsWith("/api/")) {
|
||||||
const userData = await event.fetch(`${API_BASE_PREFIX}/account`, {
|
const userData = await apiRequest(event.fetch, `${API_BASE_PREFIX}/account`, "GET", {
|
||||||
method: "GET",
|
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${event.cookies.get("session_token")}`,
|
|
||||||
Accept: "application/vnd.pgrst.object+json"
|
Accept: "application/vnd.pgrst.object+json"
|
||||||
}
|
},
|
||||||
|
returnData: true
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!userData.ok && !["/login", "/register"].includes(event.url.pathname)) {
|
if (!userData.success && !["/login", "/register"].includes(event.url.pathname)) {
|
||||||
throw redirect(303, "/login");
|
throw redirect(303, "/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userData.ok) {
|
if (userData.success) {
|
||||||
if (["/login", "/register"].includes(event.url.pathname)) {
|
if (["/login", "/register"].includes(event.url.pathname)) {
|
||||||
throw redirect(303, "/");
|
throw redirect(303, "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
const user: User = await userData.json();
|
event.locals.user = userData.data;
|
||||||
|
|
||||||
event.locals.user = user;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,13 +29,12 @@ export const load: PageServerLoad = async ({ parent, params, fetch }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const actions: Actions = {
|
export const actions: Actions = {
|
||||||
editArticle: async ({ fetch, cookies, request, params }) => {
|
editArticle: async ({ fetch, request, params }) => {
|
||||||
const data = await request.formData();
|
const data = await request.formData();
|
||||||
const coverFile = data.get("cover-image") as File;
|
const coverFile = data.get("cover-image") as File;
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"Content-Type": "application/octet-stream",
|
"Content-Type": "application/octet-stream",
|
||||||
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
||||||
Accept: "application/vnd.pgrst.object+json",
|
Accept: "application/vnd.pgrst.object+json",
|
||||||
"X-Website-Id": params.websiteId
|
"X-Website-Id": params.websiteId
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ export const actions: Actions = {
|
|||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
website_id: params.websiteId,
|
website_id: params.websiteId,
|
||||||
prefix: data.get("domain-prefix") as string
|
prefix: data.get("domain-prefix")
|
||||||
},
|
},
|
||||||
successMessage: "Successfully created/updated domain prefix"
|
successMessage: "Successfully created/updated domain prefix"
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user