@spfn/auth 0.2.0-beta.8 → 0.2.0-beta.80

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.
Files changed (42) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +549 -1819
  3. package/dist/authenticate-ofdEmk6x.d.ts +1109 -0
  4. package/dist/config.d.ts +359 -41
  5. package/dist/config.js +186 -30
  6. package/dist/config.js.map +1 -1
  7. package/dist/errors.d.ts +117 -3
  8. package/dist/errors.js +83 -1
  9. package/dist/errors.js.map +1 -1
  10. package/dist/index.d.ts +351 -109
  11. package/dist/index.js +124 -7
  12. package/dist/index.js.map +1 -1
  13. package/dist/nextjs/api.js +591 -61
  14. package/dist/nextjs/api.js.map +1 -1
  15. package/dist/nextjs/client.d.ts +28 -0
  16. package/dist/nextjs/client.js +80 -0
  17. package/dist/nextjs/client.js.map +1 -0
  18. package/dist/nextjs/server.d.ts +92 -3
  19. package/dist/nextjs/server.js +288 -24
  20. package/dist/nextjs/server.js.map +1 -1
  21. package/dist/server.d.ts +2015 -513
  22. package/dist/server.js +4198 -1086
  23. package/dist/server.js.map +1 -1
  24. package/dist/session-CGxgH3C9.d.ts +53 -0
  25. package/dist/types-1BMx0OX1.d.ts +84 -0
  26. package/migrations/0001_smooth_the_fury.sql +3 -0
  27. package/migrations/0002_deep_iceman.sql +11 -0
  28. package/migrations/0003_perfect_deathbird.sql +3 -0
  29. package/migrations/0004_concerned_rawhide_kid.sql +5 -0
  30. package/migrations/0005_lethal_lifeguard.sql +32 -0
  31. package/migrations/0006_easy_hardball.sql +24 -0
  32. package/migrations/0007_glossy_major_mapleleaf.sql +1 -0
  33. package/migrations/meta/0001_snapshot.json +1660 -0
  34. package/migrations/meta/0002_snapshot.json +1660 -0
  35. package/migrations/meta/0003_snapshot.json +1689 -0
  36. package/migrations/meta/0004_snapshot.json +1721 -0
  37. package/migrations/meta/0005_snapshot.json +1721 -0
  38. package/migrations/meta/0006_snapshot.json +1921 -0
  39. package/migrations/meta/0007_snapshot.json +1916 -0
  40. package/migrations/meta/_journal.json +49 -0
  41. package/package.json +43 -39
  42. package/dist/dto-lZmWuObc.d.ts +0 -645
