@supa-media/convex 1.7.1 → 1.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supa-media/convex",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "description": "Backend package for the Supa framework — OTP auth, schema helpers, and backend utilities for Convex",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/auth/index.ts CHANGED
@@ -12,6 +12,7 @@ export type {
12
12
  SupaAuthTestEmailConfig,
13
13
  SupaAuthTwilioConfig,
14
14
  SupaAuthUserCreated,
15
+ SupaAuthFindUserByEmail,
15
16
  } from "./setup";
16
17
  export {
17
18
  checkTwilioVerification,
package/src/auth/setup.ts CHANGED
@@ -28,10 +28,10 @@ import { Email } from "@convex-dev/auth/providers/Email";
28
28
  import { Phone } from "@convex-dev/auth/providers/Phone";
29
29
  import { assertMayReceiveEmailCode, type SupaAuthAdmission } from "./admission";
30
30
  import { createTestEmailOtps, type SupaAuthTestEmailConfig } from "./testEmail";
31
- import { userCallback, type SupaAuthUserCreated } from "./users";
31
+ import { userCallback, type SupaAuthFindUserByEmail, type SupaAuthUserCreated } from "./users";
32
32
 
33
33
  export { NOT_ADMITTED_MESSAGE, type SupaAuthAdmission } from "./admission";
34
- export type { SupaAuthUserCreated } from "./users";
34
+ export type { SupaAuthFindUserByEmail, SupaAuthUserCreated } from "./users";
35
35
 
36
36
  export {
37
37
  createTestEmailOtp,
@@ -135,6 +135,15 @@ export interface SupaAuthConfig {
135
135
  * scheduled action, not here.
136
136
  */
137
137
  onUserCreated?: SupaAuthUserCreated;
138
+ /**
139
+ * For an app that lets one person sign in with several emails: the user an
140
+ * address belongs to, or `null`. Asked when an email sign-in has no auth
141
+ * account yet, before the `users.email` match, so a sign-in through an
142
+ * attached address reaches its user rather than making a new one. Only
143
+ * return a user the address was confirmed for: whoever holds that mailbox
144
+ * signs in as them.
145
+ */
146
+ findUserByEmail?: SupaAuthFindUserByEmail;
138
147
  /** Resend email OTP configuration. */
139
148
  resend?: SupaAuthResendConfig;
140
149
  /** Twilio phone OTP configuration. */
@@ -425,6 +434,8 @@ export function createSupaAuth(config: SupaAuthConfig = {}) {
425
434
 
426
435
  return convexAuth({
427
436
  providers,
428
- callbacks: { createOrUpdateUser: userCallback(config.admission, config.onUserCreated) },
437
+ callbacks: {
438
+ createOrUpdateUser: userCallback(config.admission, config.onUserCreated, config.findUserByEmail),
439
+ },
429
440
  });
430
441
  }
package/src/auth/users.ts CHANGED
@@ -15,6 +15,12 @@ import type { convexAuth } from "@convex-dev/auth/server";
15
15
 
16
16
  import { assertMayCreateUser, type SupaAuthAdmission } from "./admission";
17
17
 
18
+ /** See `SupaAuthConfig.findUserByEmail`. */
19
+ export type SupaAuthFindUserByEmail = (
20
+ ctx: GenericMutationCtx<AnyDataModel>,
21
+ email: string,
22
+ ) => Promise<GenericId<"users"> | null>;
23
+
18
24
  /** See `SupaAuthConfig.onUserCreated`. */
19
25
  export type SupaAuthUserCreated = (
20
26
  ctx: GenericMutationCtx<AnyDataModel>,
@@ -28,6 +34,7 @@ type Handler = NonNullable<
28
34
  export function userCallback(
29
35
  admission: SupaAuthAdmission | undefined,
30
36
  onUserCreated?: SupaAuthUserCreated,
37
+ findUserByEmail?: SupaAuthFindUserByEmail,
31
38
  ): Handler {
32
39
  async function createOrUpdateUser(
33
40
  ctx: Parameters<Handler>[0],
@@ -44,8 +51,12 @@ export function userCallback(
44
51
  if (type === "email" || type === "verification") {
45
52
  updateData.emailVerificationTime = Date.now();
46
53
  }
47
- if (profile.phone) updateData.phone = profile.phone;
48
- if (profile.email) updateData.email = profile.email;
54
+ // Fill in a missing address, never replace one. A user may sign in
55
+ // through several auth accounts (an app that lets one person keep a
56
+ // work and a home email), and the address on the user row is the one
57
+ // they chose for mail, not whichever they signed in with last.
58
+ if (profile.phone && !existingUser.phone) updateData.phone = profile.phone;
59
+ if (profile.email && !existingUser.email) updateData.email = profile.email;
49
60
  if (profile.name) updateData.name = profile.name;
50
61
 
51
62
  if (Object.keys(updateData).length > 0) {
@@ -74,6 +85,13 @@ export function userCallback(
74
85
  // New auth account — try to link to existing user by email
75
86
  if (type === "email" && typeof profile.email === "string") {
76
87
  const email = profile.email;
88
+ // The app's own answer first: an address it has attached to a user as
89
+ // an extra sign-in email is that user, whatever the user row says.
90
+ const attached = findUserByEmail === undefined ? null : await findUserByEmail(ctx, email);
91
+ if (attached !== null) {
92
+ await ctx.db.patch(attached, { emailVerificationTime: Date.now() });
93
+ return attached;
94
+ }
77
95
  const existingUser = await ctx.db
78
96
  .query("users")
79
97
  .filter((q) => q.eq(q.field("email"), email))