@smplcty/auth 0.2.1 → 0.4.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.
@@ -0,0 +1,21 @@
1
+ import type { Queryable } from './types.js';
2
+ /**
3
+ * Return every distinct role name assigned to a user, in alphabetical
4
+ * order. Returns an empty array if the user has no roles.
5
+ *
6
+ * Use this after `createSession` when the caller needs to tell the
7
+ * client which roles are available (e.g. to drive UI routing on the
8
+ * frontend). For in-transaction session bootstrapping — where the role
9
+ * is already known — use `withSession` instead.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const session = await createSession(db, { ... });
14
+ * const roles = await getUserRoleNames(db, session.userId);
15
+ * return { sessionId: session.sessionId, roles };
16
+ * ```
17
+ *
18
+ * @throws {InvalidInputError} If `userId` is not a positive integer.
19
+ */
20
+ export declare function getUserRoleNames(db: Queryable, userId: number): Promise<string[]>;
21
+ //# sourceMappingURL=get-user-role-names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-user-role-names.d.ts","sourceRoot":"","sources":["../src/get-user-role-names.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAc5C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,gBAAgB,CACpC,EAAE,EAAE,SAAS,EACb,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,EAAE,CAAC,CAOnB"}
@@ -0,0 +1,34 @@
1
+ import { InvalidInputError } from './errors.js';
2
+ const SELECT_ROLE_NAMES = `
3
+ SELECT DISTINCT r.name
4
+ FROM user_roles ur
5
+ JOIN roles r ON r.role_id = ur.role_id
6
+ WHERE ur.user_id = $1
7
+ ORDER BY r.name
8
+ `;
9
+ /**
10
+ * Return every distinct role name assigned to a user, in alphabetical
11
+ * order. Returns an empty array if the user has no roles.
12
+ *
13
+ * Use this after `createSession` when the caller needs to tell the
14
+ * client which roles are available (e.g. to drive UI routing on the
15
+ * frontend). For in-transaction session bootstrapping — where the role
16
+ * is already known — use `withSession` instead.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const session = await createSession(db, { ... });
21
+ * const roles = await getUserRoleNames(db, session.userId);
22
+ * return { sessionId: session.sessionId, roles };
23
+ * ```
24
+ *
25
+ * @throws {InvalidInputError} If `userId` is not a positive integer.
26
+ */
27
+ export async function getUserRoleNames(db, userId) {
28
+ if (!Number.isInteger(userId) || userId <= 0) {
29
+ throw new InvalidInputError('userId must be a positive integer');
30
+ }
31
+ const { rows } = await db.query(SELECT_ROLE_NAMES, [userId]);
32
+ return rows.map((r) => r.name);
33
+ }
34
+ //# sourceMappingURL=get-user-role-names.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-user-role-names.js","sourceRoot":"","sources":["../src/get-user-role-names.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,iBAAiB,GAAG;;;;;;CAMzB,CAAC;AAMF;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,EAAa,EACb,MAAc;IAEd,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,iBAAiB,CAAC,mCAAmC,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC,KAAK,CAAc,iBAAiB,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1E,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { createSession } from './create-session.js';
4
4
  export { validateSession } from './validate-session.js';
5
5
  export { revokeSession } from './revoke-session.js';
6
6
  export { findUserByCommunicationMethod } from './find-user-by-communication-method.js';
7
+ export { getUserRoleNames } from './get-user-role-names.js';
7
8
  export { withSession } from './with-session.js';
8
9
  export { withTransaction } from './with-transaction.js';
9
10
  export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,cAAc,EACd,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,yBAAyB,EACzB,aAAa,EACb,MAAM,EACN,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAGvF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,cAAc,EACd,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,yBAAyB,EACzB,aAAa,EACb,MAAM,EACN,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAG5D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export { createSession } from './create-session.js';
5
5
  export { validateSession } from './validate-session.js';
6
6
  export { revokeSession } from './revoke-session.js';
7
7
  export { findUserByCommunicationMethod } from './find-user-by-communication-method.js';
8
+ export { getUserRoleNames } from './get-user-role-names.js';
8
9
  // Request bootstrapping