@@ -0,0 +1,53 @@
1
+ import { K as KeyAlgorithmType } from './types-1BMx0OX1.js';
2
+
3
+ /**
4
+ * @spfn/auth - Client Session Management
5
+ *
6
+ * Uses Jose JWE (JSON Web Encryption) to securely store session data in cookies
7
+ * More efficient than Iron Session with better Edge Runtime support
8
+ */
9
+
10
+ interface SessionData {
11
+ userId: string;
12
+ privateKey: string;
13
+ keyId: string;
14
+ algorithm: KeyAlgorithmType;
15
+ }
16
+ /**
17
+ * Seal session data into encrypted JWT (JWE)
18
+ *
19
+ * @param data - Session data to encrypt
20
+ * @param ttl - Time to live in seconds (default: 7 days)
21
+ * @returns Encrypted JWT string
22
+ */
23
+ declare function sealSession(data: SessionData, ttl?: number): Promise<string>;
24
+ /**
25
+ * Unseal encrypted JWT (JWE) to session data
26
+ *
27
+ * @param jwt - Encrypted JWT string
28
+ * @returns Session data
29
+ * @throws Error if session is invalid or expired
30
+ */
31
+ declare function unsealSession(jwt: string): Promise<SessionData>;
32
+ /**
33
+ * Get session metadata without decrypting
34
+ *
35
+ * @param jwt - Encrypted JWT string
36
+ * @returns Session metadata or null if invalid
37
+ */
38
+ declare function getSessionInfo(jwt: string): Promise<{
39
+ issuedAt: Date;
40
+ expiresAt: Date;
41
+ issuer: string;
42
+ audience: string;
43
+ } | null>;
44
+ /**
45
+ * Check if session is about to expire (within threshold)
46
+ *
47
+ * @param jwt - Encrypted JWT string
48
+ * @param thresholdHours - Hours before expiry to trigger refresh (default: 24)
49
+ * @returns True if session should be refreshed
50
+ */
51
+ declare function shouldRefreshSession(jwt: string, thresholdHours?: number): Promise<boolean>;
52
+
53
+ export { type SessionData as S, shouldRefreshSession as a, getSessionInfo as g, sealSession as s, unsealSession as u };
@@ -0,0 +1,84 @@
1
+ /**
2
+ * @spfn/auth - Shared Types
3
+ *
4
+ * Common types and constants used across the auth package
5
+ */
6
+ /**
7
+ * Supported JWT signature algorithms
8
+ *
9
+ * - ES256: ECDSA with P-256 and SHA-256 (recommended, smaller keys)
10
+ * - RS256: RSA with SHA-256 (fallback, larger keys)
11
+ */
12
+ declare const KEY_ALGORITHM: readonly ["ES256", "RS256"];
13
+ /**
14
+ * Key algorithm type derived from the const array
15
+ */
16
+ type KeyAlgorithmType = typeof KEY_ALGORITHM[number];
17
+ /**
18
+ * Invitation status enum values
19
+ * Single source of truth for all invitation statuses
20
+ */
21
+ declare const INVITATION_STATUSES: readonly ["pending", "accepted", "expired", "cancelled"];
22
+ /**
23
+ * Invitation status type derived from the const array
24
+ */
25
+ type InvitationStatus = typeof INVITATION_STATUSES[number];
26
+ /**
27
+ * User status enum values
28
+ * Single source of truth for all user statuses
29
+ *
30
+ * - active: Normal operation (default)
31
+ * - inactive: Deactivated (user request, dormant)
32
+ * - suspended: Locked (security incident, ToS violation)
33
+ * - pending_deletion: Deletion requested, within the grace period (recoverable)
34
+ * - deleted: Grace period elapsed and the account was purged (anonymize mode only —
35
+ * hard-delete removes the row instead, so this status never appears for it)
36
+ */
37
+ declare const USER_STATUSES: readonly ["active", "inactive", "suspended", "pending_deletion", "deleted"];
38
+ /**
39
+ * User status type derived from the const array
40
+ */
41
+ type UserStatus = typeof USER_STATUSES[number];
42
+ /**
43
+ * Social provider enum values
44
+ * Single source of truth for supported OAuth providers
45
+ */
46
+ declare const SOCIAL_PROVIDERS: readonly ["google", "apple", "github", "kakao", "naver", "superself"];
47
+ /**
48
+ * Social provider type derived from the const array
49
+ */
50
+ type SocialProvider = typeof SOCIAL_PROVIDERS[number];
51
+ /**
52
+ * Account deletion request status enum values
53
+ * Single source of truth for `account_deletion_requests.status`
54
+ *
55
+ * - pending: Awaiting the grace period (or immediate purge)
56
+ * - cancelled: User (or admin) recovered the account before purge
57
+ * - completed: The purge ran (row is kept as an audit record, never deleted)
58
+ */
59
+ declare const ACCOUNT_DELETION_REQUEST_STATUSES: readonly ["pending", "cancelled", "completed"];
60
+ /**
61
+ * Account deletion request status type derived from the const array
62
+ */
63
+ type AccountDeletionRequestStatus = typeof ACCOUNT_DELETION_REQUEST_STATUSES[number];
64
+ /**
65
+ * Who initiated an account deletion request
66
+ */
67
+ declare const ACCOUNT_DELETION_REQUESTED_BY: readonly ["self", "admin"];
68
+ /**
69
+ * Account deletion requester type derived from the const array
70
+ */
71
+ type AccountDeletionRequestedBy = typeof ACCOUNT_DELETION_REQUESTED_BY[number];
72
+ /**
73
+ * Purge strategy enum values
74
+ *
75
+ * - anonymize: Scrub PII, keep the row (status becomes 'deleted') — default
76
+ * - hard-delete: Physically remove the `users` row (cascades to child rows)
77
+ */
78
+ declare const PURGE_STRATEGIES: readonly ["anonymize", "hard-delete"];
79
+ /**
80
+ * Purge strategy type derived from the const array
81
+ */
82
+ type PurgeStrategy = typeof PURGE_STRATEGIES[number];
83
+
84
+ export { ACCOUNT_DELETION_REQUEST_STATUSES as A, INVITATION_STATUSES as I, type KeyAlgorithmType as K, PURGE_STRATEGIES as P, SOCIAL_PROVIDERS as S, USER_STATUSES as U, KEY_ALGORITHM as a, ACCOUNT_DELETION_REQUESTED_BY as b, type InvitationStatus as c, type UserStatus as d, type SocialProvider as e, type AccountDeletionRequestStatus as f, type AccountDeletionRequestedBy as g, type PurgeStrategy as h };
@@ -0,0 +1,3 @@
1
+ ALTER TABLE "spfn_auth"."users" ADD COLUMN "username" text;--> statement-breakpoint
2
+ CREATE INDEX "users_username_idx" ON "spfn_auth"."users" USING btree ("username");--> statement-breakpoint
3
+ ALTER TABLE "spfn_auth"."users" ADD CONSTRAINT "users_username_unique" UNIQUE("username");
@@ -0,0 +1,11 @@
1
+ ALTER TABLE "spfn_auth"."users" ALTER COLUMN "role_id" SET DATA TYPE bigint;--> statement-breakpoint
2
+ ALTER TABLE "spfn_auth"."user_profiles" ALTER COLUMN "user_id" SET DATA TYPE bigint;--> statement-breakpoint
3
+ ALTER TABLE "spfn_auth"."user_profiles" ALTER COLUMN "display_name" DROP NOT NULL;--> statement-breakpoint
4
+ ALTER TABLE "spfn_auth"."user_public_keys" ALTER COLUMN "user_id" SET DATA TYPE bigint;--> statement-breakpoint
5
+ ALTER TABLE "spfn_auth"."user_social_accounts" ALTER COLUMN "user_id" SET DATA TYPE bigint;--> statement-breakpoint
6
+ ALTER TABLE "spfn_auth"."user_invitations" ALTER COLUMN "role_id" SET DATA TYPE bigint;--> statement-breakpoint
7
+ ALTER TABLE "spfn_auth"."user_invitations" ALTER COLUMN "invited_by_id" SET DATA TYPE bigint;--> statement-breakpoint
8
+ ALTER TABLE "spfn_auth"."role_permissions" ALTER COLUMN "role_id" SET DATA TYPE bigint;--> statement-breakpoint
9
+ ALTER TABLE "spfn_auth"."role_permissions" ALTER COLUMN "permission_id" SET DATA TYPE bigint;--> statement-breakpoint
10
+ ALTER TABLE "spfn_auth"."user_permissions" ALTER COLUMN "user_id" SET DATA TYPE bigint;--> statement-breakpoint
11
+ ALTER TABLE "spfn_auth"."user_permissions" ALTER COLUMN "permission_id" SET DATA TYPE bigint;
@@ -0,0 +1,3 @@
1
+ ALTER TABLE "spfn_auth"."users" ADD COLUMN "public_id" uuid DEFAULT gen_random_uuid() NOT NULL;--> statement-breakpoint
2
+ CREATE INDEX "users_public_id_idx" ON "spfn_auth"."users" USING btree ("public_id");--> statement-breakpoint
3
+ ALTER TABLE "spfn_auth"."users" ADD CONSTRAINT "users_public_id_unique" UNIQUE("public_id");
@@ -0,0 +1,5 @@
1
+ CREATE TABLE "spfn_auth"."auth_metadata" (
2
+ "key" text PRIMARY KEY NOT NULL,
3
+ "value" text NOT NULL,
4
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
5
+ );
@@ -0,0 +1,32 @@
1
+ -- Corrective migration: drop leftover bigserial sequence defaults on FK columns.
2
+ --
3
+ -- These columns were originally declared `bigserial` in 0000 and converted to
4
+ -- `bigint` in 0002. Postgres `ALTER COLUMN ... SET DATA TYPE bigint` does NOT drop
5
+ -- the serial-created `DEFAULT nextval(...)`, so every DB that ran those migrations
6
+ -- still has a dangling sequence default on each FK column. That default is wrong:
7
+ -- an FK with no explicit value would silently receive a sequence number instead of
8
+ -- pointing at a real parent row. The entities define these columns with no default
9
+ -- (foreignKey() -> bigint, not null, no default) and all writers set them explicitly,
10
+ -- so the live DB is simply diverged. Drizzle cannot detect this — its snapshot already
11
+ -- shows bigint/no-default — so this corrective migration is written by hand.
12
+
13
+ ALTER TABLE "spfn_auth"."users" ALTER COLUMN "role_id" DROP DEFAULT;--> statement-breakpoint
14
+ ALTER TABLE "spfn_auth"."user_profiles" ALTER COLUMN "user_id" DROP DEFAULT;--> statement-breakpoint
15
+ ALTER TABLE "spfn_auth"."user_public_keys" ALTER COLUMN "user_id" DROP DEFAULT;--> statement-breakpoint
16
+ ALTER TABLE "spfn_auth"."user_social_accounts" ALTER COLUMN "user_id" DROP DEFAULT;--> statement-breakpoint
17
+ ALTER TABLE "spfn_auth"."user_invitations" ALTER COLUMN "role_id" DROP DEFAULT;--> statement-breakpoint
18
+ ALTER TABLE "spfn_auth"."user_invitations" ALTER COLUMN "invited_by_id" DROP DEFAULT;--> statement-breakpoint
19
+ ALTER TABLE "spfn_auth"."role_permissions" ALTER COLUMN "role_id" DROP DEFAULT;--> statement-breakpoint
20
+ ALTER TABLE "spfn_auth"."role_permissions" ALTER COLUMN "permission_id" DROP DEFAULT;--> statement-breakpoint
21
+ ALTER TABLE "spfn_auth"."user_permissions" ALTER COLUMN "user_id" DROP DEFAULT;--> statement-breakpoint
22
+ ALTER TABLE "spfn_auth"."user_permissions" ALTER COLUMN "permission_id" DROP DEFAULT;--> statement-breakpoint
23
+ DROP SEQUENCE IF EXISTS "spfn_auth"."users_role_id_seq";--> statement-breakpoint
24
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_profiles_user_id_seq";--> statement-breakpoint
25
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_public_keys_user_id_seq";--> statement-breakpoint
26
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_social_accounts_user_id_seq";--> statement-breakpoint
27
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_invitations_role_id_seq";--> statement-breakpoint
28
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_invitations_invited_by_id_seq";--> statement-breakpoint
29
+ DROP SEQUENCE IF EXISTS "spfn_auth"."role_permissions_role_id_seq";--> statement-breakpoint
30
+ DROP SEQUENCE IF EXISTS "spfn_auth"."role_permissions_permission_id_seq";--> statement-breakpoint
31
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_permissions_user_id_seq";--> statement-breakpoint
32
+ DROP SEQUENCE IF EXISTS "spfn_auth"."user_permissions_permission_id_seq";
@@ -0,0 +1,24 @@
1
+ CREATE TABLE "spfn_auth"."account_deletion_requests" (
2
+ "id" bigserial PRIMARY KEY NOT NULL,
3
+ "user_id" bigint,
4
+ "user_public_id" text NOT NULL,
5
+ "requested_at" timestamp with time zone DEFAULT now() NOT NULL,
6
+ "purge_scheduled_at" timestamp with time zone NOT NULL,
7
+ "status" text DEFAULT 'pending' NOT NULL,
8
+ "requested_by" text DEFAULT 'self' NOT NULL,
9
+ "reason" text,
10
+ "cancelled_at" timestamp with time zone,
11
+ "completed_at" timestamp with time zone,
12
+ "purge_strategy" text,
13
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
14
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
15
+ );
16
+ --> statement-breakpoint
17
+ ALTER TABLE "spfn_auth"."users" ADD COLUMN "deleted_at" timestamp with time zone;--> statement-breakpoint
18
+ ALTER TABLE "spfn_auth"."users" ADD COLUMN "deleted_by" text;--> statement-breakpoint
19
+ ALTER TABLE "spfn_auth"."account_deletion_requests" ADD CONSTRAINT "account_deletion_requests_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "spfn_auth"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
20
+ CREATE INDEX "account_deletion_requests_user_id_idx" ON "spfn_auth"."account_deletion_requests" USING btree ("user_id");--> statement-breakpoint
21
+ CREATE INDEX "account_deletion_requests_status_idx" ON "spfn_auth"."account_deletion_requests" USING btree ("status");--> statement-breakpoint
22
+ CREATE INDEX "account_deletion_requests_purge_scheduled_at_idx" ON "spfn_auth"."account_deletion_requests" USING btree ("purge_scheduled_at");--> statement-breakpoint
23
+ CREATE INDEX "account_deletion_requests_user_public_id_idx" ON "spfn_auth"."account_deletion_requests" USING btree ("user_public_id");--> statement-breakpoint
24
+ CREATE UNIQUE INDEX "account_deletion_requests_user_pending_unique_idx" ON "spfn_auth"."account_deletion_requests" USING btree ("user_id") WHERE "spfn_auth"."account_deletion_requests"."status" = 'pending';
@@ -0,0 +1 @@
1
+ ALTER TABLE "spfn_auth"."users" DROP CONSTRAINT "email_or_phone_check";