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;
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,
},
};