Skip to content

Commit 74ac53b

Browse files
authored
Merge pull request #383 from devforth/next
fix: update setCustomCookie to use expirySeconds and deprecate expiry…
2 parents 19aad26 + 4caed99 commit 74ac53b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

adminforth/auth.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,22 @@ class AdminForthAuth implements IAdminForthAuth {
9999
}
100100

101101
setCustomCookie({ response, payload }: {
102-
response: any, payload: {name: string, value: string, expiry: number, httpOnly: boolean}
102+
response: any, payload: { name: string, value: string, expiry: number | undefined, expirySeconds: number | undefined, httpOnly: boolean }
103103
}) {
104-
const {name, value, expiry, httpOnly} = payload;
104+
const {name, value, expiry, httpOnly, expirySeconds } = payload;
105+
106+
let expiryMs = 24 * 60 * 60 * 1000; // default 1 day
107+
if (expirySeconds !== undefined) {
108+
expiryMs = expirySeconds * 1000;
109+
} else if (expiry !== undefined) {
110+
console.warn('setCustomCookie: expiry(in ms) is deprecated, use expirySeconds instead (seconds), traceback:', new Error().stack);
111+
expiryMs = expiry;
112+
}
113+
105114
const brandSlug = this.adminforth.config.customization.brandNameSlug;
106115
response.setHeader('Set-Cookie', `adminforth_${brandSlug}_${name}=${value}; Path=${this.adminforth.config.baseUrl || '/'};${
107116
httpOnly ? ' HttpOnly;' : ''
108-
} SameSite=Strict; Expires=${new Date(Date.now() + expiry).toUTCString() } `);
117+
} SameSite=Strict; Expires=${new Date(Date.now() + expiryMs).toUTCString() } `);
109118
}
110119

111120
getCustomCookie({ cookies, name }: {

0 commit comments

Comments
 (0)