add cookieName as param

This commit is contained in:
Carlos Ortega 2022-08-04 17:02:54 +01:00
parent fab4b8698d
commit 1d41d6cdec

View File

@ -15,6 +15,7 @@ interface AuthConfig {
host?: string; host?: string;
protocol?: string; protocol?: string;
basePath?: string; basePath?: string;
cookieName?: string;
} }
interface AuthCallbacks { interface AuthCallbacks {
@ -31,6 +32,10 @@ export class Auth {
return this.config?.basePath ?? "/api/auth"; return this.config?.basePath ?? "/api/auth";
} }
get cookieName() {
return this.config?.cookieName ?? "svelteauthjwt";
}
getJwtSecret() { getJwtSecret() {
if (this.config?.jwtSecret) { if (this.config?.jwtSecret) {
return this.config?.jwtSecret; return this.config?.jwtSecret;
@ -51,13 +56,13 @@ export class Auth {
const cookies = cookie.parse(headers.get("cookie")); const cookies = cookie.parse(headers.get("cookie"));
if (!cookies.svelteauthjwt) { if (!cookies[this.cookieName]) {
return null; return null;
} }
let token: JWT; let token: JWT;
try { try {
token = (jsonwebtoken.verify(cookies.svelteauthjwt, this.getJwtSecret()) || {}) as JWT; token = (jsonwebtoken.verify(cookies[this.cookieName], this.getJwtSecret()) || {}) as JWT;
} catch { } catch {
return null; return null;
} }
@ -130,7 +135,7 @@ export class Auth {
return { return {
status: 302, status: 302,
headers: { headers: {
"set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`,
Location: redirect, Location: redirect,
}, },
}; };
@ -147,7 +152,7 @@ export class Auth {
if (method === "POST") { if (method === "POST") {
return { return {
headers: { headers: {
"set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`,
}, },
body: { body: {
signout: true, signout: true,
@ -160,7 +165,7 @@ export class Auth {
return { return {
status: 302, status: 302,
headers: { headers: {
"set-cookie": `svelteauthjwt=${jwt}; Path=/; HttpOnly`, "set-cookie": `${this.cookieName}=${jwt}; Path=/; HttpOnly`,
Location: redirect, Location: redirect,
}, },
}; };