Skip to content

Commit

Permalink
v4.1.0 - cookie 1.0, init should only be called when no session exists
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmund committed Jan 7, 2025
1 parent d96fb7f commit c2536d3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-kit-cookie-session",
"version": "4.0.0",
"version": "4.1.0",
"description": "⚒️ Encrypted 'stateless' cookie sessions for SvelteKit",
"repository": {
"type": "git",
Expand All @@ -24,7 +24,7 @@
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check --plugin-search-dir=. .",
"format": "prettier --write --plugin-search-dir=. .",
"package:publish": "npm run package && cd package && npm publish"
"package:publish": "npm run package && cd dist && npm publish"
},
"devDependencies": {
"@playwright/test": "^1.25.1",
Expand Down
14 changes: 8 additions & 6 deletions src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ export class CookieSession<SessionType = Record<string, any>> {
async init() {
const { data, state } = await this.getSessionData();

this.#initialData = await this.#config.init(this.#event);

if (this.#config.saveUninitialized && !data && this.#initialData) {
if (this.#config.saveUninitialized && !data) {
this.#initialData = await this.#config.init(this.#event);
await this.set(this.#initialData);
}

Expand Down Expand Up @@ -86,7 +85,7 @@ export class CookieSession<SessionType = Record<string, any>> {
let maxAge = this.#config.cookie.maxAge;

if (this.#sessionData?.expires) {
maxAge = new Date(this.#sessionData.expires).getTime() / 1000 - new Date().getTime() / 1000;
maxAge = Math.round(new Date(this.#sessionData.expires).getTime() / 1000 - new Date().getTime() / 1000);
}

this.#state.needsSync = true;
Expand Down Expand Up @@ -143,7 +142,7 @@ export class CookieSession<SessionType = Record<string, any>> {
let maxAge = this.#config.cookie.maxAge;

if (this.#sessionData?.expires) {
maxAge = new Date(this.#sessionData.expires).getTime() / 1000 - new Date().getTime() / 1000;
maxAge = Math.round(new Date(this.#sessionData.expires).getTime() / 1000 - new Date().getTime() / 1000);
}

await this.setCookie(maxAge);
Expand All @@ -156,7 +155,10 @@ export class CookieSession<SessionType = Record<string, any>> {

this.#state.needsSync = true;

const newMaxAge = expiresToMaxage(expires ? expires : this.#config.expires, this.#config.expires_in);
const newMaxAge = expiresToMaxage(
expires ? expires : this.#config.expires,
this.#config.expires_in
);

this.#sessionData = {
...this.#sessionData,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type { MaybePromise } from '@sveltejs/kit';
export function expiresToMaxage(expires: number, expires_in: 'days' | 'hours' | 'minutes' | 'seconds') {
switch (expires_in) {
case 'days':
return expires * 24 * 60 * 60;
return Math.round(expires * 24 * 60 * 60);
case 'hours':
return expires * 60 * 60;
return Math.round(expires * 60 * 60);
case 'minutes':
return expires * 60;
return Math.round(expires * 60);
case 'seconds':
return expires;
default:
Expand Down

0 comments on commit c2536d3

Please sign in to comment.