@robelest/convex-auth 0.0.2-preview.0 → 0.0.2-preview.2

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 (166) hide show
  1. package/dist/bin.cjs +17 -15
  2. package/dist/client/index.d.ts +84 -30
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js +259 -59
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/component/_generated/component.d.ts +46 -120
  7. package/dist/component/_generated/component.d.ts.map +1 -1
  8. package/dist/component/index.d.ts +2 -4
  9. package/dist/component/index.d.ts.map +1 -1
  10. package/dist/component/index.js +2 -4
  11. package/dist/component/index.js.map +1 -1
  12. package/dist/component/public.d.ts +233 -167
  13. package/dist/component/public.d.ts.map +1 -1
  14. package/dist/component/public.js +328 -155
  15. package/dist/component/public.js.map +1 -1
  16. package/dist/component/schema.d.ts +127 -12
  17. package/dist/component/schema.d.ts.map +1 -1
  18. package/dist/component/schema.js +136 -10
  19. package/dist/component/schema.js.map +1 -1
  20. package/dist/providers/{Anonymous.d.ts → anonymous.d.ts} +8 -8
  21. package/dist/providers/{Anonymous.d.ts.map → anonymous.d.ts.map} +1 -1
  22. package/dist/providers/{Anonymous.js → anonymous.js} +9 -10
  23. package/dist/providers/anonymous.js.map +1 -0
  24. package/dist/providers/{ConvexCredentials.d.ts → credentials.d.ts} +11 -11
  25. package/dist/providers/credentials.d.ts.map +1 -0
  26. package/dist/providers/{ConvexCredentials.js → credentials.js} +8 -8
  27. package/dist/providers/credentials.js.map +1 -0
  28. package/dist/providers/{Email.d.ts → email.d.ts} +6 -6
  29. package/dist/providers/email.d.ts.map +1 -0
  30. package/dist/providers/{Email.js → email.js} +6 -6
  31. package/dist/providers/email.js.map +1 -0
  32. package/dist/providers/{Password.d.ts → password.d.ts} +10 -10
  33. package/dist/providers/{Password.d.ts.map → password.d.ts.map} +1 -1
  34. package/dist/providers/{Password.js → password.js} +19 -20
  35. package/dist/providers/password.js.map +1 -0
  36. package/dist/providers/{Phone.d.ts → phone.d.ts} +3 -3
  37. package/dist/providers/{Phone.d.ts.map → phone.d.ts.map} +1 -1
  38. package/dist/providers/{Phone.js → phone.js} +3 -3
  39. package/dist/providers/{Phone.js.map → phone.js.map} +1 -1
  40. package/dist/server/implementation/db.d.ts +5 -2
  41. package/dist/server/implementation/db.d.ts.map +1 -1
  42. package/dist/server/implementation/db.js +2 -1
  43. package/dist/server/implementation/db.js.map +1 -1
  44. package/dist/server/implementation/index.d.ts +285 -180
  45. package/dist/server/implementation/index.d.ts.map +1 -1
  46. package/dist/server/implementation/index.js +280 -173
  47. package/dist/server/implementation/index.js.map +1 -1
  48. package/dist/server/implementation/mutations/createAccountFromCredentials.d.ts.map +1 -1
  49. package/dist/server/implementation/mutations/createAccountFromCredentials.js +8 -18
  50. package/dist/server/implementation/mutations/createAccountFromCredentials.js.map +1 -1
  51. package/dist/server/implementation/mutations/createVerificationCode.d.ts.map +1 -1
  52. package/dist/server/implementation/mutations/createVerificationCode.js +16 -44
  53. package/dist/server/implementation/mutations/createVerificationCode.js.map +1 -1
  54. package/dist/server/implementation/mutations/invalidateSessions.d.ts.map +1 -1
  55. package/dist/server/implementation/mutations/invalidateSessions.js +4 -8
  56. package/dist/server/implementation/mutations/invalidateSessions.js.map +1 -1
  57. package/dist/server/implementation/mutations/modifyAccount.d.ts.map +1 -1
  58. package/dist/server/implementation/mutations/modifyAccount.js +8 -19
  59. package/dist/server/implementation/mutations/modifyAccount.js.map +1 -1
  60. package/dist/server/implementation/mutations/refreshSession.d.ts.map +1 -1
  61. package/dist/server/implementation/mutations/refreshSession.js +9 -23
  62. package/dist/server/implementation/mutations/refreshSession.js.map +1 -1
  63. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.d.ts.map +1 -1
  64. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.js +6 -12
  65. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.js.map +1 -1
  66. package/dist/server/implementation/mutations/signIn.d.ts.map +1 -1
  67. package/dist/server/implementation/mutations/signIn.js +2 -1
  68. package/dist/server/implementation/mutations/signIn.js.map +1 -1
  69. package/dist/server/implementation/mutations/signOut.d.ts.map +1 -1
  70. package/dist/server/implementation/mutations/signOut.js +5 -6
  71. package/dist/server/implementation/mutations/signOut.js.map +1 -1
  72. package/dist/server/implementation/mutations/storeRef.d.ts +8 -0
  73. package/dist/server/implementation/mutations/storeRef.d.ts.map +1 -0
  74. package/dist/server/implementation/mutations/storeRef.js +8 -0
  75. package/dist/server/implementation/mutations/storeRef.js.map +1 -0
  76. package/dist/server/implementation/mutations/userOAuth.d.ts.map +1 -1
  77. package/dist/server/implementation/mutations/userOAuth.js +16 -53
  78. package/dist/server/implementation/mutations/userOAuth.js.map +1 -1
  79. package/dist/server/implementation/mutations/verifier.d.ts.map +1 -1
  80. package/dist/server/implementation/mutations/verifier.js +4 -8
  81. package/dist/server/implementation/mutations/verifier.js.map +1 -1
  82. package/dist/server/implementation/mutations/verifierSignature.d.ts.map +1 -1
  83. package/dist/server/implementation/mutations/verifierSignature.js +6 -10
  84. package/dist/server/implementation/mutations/verifierSignature.js.map +1 -1
  85. package/dist/server/implementation/mutations/verifyCodeAndSignIn.d.ts.map +1 -1
  86. package/dist/server/implementation/mutations/verifyCodeAndSignIn.js +7 -16
  87. package/dist/server/implementation/mutations/verifyCodeAndSignIn.js.map +1 -1
  88. package/dist/server/implementation/provider.d.ts +2 -1
  89. package/dist/server/implementation/provider.d.ts.map +1 -1
  90. package/dist/server/implementation/provider.js.map +1 -1
  91. package/dist/server/implementation/rateLimit.d.ts.map +1 -1
  92. package/dist/server/implementation/rateLimit.js +13 -39
  93. package/dist/server/implementation/rateLimit.js.map +1 -1
  94. package/dist/server/implementation/refreshTokens.d.ts +1 -8
  95. package/dist/server/implementation/refreshTokens.d.ts.map +1 -1
  96. package/dist/server/implementation/refreshTokens.js +14 -58
  97. package/dist/server/implementation/refreshTokens.js.map +1 -1
  98. package/dist/server/implementation/sessions.d.ts +2 -20
  99. package/dist/server/implementation/sessions.d.ts.map +1 -1
  100. package/dist/server/implementation/sessions.js +8 -35
  101. package/dist/server/implementation/sessions.js.map +1 -1
  102. package/dist/server/implementation/types.d.ts +11 -267
  103. package/dist/server/implementation/types.d.ts.map +1 -1
  104. package/dist/server/implementation/types.js +1 -181
  105. package/dist/server/implementation/types.js.map +1 -1
  106. package/dist/server/implementation/users.d.ts.map +1 -1
  107. package/dist/server/implementation/users.js +19 -67
  108. package/dist/server/implementation/users.js.map +1 -1
  109. package/dist/server/index.d.ts +18 -0
  110. package/dist/server/index.d.ts.map +1 -1
  111. package/dist/server/index.js +255 -0
  112. package/dist/server/index.js.map +1 -1
  113. package/dist/server/provider_utils.d.ts +1 -1
  114. package/dist/server/provider_utils.d.ts.map +1 -1
  115. package/dist/server/provider_utils.js +2 -2
  116. package/dist/server/provider_utils.js.map +1 -1
  117. package/dist/server/types.d.ts +91 -52
  118. package/dist/server/types.d.ts.map +1 -1
  119. package/package.json +3 -6
  120. package/src/cli/index.ts +20 -19
  121. package/src/client/index.ts +347 -110
  122. package/src/component/_generated/component.ts +55 -214
  123. package/src/component/index.ts +1 -11
  124. package/src/component/public.ts +366 -178
  125. package/src/component/schema.ts +150 -19
  126. package/src/providers/{Anonymous.ts → anonymous.ts} +10 -11
  127. package/src/providers/{ConvexCredentials.ts → credentials.ts} +11 -11
  128. package/src/providers/{Email.ts → email.ts} +5 -5
  129. package/src/providers/{Password.ts → password.ts} +22 -27
  130. package/src/providers/{Phone.ts → phone.ts} +2 -2
  131. package/src/server/implementation/db.ts +5 -2
  132. package/src/server/implementation/index.ts +368 -313
  133. package/src/server/implementation/mutations/createAccountFromCredentials.ts +11 -25
  134. package/src/server/implementation/mutations/createVerificationCode.ts +16 -47
  135. package/src/server/implementation/mutations/invalidateSessions.ts +4 -9
  136. package/src/server/implementation/mutations/modifyAccount.ts +8 -22
  137. package/src/server/implementation/mutations/refreshSession.ts +11 -24
  138. package/src/server/implementation/mutations/retrieveAccountWithCredentials.ts +9 -17
  139. package/src/server/implementation/mutations/signIn.ts +2 -1
  140. package/src/server/implementation/mutations/signOut.ts +5 -8
  141. package/src/server/implementation/mutations/storeRef.ts +7 -0
  142. package/src/server/implementation/mutations/userOAuth.ts +10 -50
  143. package/src/server/implementation/mutations/verifier.ts +4 -9
  144. package/src/server/implementation/mutations/verifierSignature.ts +6 -12
  145. package/src/server/implementation/mutations/verifyCodeAndSignIn.ts +7 -18
  146. package/src/server/implementation/provider.ts +2 -1
  147. package/src/server/implementation/rateLimit.ts +15 -41
  148. package/src/server/implementation/refreshTokens.ts +26 -76
  149. package/src/server/implementation/sessions.ts +8 -39
  150. package/src/server/implementation/types.ts +16 -191
  151. package/src/server/implementation/users.ts +19 -66
  152. package/src/server/index.ts +373 -0
  153. package/src/server/provider_utils.ts +2 -2
  154. package/src/server/types.ts +116 -51
  155. package/dist/providers/Anonymous.js.map +0 -1
  156. package/dist/providers/ConvexCredentials.d.ts.map +0 -1
  157. package/dist/providers/ConvexCredentials.js.map +0 -1
  158. package/dist/providers/Email.d.ts.map +0 -1
  159. package/dist/providers/Email.js.map +0 -1
  160. package/dist/providers/Password.js.map +0 -1
  161. package/providers/Anonymous/package.json +0 -6
  162. package/providers/ConvexCredentials/package.json +0 -6
  163. package/providers/Email/package.json +0 -6
  164. package/providers/Password/package.json +0 -6
  165. package/providers/Phone/package.json +0 -6
  166. package/server/package.json +0 -6
