From 1d41d6cdecc58ebcb68e60eeb89081483f8ba0ba Mon Sep 17 00:00:00 2001 From: Carlos Ortega Date: Thu, 4 Aug 2022 17:02:54 +0100 Subject: [PATCH] add cookieName as param --- src/auth.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 8033fb2..ea8b7cc 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -15,6 +15,7 @@ interface AuthConfig { host?: string; protocol?: string; basePath?: string; + cookieName?: string; } interface AuthCallbacks { @@ -31,6 +32,10 @@ export class Auth { return this.config?.basePath ?? "/api/auth"; } + get cookieName() { + return this.config?.cookieName ?? "svelteauthjwt"; + } + getJwtSecret() { if (this.config?.jwtSecret) { return this.config?.jwtSecret; @@ -51,13 +56,13 @@ export class Auth { const cookies = cookie.parse(headers.get("cookie")); - if (!cookies.svelteauthjwt) { + if (!cookies[this.cookieName]) { return null; } let token: JWT; try { - token = (jsonwebtoken.verify(cookies.svelteauthjwt, this.getJwtSecret()) || {}) as JWT; + token = (jsonwebtoken.verify(cookies[this.cookieName], this.getJwtSecret()) || {}) as JWT; } catch { return null; } @@ -130,7 +135,7 @@ export class Auth { return { status: 302, headers: { - "set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, + "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`, Location: redirect, }, }; @@ -147,7 +152,7 @@ export class Auth { if (method === "POST") { return { headers: { - "set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, + "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`, }, body: { signout: true, @@ -160,7 +165,7 @@ export class Auth { return { status: 302, headers: { - "set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, + "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`, Location: redirect, }, };