@absolutejs/auth 0.85.0 → 0.86.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/dist/index.js CHANGED
@@ -10243,29 +10243,17 @@ init_crypto();
10243
10243
  var acceptInvitation = async ({
10244
10244
  organizationStore,
10245
10245
  token,
10246
- userId
10246
+ userId,
10247
+ verifiedEmail
10247
10248
  }) => {
10248
- const invitation = await organizationStore.getInvitationByTokenHash(await hashToken(token));
10249
- if (!invitation || invitation.state !== "pending")
10249
+ if (!verifiedEmail?.trim() || !organizationStore.acceptInvitation)
10250
10250
  return;
10251
- if (invitation.expiresAt < Date.now())
10252
- return;
10253
- const now = Date.now();
10254
- await organizationStore.saveInvitation({
10255
- ...invitation,
10256
- acceptedAt: now,
10257
- state: "accepted"
10251
+ return organizationStore.acceptInvitation({
10252
+ now: Date.now(),
10253
+ tokenHash: await hashToken(token),
10254
+ userId,
10255
+ verifiedEmail: verifiedEmail.trim().toLowerCase()
10258
10256
  });
10259
- const membership = {
10260
- createdAt: now,
10261
- organizationId: invitation.organizationId,
10262
- roles: invitation.roles,
10263
- status: "active",
10264
- updatedAt: now,
10265
- userId
10266
- };
10267
- await organizationStore.saveMembership(membership);
10268
- return membership;
10269
10257
  };
10270
10258
  var createOrganization = async ({
10271
10259
  metadata,
@@ -10336,6 +10324,7 @@ var organizationRoutes = ({
10336
10324
  canManageMembers,
10337
10325
  emit,
10338
10326
  getUserId,
10327
+ getVerifiedEmail,
10339
10328
  invitationDurationMs,
10340
10329
  onMembershipAdded,
10341
10330
  onMembershipRemoved,
@@ -10524,7 +10513,8 @@ var organizationRoutes = ({
10524
10513
  const membership = await acceptInvitation({
10525
10514
  organizationStore,
10526
10515
  token,
10527
- userId: getUserId(user)
10516
+ userId: getUserId(user),
10517
+ verifiedEmail: await getVerifiedEmail?.(user)
10528
10518
  });
10529
10519
  if (!membership) {
10530
10520
  return status("Bad Request", "Invalid or expired invitation");
@@ -39421,6 +39411,27 @@ var toInvitation = (row) => ({
39421
39411
  });
39422
39412
  var createNeonOrganizationStore = (databaseUrl) => createPostgresOrganizationStore(createNeonDatabase(databaseUrl));
39423
39413
  var createPostgresOrganizationStore = (db) => ({
39414
+ acceptInvitation: async ({ now, tokenHash, userId, verifiedEmail }) => {
39415
+ const claimed = db.$with("claimed").as(db.update(organizationInvitationsTable).set({ accepted_at_ms: now, state: "accepted" }).where(and(eq(organizationInvitationsTable.token_hash, tokenHash), eq(organizationInvitationsTable.state, "pending"), gt(organizationInvitationsTable.expires_at_ms, now), sql`lower(trim(${organizationInvitationsTable.email})) = ${verifiedEmail}`, exists(db.select().from(organizationsTable).where(eq(organizationsTable.organization_id, organizationInvitationsTable.organization_id))), notExists(db.select().from(organizationMembershipsTable).where(and(eq(organizationMembershipsTable.organization_id, organizationInvitationsTable.organization_id), eq(organizationMembershipsTable.user_id, userId), eq(organizationMembershipsTable.status, "suspended")))))).returning({
39416
+ organizationId: organizationInvitationsTable.organization_id,
39417
+ roles: organizationInvitationsTable.roles
39418
+ }));
39419
+ const [row] = await db.with(claimed).insert(organizationMembershipsTable).select(db.select({
39420
+ created_at_ms: sql`${now}`.as("created_at_ms"),
39421
+ organization_id: claimed.organizationId,
39422
+ roles: claimed.roles,
39423
+ status: sql`'active'`.as("status"),
39424
+ updated_at_ms: sql`${now}`.as("updated_at_ms"),
39425
+ user_id: sql`${userId}`.as("user_id")
39426
+ }).from(claimed)).onConflictDoUpdate({
39427
+ set: { user_id: userId },
39428
+ target: [
39429
+ organizationMembershipsTable.organization_id,
39430
+ organizationMembershipsTable.user_id
39431
+ ]
39432
+ }).returning();
39433
+ return row && row.status === "active" ? toMembership(row) : undefined;
39434
+ },
39424
39435
  deleteOrganization: async (organizationId) => {
39425
39436
  await db.delete(organizationsTable).where(eq(organizationsTable.organization_id, organizationId));
39426
39437
  },
@@ -40471,6 +40482,30 @@ var createInMemoryOrganizationStore = () => {
40471
40482
  const memberships = new Map;
40472
40483
  const invitations = new Map;
40473
40484
  return {
40485
+ acceptInvitation: async ({ tokenHash, userId, verifiedEmail, now }) => {
40486
+ const invitation = [...invitations.values()].find((value) => value.tokenHash === tokenHash);
40487
+ if (!invitation || invitation.state !== "pending" || invitation.expiresAt <= now || invitation.email.trim().toLowerCase() !== verifiedEmail || !organizations.has(invitation.organizationId))
40488
+ return;
40489
+ const key = membershipKey(invitation.organizationId, userId);
40490
+ const existing = memberships.get(key);
40491
+ if (existing?.status === "suspended")
40492
+ return;
40493
+ const membership = existing ?? {
40494
+ createdAt: now,
40495
+ organizationId: invitation.organizationId,
40496
+ roles: [...invitation.roles],
40497
+ status: "active",
40498
+ updatedAt: now,
40499
+ userId
40500
+ };
40501
+ invitations.set(invitation.invitationId, {
40502
+ ...invitation,
40503
+ acceptedAt: now,
40504
+ state: "accepted"
40505
+ });
40506
+ memberships.set(key, membership);
40507
+ return cloneMembership(membership);
40508
+ },
40474
40509
  deleteOrganization: async (organizationId) => {
40475
40510
  organizations.delete(organizationId);
40476
40511
  },
@@ -41573,5 +41608,5 @@ export {
41573
41608
  writeWarrant
41574
41609
  };
41575
41610
 
41576
- //# debugId=B0A4E820C09B981564756E2164756E21
41611
+ //# debugId=466F05C3ADFAC39864756E2164756E21
41577
41612
  //# sourceMappingURL=index.js.map