From ac77764b3d42da39a2b85325aedb0767f20ca90a Mon Sep 17 00:00:00 2001 From: Dan6erbond Date: Wed, 12 Jan 2022 20:28:20 +0100 Subject: [PATCH] [FIX] Fix Client Functions and Add Base Path Option (#63) Fixes #54. --- src/client/helpers.ts | 19 +++++++++++++++++++ src/client/signIn.ts | 7 ++++--- src/client/signOut.ts | 10 +++++++--- src/client/types.ts | 3 +++ 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/client/helpers.ts create mode 100644 src/client/types.ts diff --git a/src/client/helpers.ts b/src/client/helpers.ts new file mode 100644 index 0000000..320ef4e --- /dev/null +++ b/src/client/helpers.ts @@ -0,0 +1,19 @@ +function mergePath(basePaths: (string | null)[], path: string) { + if (path.startsWith("/")) { + path = path.slice(1); + } + let retPath; + for (let basePath of basePaths) { + if (basePath !== null) { + if (!basePath.startsWith("/")) { + basePath = "/" + basePath; + } + if (!basePath.endsWith("/")) { + basePath = basePath + "/"; + } + retPath = basePath + path; + } + } + + return retPath; +} diff --git a/src/client/signIn.ts b/src/client/signIn.ts index c6b372d..7eb347e 100644 --- a/src/client/signIn.ts +++ b/src/client/signIn.ts @@ -1,14 +1,15 @@ /* import { goto } from "@sveltejs/kit/assets/runtime/app/navigation"; import { page } from "@sveltejs/kit/assets/runtime/app/stores"; */ import type { LoadInput } from "@sveltejs/kit"; +import type { ClientRequestConfig } from "./types"; -interface SignInConfig { +interface SignInConfig extends ClientRequestConfig { redirectUrl?: string; } export async function signIn(provider: string, data?: any, config?: SignInConfig) { if (data) { - const path = `/api/auth/callback/${provider}`; + const path = mergePath(["/api/auth", config?.basePath ?? null], `/callback/${provider}`); const res = await fetch(path, { method: "POST", headers: { @@ -34,7 +35,7 @@ export async function signIn(provider: string, data?: any, config?: SignInConfig redirect: redirectUrl ?? "/", }; const query = new URLSearchParams(queryData); - const path = `/api/auth/login/${provider}?${query}`; + const path = mergePath(["/api/auth", config?.basePath ?? null], `/signin/${provider}?${query}`); return path; // await goto(path); } diff --git a/src/client/signOut.ts b/src/client/signOut.ts index a6d91cf..6354689 100644 --- a/src/client/signOut.ts +++ b/src/client/signOut.ts @@ -1,14 +1,18 @@ /* import { session as session$ } from "$app/stores"; */ -export async function signOut() { - let res = await fetch("/api/auth/signout", { method: "POST" }); +import type { ClientRequestConfig } from "./types"; + +export async function signOut(config?: ClientRequestConfig) { + let res = await fetch(mergePath(["/api/auth", config?.basePath ?? null], "signout"), { + method: "POST", + }); const { signout } = await res.json(); if (!signout) { throw new Error("Sign out not successful!"); } - res = await fetch("/api/auth/session"); + res = await fetch(mergePath(["/api/auth", config?.basePath ?? null], "session")); const session = await res.json(); return session; diff --git a/src/client/types.ts b/src/client/types.ts new file mode 100644 index 0000000..db05348 --- /dev/null +++ b/src/client/types.ts @@ -0,0 +1,3 @@ +export interface ClientRequestConfig { + basePath?: string; +}