@@ -5,7 +5,8 @@ import { ConvexCredentialsConfig } from "../../types.js";
5
5
  import { upsertUserAndAccount } from "../users.js";
6
6
  import { getAuthSessionId } from "../sessions.js";
7
7
  import { LOG_LEVELS, logWithLevel, maybeRedact } from "../utils.js";
8
- import { createAuthDb } from "../db.js";
8
+ import { authDb } from "../db.js";
9
+ import { AUTH_STORE_REF } from "./storeRef.js";
9
10
 
10
11
  export const createAccountFromCredentialsArgs = v.object({
11
12
  provider: v.string(),
@@ -37,18 +38,12 @@ export async function createAccountFromCredentialsImpl(
37
38
  shouldLinkViaEmail,
38
39
  shouldLinkViaPhone,
39
40
  } = args;
40
- const authDb =
41
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
41
+ const db = authDb(ctx, config);
42
42
  const provider = getProviderOrThrow(providerId) as ConvexCredentialsConfig;
43
- const existingAccount =
44
- authDb !== null
45
- ? ((await authDb.accounts.get(provider.id, account.id)) as Doc<"account"> | null)
46
- : await ctx.db
47
- .query("account")
48
- .withIndex("providerAndAccountId", (q) =>
49
- q.eq("provider", provider.id).eq("providerAccountId", account.id),
50
- )
51
- .unique();
43
+ const existingAccount = (await db.accounts.get(
44
+ provider.id,
45
+ account.id,
46
+ )) as Doc<"account"> | null;
52
47
  if (existingAccount !== null) {
53
48
  if (
54
49
  account.secret !== undefined &&
@@ -63,10 +58,7 @@ export async function createAccountFromCredentialsImpl(
63
58
  return {
64
59
  account: existingAccount,
65
60
  // TODO: Ian removed this,
66
- user:
67
- authDb !== null
68
- ? ((await authDb.users.getById(existingAccount.userId)) as unknown as Doc<"user">)
69
- : (await ctx.db.get(existingAccount.userId))!,
61
+ user: (await db.users.getById(existingAccount.userId)) as unknown as Doc<"user">,
70
62
  };
71
63
  }
72
64
 
@@ -89,14 +81,8 @@ export async function createAccountFromCredentialsImpl(
89
81
  );
90
82
 
91
83
  return {
92
- account:
93
- authDb !== null
94
- ? ((await authDb.accounts.getById(accountId)) as Doc<"account">)
95
- : (await ctx.db.get(accountId))!,
96
- user:
97
- authDb !== null
98
- ? ((await authDb.users.getById(userId)) as unknown as Doc<"user">)
99
- : (await ctx.db.get(userId))!,
84
+ account: (await db.accounts.getById(accountId)) as Doc<"account">,
85
+ user: (await db.users.getById(userId)) as unknown as Doc<"user">,
100
86
  };
101
87
  }
102
88
 
@@ -104,7 +90,7 @@ export const callCreateAccountFromCredentials = async (
104
90
  ctx: ActionCtx,
105
91
  args: Infer<typeof createAccountFromCredentialsArgs>,
106
92
  ): Promise<ReturnType> => {
107
- return ctx.runMutation("auth:store" as any, {
93
+ return ctx.runMutation(AUTH_STORE_REF, {
108
94
  args: {
109
95
  type: "createAccountFromCredentials",
110
96
  ...args,
@@ -5,7 +5,8 @@ import { EmailConfig, PhoneConfig } from "../../types.js";
5
5
  import { getAccountOrThrow, upsertUserAndAccount } from "../users.js";
6
6
  import { getAuthSessionId } from "../sessions.js";
7
7
  import { LOG_LEVELS, logWithLevel, sha256 } from "../utils.js";
8
- import { createAuthDb } from "../db.js";
8
+ import { authDb } from "../db.js";
9
+ import { AUTH_STORE_REF } from "./storeRef.js";
9
10
 
10
11
  export const createVerificationCodeArgs = v.object({
11
12
  accountId: v.optional(v.string()),
@@ -35,24 +36,14 @@ export async function createVerificationCodeImpl(
35
36
  accountId: existingAccountId,
36
37
  allowExtraProviders,
37
38
  } = args;
38
- const authDb =
39
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
39
+ const db = authDb(ctx, config);
40
40
  const typedExistingAccountId = existingAccountId as
41
41
  | GenericId<"account">
42
42
  | undefined;
43
43
  const existingAccount =
44
44
  typedExistingAccountId !== undefined
45
45
  ? await getAccountOrThrow(ctx, typedExistingAccountId, config)
46
- : authDb !== null
47
- ? await authDb.accounts.get(providerId, email ?? phone!)
48
- : await ctx.db
49
- .query("account")
50
- .withIndex("providerAndAccountId", (q) =>
51
- q
52
- .eq("provider", providerId)
53
- .eq("providerAccountId", email ?? phone!),
54
- )
55
- .unique();
46
+ : await db.accounts.get(providerId, email ?? phone!);
56
47
 
57
48
  const provider = getProviderOrThrow(providerId, allowExtraProviders) as
58
49
  | EmailConfig
@@ -84,7 +75,7 @@ export const callCreateVerificationCode = async (
84
75
  ctx: ActionCtx,
85
76
  args: Infer<typeof createVerificationCodeArgs>,
86
77
  ): Promise<ReturnType> => {
87
- return ctx.runMutation("auth:store" as any, {
78
+ return ctx.runMutation(AUTH_STORE_REF, {
88
79
  args: {
89
80
  type: "createVerificationCode",
90
81
  ...args,
@@ -101,39 +92,17 @@ async function generateUniqueVerificationCode(
101
92
  { email, phone }: { email?: string; phone?: string },
102
93
  config: Provider.Config,
103
94
  ) {
104
- const authDb =
105
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
106
- const existingCode =
107
- authDb !== null
108
- ? await authDb.verificationCodes.getByAccountId(accountId)
109
- : await ctx.db
110
- .query("verification")
111
- .withIndex("accountId", (q) => q.eq("accountId", accountId))
112
- .unique();
95
+ const db = authDb(ctx, config);
96
+ const existingCode = await db.verificationCodes.getByAccountId(accountId);
113
97
  if (existingCode !== null) {
114
- if (authDb !== null) {
115
- await authDb.verificationCodes.delete(existingCode._id);
116
- } else {
117
- await ctx.db.delete(existingCode._id);
118
- }
119
- }
120
- if (authDb !== null) {
121
- await authDb.verificationCodes.create({
122
- accountId,
123
- provider,
124
- code: await sha256(code),
125
- expirationTime,
126
- emailVerified: email,
127
- phoneVerified: phone,
128
- });
129
- } else {
130
- await ctx.db.insert("verification", {
131
- accountId,
132
- provider,
133
- code: await sha256(code),
134
- expirationTime,
135
- emailVerified: email,
136
- phoneVerified: phone,
137
- });
98
+ await db.verificationCodes.delete(existingCode._id);
138
99
  }
100
+ await db.verificationCodes.create({
101
+ accountId,
102
+ provider,
103
+ code: await sha256(code),
104
+ expirationTime,
105
+ emailVerified: email,
106
+ phoneVerified: phone,
107
+ });
139
108
  }
@@ -3,7 +3,8 @@ import { deleteSession } from "../sessions.js";
3
3
  import { ActionCtx, MutationCtx } from "../types.js";
4
4
  import { LOG_LEVELS, logWithLevel } from "../utils.js";
5
5
  import * as Provider from "../provider.js";
6
- import { createAuthDb } from "../db.js";
6
+ import { authDb } from "../db.js";
7
+ import { AUTH_STORE_REF } from "./storeRef.js";
7
8
 
8
9
  export const invalidateSessionsArgs = v.object({
9
10
  userId: v.string(),
@@ -14,7 +15,7 @@ export const callInvalidateSessions = async (
14
15
  ctx: ActionCtx,
15
16
  args: Infer<typeof invalidateSessionsArgs>,
16
17
  ): Promise<void> => {
17
- return ctx.runMutation("auth:store" as any, {
18
+ return ctx.runMutation(AUTH_STORE_REF, {
18
19
  args: {
19
20
  type: "invalidateSessions",
20
21
  ...args,
@@ -31,13 +32,7 @@ export const invalidateSessionsImpl = async (
31
32
  const { userId, except } = args;
32
33
  const exceptSet = new Set(except ?? []);
33
34
  const typedUserId = userId as GenericId<"user">;
34
- const sessions =
35
- config.component !== undefined
36
- ? await createAuthDb(ctx, config.component).sessions.listByUser(typedUserId)
37
- : await ctx.db
38
- .query("session")
39
- .withIndex("userId", (q) => q.eq("userId", typedUserId))
40
- .collect();
35
+ const sessions = await authDb(ctx, config).sessions.listByUser(typedUserId);
41
36
  for (const session of sessions) {
42
37
  if (!exceptSet.has(session._id)) {
43
38
  await deleteSession(ctx, session, config);
@@ -3,7 +3,8 @@ import { ActionCtx, MutationCtx } from "../types.js";
3
3
  import { GetProviderOrThrowFunc, hash } from "../provider.js";
4
4
  import { LOG_LEVELS, logWithLevel, maybeRedact } from "../utils.js";
5
5
  import * as Provider from "../provider.js";
6
- import { createAuthDb } from "../db.js";
6
+ import { authDb } from "../db.js";
7
+ import { AUTH_STORE_REF } from "./storeRef.js";
7
8
 
8
9
  export const modifyAccountArgs = v.object({
9
10
  provider: v.string(),
@@ -17,8 +18,7 @@ export async function modifyAccountImpl(
17
18
  config: Provider.Config,
18
19
  ): Promise<void> {
19
20
  const { provider, account } = args;
20
- const authDb =
21
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
21
+ const db = authDb(ctx, config);
22
22
  logWithLevel(LOG_LEVELS.DEBUG, "retrieveAccountWithCredentialsImpl args:", {
23
23
  provider: provider,
24
24
  account: {
@@ -26,29 +26,15 @@ export async function modifyAccountImpl(
26
26
  secret: maybeRedact(account.secret ?? ""),
27
27
  },
28
28
  });
29
- const existingAccount =
30
- authDb !== null
31
- ? await authDb.accounts.get(provider, account.id)
32
- : await ctx.db
33
- .query("account")
34
- .withIndex("providerAndAccountId", (q) =>
35
- q.eq("provider", provider).eq("providerAccountId", account.id),
36
- )
37
- .unique();
29
+ const existingAccount = await db.accounts.get(provider, account.id);
38
30
  if (existingAccount === null) {
39
31
  throw new Error(
40
32
  `Cannot modify account with ID ${account.id} because it does not exist`,
41
33
  );
42
34
  }
43
- if (authDb !== null) {
44
- await authDb.accounts.patch(existingAccount._id, {
45
- secret: await hash(getProviderOrThrow(provider), account.secret),
46
- });
47
- } else {
48
- await ctx.db.patch(existingAccount._id, {
49
- secret: await hash(getProviderOrThrow(provider), account.secret),
50
- });
51
- }
35
+ await db.accounts.patch(existingAccount._id, {
36
+ secret: await hash(getProviderOrThrow(provider), account.secret),
37
+ });
52
38
  return;
53
39
  }
54
40
 
@@ -56,7 +42,7 @@ export const callModifyAccount = async (
56
42
  ctx: ActionCtx,
57
43
  args: Infer<typeof modifyAccountArgs>,
58
44
  ): Promise<void> => {
59
- return ctx.runMutation("auth:store" as any, {
45
+ return ctx.runMutation(AUTH_STORE_REF, {
60
46
  args: {
61
47
  type: "modifyAccount",
62
48
  ...args,
@@ -1,5 +1,5 @@
1
1
  import { Infer, v } from "convex/values";
2
- import { ActionCtx, MutationCtx } from "../types.js";
2
+ import { ActionCtx, Doc, MutationCtx } from "../types.js";
3
3
  import * as Provider from "../provider.js";
4
4
  import { logWithLevel, maybeRedact } from "../utils.js";
5
5
  import {
@@ -11,7 +11,8 @@ import {
11
11
  refreshTokenIfValid,
12
12
  } from "../refreshTokens.js";
13
13
  import { generateTokensForSession } from "../sessions.js";
14
- import { createAuthDb } from "../db.js";
14
+ import { authDb } from "../db.js";
15
+ import { AUTH_STORE_REF } from "./storeRef.js";
15
16
 
16
17
  export const refreshSessionArgs = v.object({
17
18
  refreshToken: v.string(),
@@ -28,8 +29,7 @@ export async function refreshSessionImpl(
28
29
  getProviderOrThrow: Provider.GetProviderOrThrowFunc,
29
30
  config: Provider.Config,
30
31
  ): Promise<ReturnType> {
31
- const authDb =
32
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
32
+ const db = authDb(ctx, config);
33
33
  const { refreshToken } = args;
34
34
  const { refreshTokenId, sessionId: tokenSessionId } =
35
35
  parseRefreshToken(refreshToken);
@@ -49,21 +49,14 @@ export async function refreshSessionImpl(
49
49
  if (validationResult === null) {
50
50
  // Replicating `deleteSession` but ensuring that we delete both the session
51
51
  // and the refresh token, even if one of them is missing.
52
- let session = null;
52
+ let session: Doc<"session"> | null = null;
53
53
  try {
54
- session =
55
- authDb !== null
56
- ? await authDb.sessions.getById(tokenSessionId)
57
- : await ctx.db.get(tokenSessionId);
54
+ session = await db.sessions.getById(tokenSessionId);
58
55
  } catch {
59
56
  logWithLevel("DEBUG", "Skipping invalid session id during refresh cleanup");
60
57
  }
61
58
  if (session !== null) {
62
- if (authDb !== null) {
63
- await authDb.sessions.delete(session._id);
64
- } else {
65
- await ctx.db.delete(session._id);
66
- }
59
+ await db.sessions.delete(session._id);
67
60
  }
68
61
  try {
69
62
  await deleteAllRefreshTokens(ctx, tokenSessionId, config);
@@ -83,15 +76,9 @@ export async function refreshSessionImpl(
83
76
 
84
77
  // First use -- mark as used and generate new refresh token
85
78
  if (tokenFirstUsed === undefined) {
86
- if (authDb !== null) {
87
- await authDb.refreshTokens.patch(refreshTokenId, {
88
- firstUsedTime: Date.now(),
89
- });
90
- } else {
91
- await ctx.db.patch(refreshTokenId, {
92
- firstUsedTime: Date.now(),
93
- });
94
- }
79
+ await db.refreshTokens.patch(refreshTokenId, {
80
+ firstUsedTime: Date.now(),
81
+ });
95
82
  const result = await generateTokensForSession(ctx, config, {
96
83
  userId,
97
84
  sessionId,
@@ -179,7 +166,7 @@ export const callRefreshSession = async (
179
166
  ctx: ActionCtx,
180
167
  args: Infer<typeof refreshSessionArgs>,
181
168
  ): Promise<ReturnType> => {
182
- return ctx.runMutation("auth:store" as any, {
169
+ return ctx.runMutation(AUTH_STORE_REF, {
183
170
  args: {
184
171
  type: "refreshSession",
185
172
  ...args,
@@ -7,7 +7,8 @@ import {
7
7
  } from "../rateLimit.js";
8
8
  import * as Provider from "../provider.js";
9
9
  import { LOG_LEVELS, logWithLevel, maybeRedact } from "../utils.js";
10
- import { createAuthDb } from "../db.js";
10
+ import { authDb } from "../db.js";
11
+ import { AUTH_STORE_REF } from "./storeRef.js";
11
12
 
12
13
  export const retrieveAccountWithCredentialsArgs = v.object({
13
14
  provider: v.string(),
@@ -27,8 +28,7 @@ export async function retrieveAccountWithCredentialsImpl(
27
28
  config: Provider.Config,
28
29
  ): Promise<ReturnType> {
29
30
  const { provider: providerId, account } = args;
30
- const authDb =
31
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
31
+ const db = authDb(ctx, config);
32
32
  logWithLevel(LOG_LEVELS.DEBUG, "retrieveAccountWithCredentialsImpl args:", {
33
33
  provider: providerId,
34
34
  account: {
@@ -36,15 +36,10 @@ export async function retrieveAccountWithCredentialsImpl(
36
36
  secret: maybeRedact(account.secret ?? ""),
37
37
  },
38
38
  });
39
- const existingAccount =
40
- authDb !== null
41
- ? ((await authDb.accounts.get(providerId, account.id)) as Doc<"account"> | null)
42
- : await ctx.db
43
- .query("account")
44
- .withIndex("providerAndAccountId", (q) =>
45
- q.eq("provider", providerId).eq("providerAccountId", account.id),
46
- )
47
- .unique();
39
+ const existingAccount = (await db.accounts.get(
40
+ providerId,
41
+ account.id,
42
+ )) as Doc<"account"> | null;
48
43
  if (existingAccount === null) {
49
44
  return "InvalidAccountId";
50
45
  }
@@ -67,10 +62,7 @@ export async function retrieveAccountWithCredentialsImpl(
67
62
  return {
68
63
  account: existingAccount,
69
64
  // TODO: Ian removed this
70
- user:
71
- authDb !== null
72
- ? ((await authDb.users.getById(existingAccount.userId)) as unknown as Doc<"user">)
73
- : (await ctx.db.get(existingAccount.userId))!,
65
+ user: (await db.users.getById(existingAccount.userId)) as unknown as Doc<"user">,
74
66
  };
75
67
  }
76
68
 
@@ -78,7 +70,7 @@ export const callRetreiveAccountWithCredentials = async (
78
70
  ctx: ActionCtx,
79
71
  args: Infer<typeof retrieveAccountWithCredentialsArgs>,
80
72
  ): Promise<ReturnType> => {
81
- return ctx.runMutation("auth:store" as any, {
73
+ return ctx.runMutation(AUTH_STORE_REF, {
82
74
  args: {
83
75
  type: "retrieveAccountWithCredentials",
84
76
  ...args,
@@ -6,6 +6,7 @@ import {
6
6
  maybeGenerateTokensForSession,
7
7
  } from "../sessions.js";
8
8
  import { LOG_LEVELS, logWithLevel } from "../utils.js";
9
+ import { AUTH_STORE_REF } from "./storeRef.js";
9
10
 
10
11
  export const signInArgs = v.object({
11
12
  userId: v.string(),
@@ -42,7 +43,7 @@ export const callSignIn = async (
42
43
  ctx: ActionCtx,
43
44
  args: Infer<typeof signInArgs>,
44
45
  ): Promise<ReturnType> => {
45
- return ctx.runMutation("auth:store" as any, {
46
+ return ctx.runMutation(AUTH_STORE_REF, {
46
47
  args: {
47
48
  type: "signIn",
48
49
  ...args,
@@ -2,7 +2,8 @@ import { GenericId } from "convex/values";
2
2
  import { ActionCtx, MutationCtx } from "../types.js";
3
3
  import { deleteSession, getAuthSessionId } from "../sessions.js";
4
4
  import * as Provider from "../provider.js";
5
- import { createAuthDb } from "../db.js";
5
+ import { authDb } from "../db.js";
6
+ import { AUTH_STORE_REF } from "./storeRef.js";
6
7
 
7
8
  type ReturnType = {
8
9
  userId: GenericId<"user">;
@@ -13,14 +14,10 @@ export async function signOutImpl(
13
14
  ctx: MutationCtx,
14
15
  config: Provider.Config,
15
16
  ): Promise<ReturnType> {
16
- const authDb =
17
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
17
+ const db = authDb(ctx, config);
18
18
  const sessionId = await getAuthSessionId(ctx);
19
19
  if (sessionId !== null) {
20
- const session =
21
- authDb !== null
22
- ? await authDb.sessions.getById(sessionId)
23
- : await ctx.db.get(sessionId);
20
+ const session = await db.sessions.getById(sessionId);
24
21
  if (session !== null) {
25
22
  await deleteSession(ctx, session, config);
26
23
  return { userId: session.userId, sessionId: session._id };
@@ -30,7 +27,7 @@ export async function signOutImpl(
30
27
  }
31
28
 
32
29
  export const callSignOut = async (ctx: ActionCtx): Promise<void> => {
33
- return ctx.runMutation("auth:store" as any, {
30
+ return ctx.runMutation(AUTH_STORE_REF, {
34
31
  args: {
35
32
  type: "signOut",
36
33
  },
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Internal function reference for the library's store dispatch mutation.
3
+ *
4
+ * This remains string-based because the library code cannot import the
5
+ * consumer app's generated `internal` API module.
6
+ */
7
+ export const AUTH_STORE_REF = "auth:store" as any;
@@ -4,7 +4,8 @@ import * as Provider from "../provider.js";
4
4
  import { OAuthConfig } from "@auth/core/providers/oauth.js";
5
5
  import { upsertUserAndAccount } from "../users.js";
6
6
  import { generateRandomString, logWithLevel, sha256 } from "../utils.js";
7
- import { createAuthDb } from "../db.js";
7
+ import { authDb } from "../db.js";
8
+ import { AUTH_STORE_REF } from "./storeRef.js";
8
9
 
9
10
  const OAUTH_SIGN_IN_EXPIRATION_MS = 1000 * 60 * 2; // 2 minutes
10
11
 
@@ -25,26 +26,11 @@ export async function userOAuthImpl(
25
26
  ): Promise<ReturnType> {
26
27
  logWithLevel("DEBUG", "userOAuthImpl args:", args);
27
28
  const { profile, provider, providerAccountId, signature } = args;
28
- const authDb =
29
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
29
+ const db = authDb(ctx, config);
30
30
  const providerConfig = getProviderOrThrow(provider) as OAuthConfig<any>;
31
- const existingAccount =
32
- authDb !== null
33
- ? await authDb.accounts.get(provider, providerAccountId)
34
- : await ctx.db
35
- .query("account")
36
- .withIndex("providerAndAccountId", (q) =>
37
- q.eq("provider", provider).eq("providerAccountId", providerAccountId),
38
- )
39
- .unique();
31
+ const existingAccount = await db.accounts.get(provider, providerAccountId);
40
32
 
41
- const verifier =
42
- authDb !== null
43
- ? await authDb.verifiers.getBySignature(signature)
44
- : await ctx.db
45
- .query("verifier")
46
- .withIndex("signature", (q) => q.eq("signature", signature))
47
- .unique();
33
+ const verifier = await db.verifiers.getBySignature(signature);
48
34
  if (verifier === null) {
49
35
  throw new Error("Invalid state");
50
36
  }
@@ -58,44 +44,18 @@ export async function userOAuthImpl(
58
44
  );
59
45
 
60
46
  const code = generateRandomString(8, "0123456789");
61
- if (authDb !== null) {
62
- await authDb.verifiers.delete(verifier._id);
63
- } else {
64
- await ctx.db.delete(verifier._id);
65
- }
66
- const existingVerificationCode =
67
- authDb !== null
68
- ? await authDb.verificationCodes.getByAccountId(accountId)
69
- : await ctx.db
70
- .query("verification")
71
- .withIndex("accountId", (q) => q.eq("accountId", accountId))
72
- .unique();
47
+ await db.verifiers.delete(verifier._id);
48
+ const existingVerificationCode = await db.verificationCodes.getByAccountId(accountId);
73
49
  if (existingVerificationCode !== null) {
74
- if (authDb !== null) {
75
- await authDb.verificationCodes.delete(existingVerificationCode._id);
76
- } else {
77
- await ctx.db.delete(existingVerificationCode._id);
78
- }
50
+ await db.verificationCodes.delete(existingVerificationCode._id);
79
51
  }
80
- if (authDb !== null) {
81
- await authDb.verificationCodes.create({
82
- code: await sha256(code),
83
- accountId,
84
- provider,
85
- expirationTime: Date.now() + OAUTH_SIGN_IN_EXPIRATION_MS,
86
- verifier: verifier._id,
87
- });
88
- } else {
89
- await ctx.db.insert("verification", {
52
+ await db.verificationCodes.create({
90
53
  code: await sha256(code),
91
54
  accountId,
92
55
  provider,
93
56
  expirationTime: Date.now() + OAUTH_SIGN_IN_EXPIRATION_MS,
94
- // The use of a verifier means we don't need an identifier
95
- // during verification.
96
57
  verifier: verifier._id,
97
58
  });
98
- }
99
59
  return code;
100
60
  }
101
61
 
@@ -103,7 +63,7 @@ export const callUserOAuth = async (
103
63
  ctx: ActionCtx,
104
64
  args: Infer<typeof userOAuthArgs>,
105
65
  ): Promise<ReturnType> => {
106
- return ctx.runMutation("auth:store" as any, {
66
+ return ctx.runMutation(AUTH_STORE_REF, {
107
67
  args: {
108
68
  type: "userOAuth",
109
69
  ...args,
@@ -2,7 +2,8 @@ import { GenericId } from "convex/values";
2
2
  import { ActionCtx, MutationCtx } from "../types.js";
3
3
  import { getAuthSessionId } from "../sessions.js";
4
4
  import * as Provider from "../provider.js";
5
- import { createAuthDb } from "../db.js";
5
+ import { authDb } from "../db.js";
6
+ import { AUTH_STORE_REF } from "./storeRef.js";
6
7
 
7
8
  type ReturnType = GenericId<"verifier">;
8
9
 
@@ -11,17 +12,11 @@ export async function verifierImpl(
11
12
  config: Provider.Config,
12
13
  ): Promise<ReturnType> {
13
14
  const sessionId = (await getAuthSessionId(ctx)) ?? undefined;
14
- if (config.component !== undefined) {
15
- return (await createAuthDb(ctx, config.component).verifiers.create(sessionId)) as
16
- ReturnType;
17
- }
18
- return await ctx.db.insert("verifier", {
19
- sessionId,
20
- });
15
+ return (await authDb(ctx, config).verifiers.create(sessionId)) as ReturnType;
21
16
  }
22
17
 
23
18
  export const callVerifier = async (ctx: ActionCtx): Promise<ReturnType> => {
24
- return ctx.runMutation("auth:store" as any, {
19
+ return ctx.runMutation(AUTH_STORE_REF, {
25
20
  args: {
26
21
  type: "verifier",
27
22
  },
@@ -1,7 +1,8 @@
1
1
  import { GenericId, Infer, v } from "convex/values";
2
2
  import { ActionCtx, MutationCtx } from "../types.js";
3
3
  import * as Provider from "../provider.js";
4
- import { createAuthDb } from "../db.js";
4
+ import { authDb } from "../db.js";
5
+ import { AUTH_STORE_REF } from "./storeRef.js";
5
6
 
6
7
  export const verifierSignatureArgs = v.object({
7
8
  verifier: v.string(),
@@ -16,26 +17,19 @@ export async function verifierSignatureImpl(
16
17
  config: Provider.Config,
17
18
  ): Promise<ReturnType> {
18
19
  const { verifier, signature } = args;
19
- const authDb =
20
- config.component !== undefined ? createAuthDb(ctx, config.component) : null;
21
- const verifierDoc =
22
- authDb !== null
23
- ? await authDb.verifiers.getById(verifier as GenericId<"verifier">)
24
- : await ctx.db.get(verifier as GenericId<"verifier">);
20
+ const db = authDb(ctx, config);
21
+ const verifierDoc = await db.verifiers.getById(verifier as GenericId<"verifier">);
25
22
  if (verifierDoc === null) {
26
23
  throw new Error("Invalid verifier");
27
24
  }
28
- if (authDb !== null) {
29
- return await authDb.verifiers.patch(verifierDoc._id, { signature });
30
- }
31
- return await ctx.db.patch(verifierDoc._id, { signature });
25
+ return await db.verifiers.patch(verifierDoc._id, { signature });
32
26
  }
33
27
 
34
28
  export const callVerifierSignature = async (
35
29
  ctx: ActionCtx,
36
30
  args: Infer<typeof verifierSignatureArgs>,
37
31
  ): Promise<void> => {
38
- return ctx.runMutation("auth:store" as any, {
32
+ return ctx.runMutation(AUTH_STORE_REF, {
39
33
  args: {
40
34
  type: "verifierSignature",
41
35
  ...args,