ideal-auth 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -106,6 +106,7 @@ Returns a function `auth()` that creates an `AuthInstance` on each call.
106
106
  | `session.cookieName` | `string` | No | `'ideal_session'` |
107
107
  | `session.maxAge` | `number` (seconds) | No | `604800` (7 days) |
108
108
  | `session.rememberMaxAge` | `number` (seconds) | No | `2592000` (30 days) |
109
+ | `session.autoTouch` | `boolean` | No | `false`. Auto-extend sessions past halfway on `check()`/`user()`/`id()`. Enable for Express/Hono. Disable for Next.js. |
109
110
  | `session.cookie` | `Partial<ConfigurableCookieOptions>` | No | secure in prod, sameSite lax, path / (`httpOnly` is always `true` — not configurable) |
110
111
 
111
112
  #### AuthInstance Methods
@@ -116,9 +117,10 @@ Returns a function `auth()` that creates an `AuthInstance` on each call.
116
117
  | `loginById(id, options?)` | `Promise<void>` | Resolve user by ID, then set session cookie |
117
118
  | `attempt(credentials, options?)` | `Promise<boolean>` | Find user, verify password, login if valid |
118
119
  | `logout()` | `Promise<void>` | Delete session cookie |
119
- | `check()` | `Promise<boolean>` | Is the session valid? |
120
- | `user()` | `Promise<User \| null>` | Get the authenticated user |
121
- | `id()` | `Promise<string \| null>` | Get the authenticated user's ID |
120
+ | `check()` | `Promise<boolean>` | Is the session valid? (read-only) |
121
+ | `user()` | `Promise<User \| null>` | Get the authenticated user (read-only) |
122
+ | `id()` | `Promise<string \| null>` | Get the authenticated user's ID (read-only) |
123
+ | `touch()` | `Promise<void>` | Extend session expiry. Reseals past halfway (`autoTouch: false`) or immediately (`autoTouch: true`). |
122
124
 
123
125
  All login methods accept an optional `LoginOptions` object:
124
126
 