9
10
  export { withSession } from './with-session.js';
10
11
  export { withTransaction } from './with-transaction.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAgBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO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,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
@@ -1,9 +1,15 @@
1
1
  import type { Queryable } from './types.js';
2
2
  /**
3
- * Revoke a session by hard-deleting its row from the `sessions` table.
3
+ * Revoke a session by setting its `expires_at` to now.
4
4
  *
5
- * Idempotent: revoking a non-existent session is not an error. Returns
6
- * normally regardless of whether a row was deleted.
5
+ * The session row is preserved for audit — you can distinguish
6
+ * "explicitly revoked" (expires_at close to created_at) from
7
+ * "expired naturally" (expires_at = created_at + ttl). The
8
+ * `session_authorization()` RLS function already checks
9
+ * `expires_at > now()`, so the session is locked out immediately.
10
+ *
11
+ * Idempotent: revoking a non-existent or already-expired session
12
+ * is not an error.
7
13
  *
8
14
  * @example
9
15
  * ```ts
@@ -1 +1 @@
1
- {"version":3,"file":"revoke-session.d.ts","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAI5C;;;;;;;;;;;;GAYG;AACH,wBAAsB,aAAa,CACjC,EAAE,EAAE,SAAS,EACb,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAKf"}
1
+ {"version":3,"file":"revoke-session.d.ts","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAS5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,aAAa,CACjC,EAAE,EAAE,SAAS,EACb,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAKf"}
@@ -1,10 +1,21 @@
1
1
  import { InvalidInputError } from './errors.js';
2
- const DELETE_SESSION = 'DELETE FROM sessions WHERE session_id = $1';
2
+ const EXPIRE_SESSION = `
3
+ UPDATE sessions
4
+ SET expires_at = now()
5
+ WHERE session_id = $1
6
+ AND (expires_at IS NULL OR expires_at > now())
7
+ `;
3
8
  /**
4
- * Revoke a session by hard-deleting its row from the `sessions` table.
9
+ * Revoke a session by setting its `expires_at` to now.
5
10
  *
6
- * Idempotent: revoking a non-existent session is not an error. Returns
7
- * normally regardless of whether a row was deleted.
11
+ * The session row is preserved for audit — you can distinguish
12
+ * "explicitly revoked" (expires_at close to created_at) from
13
+ * "expired naturally" (expires_at = created_at + ttl). The
14
+ * `session_authorization()` RLS function already checks
15
+ * `expires_at > now()`, so the session is locked out immediately.
16
+ *
17
+ * Idempotent: revoking a non-existent or already-expired session
18
+ * is not an error.
8
19
  *
9
20
  * @example
10
21
  * ```ts
@@ -17,6 +28,6 @@ export async function revokeSession(db, sessionId) {
17
28
  if (typeof sessionId !== 'string' || sessionId.length === 0) {
18
29
  throw new InvalidInputError('sessionId must be a non-empty string');
19
30
  }
20
- await db.query(DELETE_SESSION, [sessionId]);
31
+ await db.query(EXPIRE_SESSION, [sessionId]);
21
32
  }
22
33
  //# sourceMappingURL=revoke-session.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"revoke-session.js","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,cAAc,GAAG,4CAA4C,CAAC;AAEpE;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,EAAa,EACb,SAAiB;IAEjB,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5D,MAAM,IAAI,iBAAiB,CAAC,sCAAsC,CAAC,CAAC;IACtE,CAAC;IACD,MAAM,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;AAC9C,CAAC"}
1
+ {"version":3,"file":"revoke-session.js","sourceRoot":"","sources":["../src/revoke-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,cAAc,GAAG;;;;;CAKtB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,EAAa,EACb,SAAiB;IAEjB,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5D,MAAM,IAAI,iBAAiB,CAAC,sCAAsC,CAAC,CAAC;IACtE,CAAC;IACD,MAAM,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;AAC9C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smplcty/auth",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Robust, type-safe session and role-based authentication primitives for PostgreSQL apps using Row-Level Security.",
5
5
  "license": "MIT",
6
6
  "type": "module",