From 0d368c5ce0b3a370d66e887e94497c28d5e91f7f Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 1 Oct 2022 23:23:12 +0800 Subject: feat: Revert to using built-in fetch since we are not required to use https after all Not needing https is due to the change of api url in dev from 127.0.0.1 to localhost. Which previously gave us an incompatible cookie that would not be set on clients, since it came from an unsecure context (non-https api). --- apps/kit/src/routes/(main)/+layout.server.ts | 37 +++++++++------------------- 1 file changed, 11 insertions(+), 26 deletions(-) (limited to 'apps/kit/src/routes') diff --git a/apps/kit/src/routes/(main)/+layout.server.ts b/apps/kit/src/routes/(main)/+layout.server.ts index ba0cd41..6bc7071 100644 --- a/apps/kit/src/routes/(main)/+layout.server.ts +++ b/apps/kit/src/routes/(main)/+layout.server.ts @@ -1,35 +1,20 @@ -import { api_base, CookieNames, is_development } from "$lib/configuration"; +import { api_base, CookieNames } from "$lib/configuration"; import { logError } from "$lib/logger"; import { error, redirect } from "@sveltejs/kit"; import type { LayoutServerLoad } from "./$types"; -import { request, Agent, Pool } from "undici"; export const load: LayoutServerLoad = async ({ routeId, cookies }) => { const isPublicRoute = routeId?.startsWith("(main)/(public)") ?? true; - const sessionCookie = cookies.get(CookieNames.session); - let sessionIsValid = false; - if (is_development()) { - sessionIsValid = (await request(api_base("_/valid-session"), { - throwOnError: false, - dispatcher: new Agent({ - factory: (origin, opts) => { - return new Pool(origin, opts); - } - }) - })).statusCode === 200; - } else { - sessionIsValid = (await fetch(api_base("_/valid-session"), { - headers: { - Cookie: CookieNames.session + "=" + sessionCookie, - } - }).catch((e) => { - logError(e); - throw error(503, { - message: "We are experiencing a service distruption! Have patience while we resolve the issue." - }) - })).ok; - } - + let sessionIsValid = (await fetch(api_base("_/valid-session"), { + headers: { + Cookie: CookieNames.session + "=" + cookies.get(CookieNames.session) + } + }).catch((e) => { + logError(e); + throw error(503, { + message: "We are experiencing a service distruption! Have patience while we resolve the issue." + }) + })).ok; if (sessionIsValid && isPublicRoute) { throw redirect(302, "/home"); } else if (!sessionIsValid && !isPublicRoute) { -- cgit v1.3