@@ -6,6 +6,7 @@ interface AuthInstanceDeps<TUser extends AnyUser> {
6
6
  maxAge: number;
7
7
  rememberMaxAge: number;
8
8
  cookieOptions: ConfigurableCookieOptions;
9
+ autoTouch: boolean;
9
10
  resolveUser?: (id: string) => Promise<TUser | null | undefined>;
10
11
  sessionFields?: (keyof TUser & string)[];
11
12
  hash?: HashInstance;
@@ -3,6 +3,7 @@ import { buildCookieOptions } from './session/cookie';
3
3
  export function createAuthInstance(deps) {
4
4
  let cachedPayload;
5
5
  let cachedUser;
6
+ let didAutoTouch = false;
6
7
  async function readSession() {
7
8
  if (cachedPayload !== undefined)
8
9
  return cachedPayload;
@@ -12,8 +13,31 @@ export function createAuthInstance(deps) {
12
13
  return null;
13
14
  }
14
15
  cachedPayload = await unseal(raw, deps.secret);
16
+ // Auto-touch: reseal past halfway when enabled
17
+ if (deps.autoTouch && cachedPayload && !didAutoTouch) {
18
+ const elapsed = Math.floor(Date.now() / 1000) - cachedPayload.iat;
19
+ if (elapsed >= cachedPayload.ttl / 2) {
20
+ await resealSession(cachedPayload);
21
+ }
22
+ }
15
23
  return cachedPayload;
16
24
  }
25
+ async function resealSession(session) {
26
+ didAutoTouch = true;
27
+ const ttl = session.ttl;
28
+ const now = Math.floor(Date.now() / 1000);
29
+ const newPayload = {
30
+ uid: session.uid,
31
+ iat: session.iat, // preserve original issued-at for passwordChangedAt checks
32
+ exp: now + ttl,
33
+ ttl,
34
+ ...(session.data !== undefined && { data: session.data }),
35
+ };
36
+ const sealed = await seal(newPayload, deps.secret);
37
+ const opts = buildCookieOptions(ttl, deps.cookieOptions);
38
+ await deps.cookie.set(deps.cookieName, sealed, opts);
39
+ cachedPayload = newPayload;
40
+ }
17
41
  function pickSessionData(user) {
18
42
  if (!deps.sessionFields)
19
43
  return undefined;
@@ -32,6 +56,7 @@ export function createAuthInstance(deps) {
32
56
  uid: String(user.id),
33
57
  iat: now,
34
58
  exp: now + maxAge,
59
+ ttl: maxAge,
35
60
  data: pickSessionData(user),
36
61
  };
37
62
  const sealed = await seal(payload, deps.secret);
@@ -125,18 +150,16 @@ export function createAuthInstance(deps) {
125
150
  const session = await readSession();
126
151
  if (!session)
127
152
  return;
128
- const maxAge = session.exp - session.iat;
129
- const now = Math.floor(Date.now() / 1000);
130
- const newPayload = {
131
- uid: session.uid,
132
- iat: session.iat, // preserve original issued-at for passwordChangedAt checks
133
- exp: now + maxAge,
134
- ...(session.data !== undefined && { data: session.data }),
135
- };
136
- const sealed = await seal(newPayload, deps.secret);
137
- const opts = buildCookieOptions(maxAge, deps.cookieOptions);
138
- await deps.cookie.set(deps.cookieName, sealed, opts);
139
- cachedPayload = newPayload;
153
+ if (didAutoTouch)
154
+ return; // already resealed by autoTouch on this request
155
+ // autoTouch enabled: reseal immediately (user opted in to cookie writes)
156
+ // autoTouch disabled: only reseal past halfway (conservative)
157
+ if (!deps.autoTouch) {
158
+ const elapsed = Math.floor(Date.now() / 1000) - session.iat;
159
+ if (elapsed < session.ttl / 2)
160
+ return;
161
+ }
162
+ await resealSession(session);
140
163
  },
141
164
  };
142
165
  }
package/dist/auth.js CHANGED
@@ -34,6 +34,7 @@ export function createAuth(config) {
34
34
  maxAge: config.session?.maxAge ?? SESSION_DEFAULTS.maxAge,
35
35
  rememberMaxAge: config.session?.rememberMaxAge ?? SESSION_DEFAULTS.rememberMaxAge,
36
36
  cookieOptions: config.session?.cookie ?? {},
37
+ autoTouch: config.session?.autoTouch ?? false,
37
38
  resolveUser: config.resolveUser,
38
39
  sessionFields: config.sessionFields,
39
40
  hash: config.hash,
@@ -15,6 +15,7 @@ export async function unseal(sealed, secret) {
15
15
  uid: data.uid,
16
16
  iat: data.iat,
17
17
  exp: data.exp,
18
+ ttl: (typeof data.ttl === 'number' && data.ttl > 0) ? data.ttl : (data.exp - data.iat),
18
19
  ...(data.data !== undefined && { data: data.data }),
19
20
  };
20
21
  }
package/dist/types.d.ts CHANGED
@@ -22,6 +22,7 @@ export interface SessionPayload {
22
22
  uid: string;
23
23
  iat: number;
24
24
  exp: number;
25
+ ttl: number;
25
26
  data?: Record<string, unknown>;
26
27
  }
27
28
  export interface LoginOptions {
@@ -35,6 +36,8 @@ interface AuthConfigBase<TUser extends AnyUser> {
35
36
  maxAge?: number;
36
37
  rememberMaxAge?: number;
37
38
  cookie?: Partial<ConfigurableCookieOptions>;
39
+ /** Automatically extend session on read when past the halfway point. Default: false. */
40
+ autoTouch?: boolean;
38
41
  };
39
42
  hash?: HashInstance;
40
43
  resolveUserByCredentials?: (credentials: Record<string, any>) => Promise<AnyUser | null | undefined>;
@@ -86,7 +89,7 @@ export interface AuthInstance<TUser extends AnyUser = AnyUser> {
86
89
  check(): Promise<boolean>;
87
90
  user(): Promise<TUser | null>;
88
91
  id(): Promise<string | null>;
89
- /** Re-seal the session cookie with a fresh expiry. No database call needed. Does nothing if no valid session exists. */
92
+ /** Re-seal the session cookie with a fresh expiry. When autoTouch is disabled (default), only reseals past the halfway point. No database call needed. Does nothing if no valid session exists or if already resealed on this request. */
90
93
  touch(): Promise<void>;
91
94
  }
92
95
  export interface HashInstance {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ideal-auth",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Auth primitives for the JS ecosystem. Zero framework dependencies.",
5
5
  "scripts": {
6
6
  "build": "tsc",
@@ -167,19 +167,34 @@ type LoginOptions = {
167
167
  };
168
168
  ```
169
169
 
170
- #### Session Extension with `touch()`
170
+ #### Session Extension
171
171
 
172
- Sessions have a fixed expiry. Call `touch()` in middleware to extend the session for active users:
172
+ Two options for keeping active users logged in:
173
+
174
+ **`autoTouch: true`** — automatic. Enable for Express, Hono, Elysia, SvelteKit. `check()`/`user()`/`id()` auto-reseal past halfway. Do NOT use with Next.js (Server Components can't write cookies).
175
+
176
+ ```typescript
177
+ const auth = createAuth<User>({
178
+ session: { autoTouch: true },
179
+ ...
180
+ });
181
+ ```
182
+
183
+ **Manual `touch()`** — call in middleware. When `autoTouch` is false (default), only reseals past halfway. When `autoTouch` is true, reseals immediately.
173
184
 
174
185
  ```typescript
175
- // In middleware — where cookie writes are allowed
176
186
  const session = auth();
177
187
  if (await session.check()) {
178
- await session.touch(); // re-seals cookie with fresh exp
188
+ await session.touch();
179
189
  }
180
190
  ```
181
191
 
182
- `touch()` re-seals with the same `maxAge` as the original session. No database call needed. `check()`, `user()`, and `id()` are read-only — they never write cookies. Only call `touch()` where cookie writes are allowed (middleware, route handlers, server actions — NOT Server Components).
192
+ | | `autoTouch: false` (default) | `autoTouch: true` |
193
+ |---|---|---|
194
+ | `check()`/`user()`/`id()` | Read-only | Auto-reseals past halfway |
195
+ | `touch()` | Reseals past halfway | Reseals immediately |
196
+
197
+ `touch()` preserves original `iat` — `passwordChangedAt` invalidation still works. Does not update user data — use `auth().login(updatedUser)` for that.
183
198
 
184
199
  #### `attempt()` — Two Modes
185
200