Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cookie name to be changed #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface AuthConfig {
host?: string;
protocol?: string;
basePath?: string;
cookieName?: string;
}

interface AuthCallbacks {
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
},
};
Expand All @@ -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,
Expand All @@ -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,
},
};
Expand Down