From 98f73f36ead0dd744cde5052ae3ffa3a16c41e2f Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Mon, 24 May 2021 16:20:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Refactor=20`getPath`=20to=20`get?= =?UTF-8?q?Url`=20and=20add=20`getPath`=20to=20fix=20detection=20of=20rout?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth.ts | 17 +++++++++++++---- src/providers/base.ts | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index f7250c1..caa192c 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -26,6 +26,10 @@ interface AuthCallbacks { export class Auth { constructor(private readonly config?: AuthConfig) {} + get basePath() { + return this.config?.basePath ?? "/api/auth"; + } + getJwtSecret() { if (this.config?.jwtSecret) { return this.config?.jwtSecret; @@ -68,9 +72,14 @@ export class Auth { return this.config?.host ?? `http://${host}`; } - getPath(path: string, host?: string) { - const uri = join([this.config?.basePath ?? "/api/auth", path]); - return new URL(uri, this.getBaseUrl(host)).pathname; + getPath(path: string) { + const pathname = join([this.basePath, path]); + return pathname; + } + + getUrl(path: string, host?: string) { + const pathname = this.getPath(path); + return new URL(pathname, this.getBaseUrl(host)).href; } setToken(headers: Headers, newToken: JWT | any) { @@ -129,7 +138,7 @@ export class Auth { async handleEndpoint(request: ServerRequest): Promise { const { path, headers, method, host } = request; - if (path === this.getPath("signout", host)) { + if (path === this.getPath("signout")) { const token = this.setToken(headers, {}); const jwt = this.signToken(token); diff --git a/src/providers/base.ts b/src/providers/base.ts index aef5bb4..ce83396 100644 --- a/src/providers/base.ts +++ b/src/providers/base.ts @@ -16,15 +16,15 @@ export abstract class Provider { } getUri(svelteKitAuth: Auth, path: string, host?: string) { - return `http://${host}${path}`; + return svelteKitAuth.getUrl(path, host); } getCallbackUri(svelteKitAuth: Auth, host?: string) { - return svelteKitAuth.getPath(`${"/api/auth/callback/"}${this.id}`, host); + return this.getUri(svelteKitAuth, `${"/callback/"}${this.id}`, host); } getSigninUri(svelteKitAuth: Auth, host?: string) { - return svelteKitAuth.getPath(`${"/api/auth/signin/"}${this.id}`, host); + return this.getUri(svelteKitAuth, `${"/signin/"}${this.id}`, host); } abstract signin = Record, Body = unknown>(