@robelest/convex-auth 0.0.1

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 (280) hide show
  1. package/README.md +6 -0
  2. package/dist/bin.cjs +27733 -0
  3. package/dist/client/index.d.ts +49 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +283 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/component/_generated/api.d.ts +36 -0
  8. package/dist/component/_generated/api.d.ts.map +1 -0
  9. package/dist/component/_generated/api.js +31 -0
  10. package/dist/component/_generated/api.js.map +1 -0
  11. package/dist/component/_generated/component.d.ts +295 -0
  12. package/dist/component/_generated/component.d.ts.map +1 -0
  13. package/dist/component/_generated/component.js +11 -0
  14. package/dist/component/_generated/component.js.map +1 -0
  15. package/dist/component/_generated/dataModel.d.ts +46 -0
  16. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  17. package/dist/component/_generated/dataModel.js +11 -0
  18. package/dist/component/_generated/dataModel.js.map +1 -0
  19. package/dist/component/_generated/server.d.ts +121 -0
  20. package/dist/component/_generated/server.d.ts.map +1 -0
  21. package/dist/component/_generated/server.js +78 -0
  22. package/dist/component/_generated/server.js.map +1 -0
  23. package/dist/component/convex.config.d.ts +3 -0
  24. package/dist/component/convex.config.d.ts.map +1 -0
  25. package/dist/component/convex.config.js +4 -0
  26. package/dist/component/convex.config.js.map +1 -0
  27. package/dist/component/index.d.ts +15 -0
  28. package/dist/component/index.d.ts.map +1 -0
  29. package/dist/component/index.js +13 -0
  30. package/dist/component/index.js.map +1 -0
  31. package/dist/component/public.d.ts +450 -0
  32. package/dist/component/public.d.ts.map +1 -0
  33. package/dist/component/public.js +528 -0
  34. package/dist/component/public.js.map +1 -0
  35. package/dist/component/schema.d.ts +107 -0
  36. package/dist/component/schema.d.ts.map +1 -0
  37. package/dist/component/schema.js +26 -0
  38. package/dist/component/schema.js.map +1 -0
  39. package/dist/providers/Anonymous.d.ts +50 -0
  40. package/dist/providers/Anonymous.d.ts.map +1 -0
  41. package/dist/providers/Anonymous.js +39 -0
  42. package/dist/providers/Anonymous.js.map +1 -0
  43. package/dist/providers/ConvexCredentials.d.ts +88 -0
  44. package/dist/providers/ConvexCredentials.d.ts.map +1 -0
  45. package/dist/providers/ConvexCredentials.js +37 -0
  46. package/dist/providers/ConvexCredentials.js.map +1 -0
  47. package/dist/providers/Email.d.ts +33 -0
  48. package/dist/providers/Email.d.ts.map +1 -0
  49. package/dist/providers/Email.js +50 -0
  50. package/dist/providers/Email.js.map +1 -0
  51. package/dist/providers/Password.d.ts +95 -0
  52. package/dist/providers/Password.d.ts.map +1 -0
  53. package/dist/providers/Password.js +174 -0
  54. package/dist/providers/Password.js.map +1 -0
  55. package/dist/providers/Phone.d.ts +22 -0
  56. package/dist/providers/Phone.d.ts.map +1 -0
  57. package/dist/providers/Phone.js +37 -0
  58. package/dist/providers/Phone.js.map +1 -0
  59. package/dist/server/convex_types.d.ts +17 -0
  60. package/dist/server/convex_types.d.ts.map +1 -0
  61. package/dist/server/convex_types.js +2 -0
  62. package/dist/server/convex_types.js.map +1 -0
  63. package/dist/server/cookies.d.ts +35 -0
  64. package/dist/server/cookies.d.ts.map +1 -0
  65. package/dist/server/cookies.js +34 -0
  66. package/dist/server/cookies.js.map +1 -0
  67. package/dist/server/implementation/db.d.ts +80 -0
  68. package/dist/server/implementation/db.d.ts.map +1 -0
  69. package/dist/server/implementation/db.js +59 -0
  70. package/dist/server/implementation/db.js.map +1 -0
  71. package/dist/server/implementation/index.d.ts +370 -0
  72. package/dist/server/implementation/index.d.ts.map +1 -0
  73. package/dist/server/implementation/index.js +521 -0
  74. package/dist/server/implementation/index.js.map +1 -0
  75. package/dist/server/implementation/mutations/createAccountFromCredentials.d.ts +33 -0
  76. package/dist/server/implementation/mutations/createAccountFromCredentials.d.ts.map +1 -0
  77. package/dist/server/implementation/mutations/createAccountFromCredentials.js +71 -0
  78. package/dist/server/implementation/mutations/createAccountFromCredentials.js.map +1 -0
  79. package/dist/server/implementation/mutations/createVerificationCode.d.ts +25 -0
  80. package/dist/server/implementation/mutations/createVerificationCode.d.ts.map +1 -0
  81. package/dist/server/implementation/mutations/createVerificationCode.js +84 -0
  82. package/dist/server/implementation/mutations/createVerificationCode.js.map +1 -0
  83. package/dist/server/implementation/mutations/index.d.ts +304 -0
  84. package/dist/server/implementation/mutations/index.d.ts.map +1 -0
  85. package/dist/server/implementation/mutations/index.js +108 -0
  86. package/dist/server/implementation/mutations/index.js.map +1 -0
  87. package/dist/server/implementation/mutations/invalidateSessions.d.ts +13 -0
  88. package/dist/server/implementation/mutations/invalidateSessions.d.ts.map +1 -0
  89. package/dist/server/implementation/mutations/invalidateSessions.js +35 -0
  90. package/dist/server/implementation/mutations/invalidateSessions.js.map +1 -0
  91. package/dist/server/implementation/mutations/modifyAccount.d.ts +23 -0
  92. package/dist/server/implementation/mutations/modifyAccount.d.ts.map +1 -0
  93. package/dist/server/implementation/mutations/modifyAccount.js +48 -0
  94. package/dist/server/implementation/mutations/modifyAccount.js.map +1 -0
  95. package/dist/server/implementation/mutations/refreshSession.d.ts +16 -0
  96. package/dist/server/implementation/mutations/refreshSession.d.ts.map +1 -0
  97. package/dist/server/implementation/mutations/refreshSession.js +116 -0
  98. package/dist/server/implementation/mutations/refreshSession.js.map +1 -0
  99. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.d.ts +27 -0
  100. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.d.ts.map +1 -0
  101. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.js +55 -0
  102. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.js.map +1 -0
  103. package/dist/server/implementation/mutations/signIn.d.ts +17 -0
  104. package/dist/server/implementation/mutations/signIn.d.ts.map +1 -0
  105. package/dist/server/implementation/mutations/signIn.js +26 -0
  106. package/dist/server/implementation/mutations/signIn.js.map +1 -0
  107. package/dist/server/implementation/mutations/signOut.d.ts +11 -0
  108. package/dist/server/implementation/mutations/signOut.d.ts.map +1 -0
  109. package/dist/server/implementation/mutations/signOut.js +24 -0
  110. package/dist/server/implementation/mutations/signOut.js.map +1 -0
  111. package/dist/server/implementation/mutations/userOAuth.d.ts +19 -0
  112. package/dist/server/implementation/mutations/userOAuth.d.ts.map +1 -0
  113. package/dist/server/implementation/mutations/userOAuth.js +84 -0
  114. package/dist/server/implementation/mutations/userOAuth.js.map +1 -0
  115. package/dist/server/implementation/mutations/verifier.d.ts +8 -0
  116. package/dist/server/implementation/mutations/verifier.d.ts.map +1 -0
  117. package/dist/server/implementation/mutations/verifier.js +19 -0
  118. package/dist/server/implementation/mutations/verifier.js.map +1 -0
  119. package/dist/server/implementation/mutations/verifierSignature.d.ts +15 -0
  120. package/dist/server/implementation/mutations/verifierSignature.d.ts.map +1 -0
  121. package/dist/server/implementation/mutations/verifierSignature.js +29 -0
  122. package/dist/server/implementation/mutations/verifierSignature.js.map +1 -0
  123. package/dist/server/implementation/mutations/verifyCodeAndSignIn.d.ts +21 -0
  124. package/dist/server/implementation/mutations/verifyCodeAndSignIn.d.ts.map +1 -0
  125. package/dist/server/implementation/mutations/verifyCodeAndSignIn.js +127 -0
  126. package/dist/server/implementation/mutations/verifyCodeAndSignIn.js.map +1 -0
  127. package/dist/server/implementation/provider.d.ts +6 -0
  128. package/dist/server/implementation/provider.d.ts.map +1 -0
  129. package/dist/server/implementation/provider.js +21 -0
  130. package/dist/server/implementation/provider.js.map +1 -0
  131. package/dist/server/implementation/rateLimit.d.ts +6 -0
  132. package/dist/server/implementation/rateLimit.d.ts.map +1 -0
  133. package/dist/server/implementation/rateLimit.js +76 -0
  134. package/dist/server/implementation/rateLimit.js.map +1 -0
  135. package/dist/server/implementation/redirects.d.ts +6 -0
  136. package/dist/server/implementation/redirects.d.ts.map +1 -0
  137. package/dist/server/implementation/redirects.js +40 -0
  138. package/dist/server/implementation/redirects.js.map +1 -0
  139. package/dist/server/implementation/refreshTokens.d.ts +40 -0
  140. package/dist/server/implementation/refreshTokens.d.ts.map +1 -0
  141. package/dist/server/implementation/refreshTokens.js +160 -0
  142. package/dist/server/implementation/refreshTokens.js.map +1 -0
  143. package/dist/server/implementation/sessions.d.ts +43 -0
  144. package/dist/server/implementation/sessions.d.ts.map +1 -0
  145. package/dist/server/implementation/sessions.js +94 -0
  146. package/dist/server/implementation/sessions.js.map +1 -0
  147. package/dist/server/implementation/signIn.d.ts +31 -0
  148. package/dist/server/implementation/signIn.d.ts.map +1 -0
  149. package/dist/server/implementation/signIn.js +148 -0
  150. package/dist/server/implementation/signIn.js.map +1 -0
  151. package/dist/server/implementation/tokens.d.ts +7 -0
  152. package/dist/server/implementation/tokens.d.ts.map +1 -0
  153. package/dist/server/implementation/tokens.js +18 -0
  154. package/dist/server/implementation/tokens.js.map +1 -0
  155. package/dist/server/implementation/types.d.ts +288 -0
  156. package/dist/server/implementation/types.d.ts.map +1 -0
  157. package/dist/server/implementation/types.js +182 -0
  158. package/dist/server/implementation/types.js.map +1 -0
  159. package/dist/server/implementation/users.d.ts +27 -0
  160. package/dist/server/implementation/users.d.ts.map +1 -0
  161. package/dist/server/implementation/users.js +181 -0
  162. package/dist/server/implementation/users.js.map +1 -0
  163. package/dist/server/implementation/utils.d.ts +17 -0
  164. package/dist/server/implementation/utils.d.ts.map +1 -0
  165. package/dist/server/implementation/utils.js +72 -0
  166. package/dist/server/implementation/utils.js.map +1 -0
  167. package/dist/server/index.d.ts +17 -0
  168. package/dist/server/index.d.ts.map +1 -0
  169. package/dist/server/index.js +54 -0
  170. package/dist/server/index.js.map +1 -0
  171. package/dist/server/oauth/authorizationUrl.d.ts +13 -0
  172. package/dist/server/oauth/authorizationUrl.d.ts.map +1 -0
  173. package/dist/server/oauth/authorizationUrl.js +91 -0
  174. package/dist/server/oauth/authorizationUrl.js.map +1 -0
  175. package/dist/server/oauth/callback.d.ts +19 -0
  176. package/dist/server/oauth/callback.d.ts.map +1 -0
  177. package/dist/server/oauth/callback.js +173 -0
  178. package/dist/server/oauth/callback.js.map +1 -0
  179. package/dist/server/oauth/checks.d.ts +52 -0
  180. package/dist/server/oauth/checks.d.ts.map +1 -0
  181. package/dist/server/oauth/checks.js +106 -0
  182. package/dist/server/oauth/checks.js.map +1 -0
  183. package/dist/server/oauth/convexAuth.d.ts +12 -0
  184. package/dist/server/oauth/convexAuth.d.ts.map +1 -0
  185. package/dist/server/oauth/convexAuth.js +137 -0
  186. package/dist/server/oauth/convexAuth.js.map +1 -0
  187. package/dist/server/oauth/lib/utils/customFetch.d.ts +9 -0
  188. package/dist/server/oauth/lib/utils/customFetch.d.ts.map +1 -0
  189. package/dist/server/oauth/lib/utils/customFetch.js +11 -0
  190. package/dist/server/oauth/lib/utils/customFetch.js.map +1 -0
  191. package/dist/server/oauth/lib/utils/providers.d.ts +3 -0
  192. package/dist/server/oauth/lib/utils/providers.d.ts.map +1 -0
  193. package/dist/server/oauth/lib/utils/providers.js +7 -0
  194. package/dist/server/oauth/lib/utils/providers.js.map +1 -0
  195. package/dist/server/oauth/providers/oauth.d.ts +43 -0
  196. package/dist/server/oauth/providers/oauth.d.ts.map +1 -0
  197. package/dist/server/oauth/providers/oauth.js +3 -0
  198. package/dist/server/oauth/providers/oauth.js.map +1 -0
  199. package/dist/server/oauth/types.d.ts +24 -0
  200. package/dist/server/oauth/types.d.ts.map +1 -0
  201. package/dist/server/oauth/types.js +5 -0
  202. package/dist/server/oauth/types.js.map +1 -0
  203. package/dist/server/provider_utils.d.ts +76 -0
  204. package/dist/server/provider_utils.d.ts.map +1 -0
  205. package/dist/server/provider_utils.js +177 -0
  206. package/dist/server/provider_utils.js.map +1 -0
  207. package/dist/server/types.d.ts +412 -0
  208. package/dist/server/types.d.ts.map +1 -0
  209. package/dist/server/types.js +2 -0
  210. package/dist/server/types.js.map +1 -0
  211. package/dist/server/utils.d.ts +3 -0
  212. package/dist/server/utils.d.ts.map +1 -0
  213. package/dist/server/utils.js +11 -0
  214. package/dist/server/utils.js.map +1 -0
  215. package/package.json +126 -0
  216. package/providers/Anonymous/package.json +6 -0
  217. package/providers/ConvexCredentials/package.json +6 -0
  218. package/providers/Email/package.json +6 -0
  219. package/providers/Password/package.json +6 -0
  220. package/providers/Phone/package.json +6 -0
  221. package/server/package.json +6 -0
  222. package/src/cli/command.ts +69 -0
  223. package/src/cli/generateKeys.ts +20 -0
  224. package/src/cli/index.ts +840 -0
  225. package/src/client/index.ts +415 -0
  226. package/src/component/_generated/api.ts +52 -0
  227. package/src/component/_generated/component.ts +586 -0
  228. package/src/component/_generated/dataModel.ts +60 -0
  229. package/src/component/_generated/server.ts +156 -0
  230. package/src/component/convex.config.ts +5 -0
  231. package/src/component/index.ts +40 -0
  232. package/src/component/public.ts +607 -0
  233. package/src/component/schema.ts +35 -0
  234. package/src/providers/Anonymous.ts +79 -0
  235. package/src/providers/ConvexCredentials.ts +108 -0
  236. package/src/providers/Email.ts +60 -0
  237. package/src/providers/Password.ts +253 -0
  238. package/src/providers/Phone.ts +46 -0
  239. package/src/server/convex_types.ts +55 -0
  240. package/src/server/cookies.ts +42 -0
  241. package/src/server/implementation/db.ts +125 -0
  242. package/src/server/implementation/index.ts +815 -0
  243. package/src/server/implementation/mutations/createAccountFromCredentials.ts +113 -0
  244. package/src/server/implementation/mutations/createVerificationCode.ts +139 -0
  245. package/src/server/implementation/mutations/index.ts +157 -0
  246. package/src/server/implementation/mutations/invalidateSessions.ts +47 -0
  247. package/src/server/implementation/mutations/modifyAccount.ts +65 -0
  248. package/src/server/implementation/mutations/refreshSession.ts +188 -0
  249. package/src/server/implementation/mutations/retrieveAccountWithCredentials.ts +87 -0
  250. package/src/server/implementation/mutations/signIn.ts +51 -0
  251. package/src/server/implementation/mutations/signOut.ts +38 -0
  252. package/src/server/implementation/mutations/userOAuth.ts +112 -0
  253. package/src/server/implementation/mutations/verifier.ts +29 -0
  254. package/src/server/implementation/mutations/verifierSignature.ts +44 -0
  255. package/src/server/implementation/mutations/verifyCodeAndSignIn.ts +205 -0
  256. package/src/server/implementation/provider.ts +38 -0
  257. package/src/server/implementation/rateLimit.ts +105 -0
  258. package/src/server/implementation/redirects.ts +58 -0
  259. package/src/server/implementation/refreshTokens.ts +221 -0
  260. package/src/server/implementation/sessions.ts +155 -0
  261. package/src/server/implementation/signIn.ts +253 -0
  262. package/src/server/implementation/tokens.ts +29 -0
  263. package/src/server/implementation/types.ts +220 -0
  264. package/src/server/implementation/users.ts +286 -0
  265. package/src/server/implementation/utils.ts +91 -0
  266. package/src/server/index.ts +74 -0
  267. package/src/server/oauth/NOTICE.txt +21 -0
  268. package/src/server/oauth/README.md +7 -0
  269. package/src/server/oauth/authorizationUrl.ts +113 -0
  270. package/src/server/oauth/callback.ts +243 -0
  271. package/src/server/oauth/checks.ts +136 -0
  272. package/src/server/oauth/convexAuth.ts +168 -0
  273. package/src/server/oauth/lib/utils/customFetch.ts +18 -0
  274. package/src/server/oauth/lib/utils/providers.ts +12 -0
  275. package/src/server/oauth/providers/oauth.ts +56 -0
  276. package/src/server/oauth/types.ts +60 -0
  277. package/src/server/provider_utils.ts +222 -0
  278. package/src/server/types.ts +470 -0
  279. package/src/server/utils.ts +12 -0
  280. package/src/test.ts +24 -0
@@ -0,0 +1,607 @@
1
+ import { v } from "convex/values";
2
+ import { mutation, query } from "./_generated/server";
3
+
4
+ // Users
5
+ export const userGetById = query({
6
+ args: { userId: v.id("user") },
7
+ handler: async (ctx, { userId }) => {
8
+ return await ctx.db.get(userId);
9
+ },
10
+ });
11
+
12
+ export const userFindByVerifiedEmail = query({
13
+ args: { email: v.string() },
14
+ handler: async (ctx, { email }) => {
15
+ const users = await ctx.db
16
+ .query("user")
17
+ .withIndex("email", (q) => q.eq("email", email))
18
+ .filter((q) => q.neq(q.field("emailVerificationTime"), undefined))
19
+ .take(2);
20
+ return users.length === 1 ? users[0] : null;
21
+ },
22
+ });
23
+
24
+ export const userFindByVerifiedPhone = query({
25
+ args: { phone: v.string() },
26
+ handler: async (ctx, { phone }) => {
27
+ const users = await ctx.db
28
+ .query("user")
29
+ .withIndex("phone", (q) => q.eq("phone", phone))
30
+ .filter((q) => q.neq(q.field("phoneVerificationTime"), undefined))
31
+ .take(2);
32
+ return users.length === 1 ? users[0] : null;
33
+ },
34
+ });
35
+
36
+ export const userInsert = mutation({
37
+ args: { data: v.any() },
38
+ handler: async (ctx, { data }) => {
39
+ return await ctx.db.insert("user", data);
40
+ },
41
+ });
42
+
43
+ export const userUpsert = mutation({
44
+ args: { userId: v.optional(v.id("user")), data: v.any() },
45
+ handler: async (ctx, { userId, data }) => {
46
+ if (userId !== undefined) {
47
+ await ctx.db.patch(userId, data);
48
+ return userId;
49
+ }
50
+ return await ctx.db.insert("user", data);
51
+ },
52
+ });
53
+
54
+ export const userPatch = mutation({
55
+ args: { userId: v.id("user"), data: v.any() },
56
+ handler: async (ctx, { userId, data }) => {
57
+ await ctx.db.patch(userId, data);
58
+ },
59
+ });
60
+
61
+ // Accounts
62
+ export const accountGet = query({
63
+ args: { provider: v.string(), providerAccountId: v.string() },
64
+ handler: async (ctx, { provider, providerAccountId }) => {
65
+ return await ctx.db
66
+ .query("account")
67
+ .withIndex("providerAndAccountId", (q) =>
68
+ q.eq("provider", provider).eq("providerAccountId", providerAccountId),
69
+ )
70
+ .unique();
71
+ },
72
+ });
73
+
74
+ export const accountGetById = query({
75
+ args: { accountId: v.id("account") },
76
+ handler: async (ctx, { accountId }) => {
77
+ return await ctx.db.get(accountId);
78
+ },
79
+ });
80
+
81
+ export const accountInsert = mutation({
82
+ args: {
83
+ userId: v.id("user"),
84
+ provider: v.string(),
85
+ providerAccountId: v.string(),
86
+ secret: v.optional(v.string()),
87
+ },
88
+ handler: async (ctx, args) => {
89
+ return await ctx.db.insert("account", args as any);
90
+ },
91
+ });
92
+
93
+ export const accountPatch = mutation({
94
+ args: { accountId: v.id("account"), data: v.any() },
95
+ handler: async (ctx, { accountId, data }) => {
96
+ await ctx.db.patch(accountId, data);
97
+ },
98
+ });
99
+
100
+ export const accountDelete = mutation({
101
+ args: { accountId: v.id("account") },
102
+ handler: async (ctx, { accountId }) => {
103
+ await ctx.db.delete(accountId);
104
+ },
105
+ });
106
+
107
+ // Sessions
108
+ export const sessionCreate = mutation({
109
+ args: { userId: v.id("user"), expirationTime: v.number() },
110
+ handler: async (ctx, { userId, expirationTime }) => {
111
+ return await ctx.db.insert("session", {
112
+ userId: userId as any,
113
+ expirationTime,
114
+ });
115
+ },
116
+ });
117
+
118
+ export const sessionGetById = query({
119
+ args: { sessionId: v.id("session") },
120
+ handler: async (ctx, { sessionId }) => {
121
+ return await ctx.db.get(sessionId);
122
+ },
123
+ });
124
+
125
+ export const sessionDelete = mutation({
126
+ args: { sessionId: v.id("session") },
127
+ handler: async (ctx, { sessionId }) => {
128
+ if ((await ctx.db.get(sessionId)) !== null) {
129
+ await ctx.db.delete(sessionId);
130
+ }
131
+ },
132
+ });
133
+
134
+ export const sessionListByUser = query({
135
+ args: { userId: v.id("user") },
136
+ handler: async (ctx, { userId }) => {
137
+ return await ctx.db
138
+ .query("session")
139
+ .withIndex("userId", (q) => q.eq("userId", userId as any))
140
+ .collect();
141
+ },
142
+ });
143
+
144
+ // Verifiers
145
+ export const verifierCreate = mutation({
146
+ args: { sessionId: v.optional(v.id("session")) },
147
+ handler: async (ctx, { sessionId }) => {
148
+ return await ctx.db.insert("verifier", { sessionId: sessionId as any });
149
+ },
150
+ });
151
+
152
+ export const verifierGetById = query({
153
+ args: { verifierId: v.id("verifier") },
154
+ handler: async (ctx, { verifierId }) => {
155
+ return await ctx.db.get(verifierId);
156
+ },
157
+ });
158
+
159
+ export const verifierGetBySignature = query({
160
+ args: { signature: v.string() },
161
+ handler: async (ctx, { signature }) => {
162
+ return await ctx.db
163
+ .query("verifier")
164
+ .withIndex("signature", (q) => q.eq("signature", signature))
165
+ .unique();
166
+ },
167
+ });
168
+
169
+ export const verifierPatch = mutation({
170
+ args: { verifierId: v.id("verifier"), data: v.any() },
171
+ handler: async (ctx, { verifierId, data }) => {
172
+ await ctx.db.patch(verifierId, data);
173
+ },
174
+ });
175
+
176
+ export const verifierDelete = mutation({
177
+ args: { verifierId: v.id("verifier") },
178
+ handler: async (ctx, { verifierId }) => {
179
+ await ctx.db.delete(verifierId);
180
+ },
181
+ });
182
+
183
+ // Verification codes
184
+ export const verificationCodeGetByAccountId = query({
185
+ args: { accountId: v.id("account") },
186
+ handler: async (ctx, { accountId }) => {
187
+ return await ctx.db
188
+ .query("verification")
189
+ .withIndex("accountId", (q) => q.eq("accountId", accountId as any))
190
+ .unique();
191
+ },
192
+ });
193
+
194
+ export const verificationCodeGetByCode = query({
195
+ args: { code: v.string() },
196
+ handler: async (ctx, { code }) => {
197
+ return await ctx.db
198
+ .query("verification")
199
+ .withIndex("code", (q) => q.eq("code", code))
200
+ .unique();
201
+ },
202
+ });
203
+
204
+ export const verificationCodeCreate = mutation({
205
+ args: {
206
+ accountId: v.id("account"),
207
+ provider: v.string(),
208
+ code: v.string(),
209
+ expirationTime: v.number(),
210
+ verifier: v.optional(v.string()),
211
+ emailVerified: v.optional(v.string()),
212
+ phoneVerified: v.optional(v.string()),
213
+ },
214
+ handler: async (ctx, args) => {
215
+ return await ctx.db.insert("verification", args as any);
216
+ },
217
+ });
218
+
219
+ export const verificationCodeDelete = mutation({
220
+ args: { verificationCodeId: v.id("verification") },
221
+ handler: async (ctx, { verificationCodeId }) => {
222
+ await ctx.db.delete(verificationCodeId);
223
+ },
224
+ });
225
+
226
+ // Refresh tokens
227
+ export const refreshTokenCreate = mutation({
228
+ args: {
229
+ sessionId: v.id("session"),
230
+ expirationTime: v.number(),
231
+ parentRefreshTokenId: v.optional(v.id("token")),
232
+ },
233
+ handler: async (ctx, args) => {
234
+ return await ctx.db.insert("token", args as any);
235
+ },
236
+ });
237
+
238
+ export const refreshTokenGetById = query({
239
+ args: { refreshTokenId: v.id("token") },
240
+ handler: async (ctx, { refreshTokenId }) => {
241
+ return await ctx.db.get(refreshTokenId);
242
+ },
243
+ });
244
+
245
+ export const refreshTokenPatch = mutation({
246
+ args: { refreshTokenId: v.id("token"), data: v.any() },
247
+ handler: async (ctx, { refreshTokenId, data }) => {
248
+ await ctx.db.patch(refreshTokenId, data);
249
+ },
250
+ });
251
+
252
+ export const refreshTokenGetChildren = query({
253
+ args: {
254
+ sessionId: v.id("session"),
255
+ parentRefreshTokenId: v.id("token"),
256
+ },
257
+ handler: async (ctx, { sessionId, parentRefreshTokenId }) => {
258
+ return await ctx.db
259
+ .query("token")
260
+ .withIndex("sessionIdAndParentRefreshTokenId", (q) =>
261
+ q
262
+ .eq("sessionId", sessionId as any)
263
+ .eq("parentRefreshTokenId", parentRefreshTokenId as any),
264
+ )
265
+ .collect();
266
+ },
267
+ });
268
+
269
+ export const refreshTokenListBySession = query({
270
+ args: { sessionId: v.id("session") },
271
+ handler: async (ctx, { sessionId }) => {
272
+ return await ctx.db
273
+ .query("token")
274
+ .withIndex("sessionIdAndParentRefreshTokenId", (q) =>
275
+ q.eq("sessionId", sessionId as any),
276
+ )
277
+ .collect();
278
+ },
279
+ });
280
+
281
+ export const refreshTokenDeleteAll = mutation({
282
+ args: { sessionId: v.id("session") },
283
+ handler: async (ctx, { sessionId }) => {
284
+ const tokens = await ctx.db
285
+ .query("token")
286
+ .withIndex("sessionIdAndParentRefreshTokenId", (q) =>
287
+ q.eq("sessionId", sessionId as any),
288
+ )
289
+ .collect();
290
+ await Promise.all(tokens.map((token) => ctx.db.delete(token._id)));
291
+ },
292
+ });
293
+
294
+ export const refreshTokenGetActive = query({
295
+ args: { sessionId: v.id("session") },
296
+ handler: async (ctx, { sessionId }) => {
297
+ return await ctx.db
298
+ .query("token")
299
+ .withIndex("sessionId", (q) => q.eq("sessionId", sessionId as any))
300
+ .filter((q) => q.eq(q.field("firstUsedTime"), undefined))
301
+ .order("desc")
302
+ .first();
303
+ },
304
+ });
305
+
306
+ // Rate limits
307
+ export const rateLimitGet = query({
308
+ args: { identifier: v.string() },
309
+ handler: async (ctx, { identifier }) => {
310
+ return await ctx.db
311
+ .query("limit")
312
+ .withIndex("identifier", (q) => q.eq("identifier", identifier))
313
+ .unique();
314
+ },
315
+ });
316
+
317
+ export const rateLimitCreate = mutation({
318
+ args: {
319
+ identifier: v.string(),
320
+ attemptsLeft: v.number(),
321
+ lastAttemptTime: v.number(),
322
+ },
323
+ handler: async (ctx, args) => {
324
+ return await ctx.db.insert("limit", args);
325
+ },
326
+ });
327
+
328
+ export const rateLimitPatch = mutation({
329
+ args: { rateLimitId: v.id("limit"), data: v.any() },
330
+ handler: async (ctx, { rateLimitId, data }) => {
331
+ await ctx.db.patch(rateLimitId, data);
332
+ },
333
+ });
334
+
335
+ export const rateLimitDelete = mutation({
336
+ args: { rateLimitId: v.id("limit") },
337
+ handler: async (ctx, { rateLimitId }) => {
338
+ await ctx.db.delete(rateLimitId);
339
+ },
340
+ });
341
+
342
+ // Singular aliases
343
+ export const verificationGetByAccountId = verificationCodeGetByAccountId;
344
+ export const verificationGetByCode = verificationCodeGetByCode;
345
+ export const verificationCreate = verificationCodeCreate;
346
+ export const verificationDelete = verificationCodeDelete;
347
+
348
+ export const tokenCreate = refreshTokenCreate;
349
+ export const tokenGetById = refreshTokenGetById;
350
+ export const tokenPatch = refreshTokenPatch;
351
+ export const tokenGetChildren = refreshTokenGetChildren;
352
+ export const tokenListBySession = refreshTokenListBySession;
353
+ export const tokenDeleteAll = refreshTokenDeleteAll;
354
+ export const tokenGetActive = refreshTokenGetActive;
355
+
356
+ export const limitGet = rateLimitGet;
357
+ export const limitCreate = rateLimitCreate;
358
+ export const limitPatch = rateLimitPatch;
359
+ export const limitDelete = rateLimitDelete;
360
+
361
+ // Organization
362
+ export const organizationCreate = mutation({
363
+ args: { data: v.any() },
364
+ handler: async (ctx, { data }) => {
365
+ return await (ctx.db as any).insert("organization", data);
366
+ },
367
+ });
368
+
369
+ export const organizationGet = query({
370
+ args: { organizationId: v.id("organization") },
371
+ handler: async (ctx, { organizationId }) => {
372
+ return await (ctx.db as any).get(organizationId);
373
+ },
374
+ });
375
+
376
+ export const organizationList = query({
377
+ args: { ownerUserId: v.optional(v.id("user")) },
378
+ handler: async (ctx, { ownerUserId }) => {
379
+ if (ownerUserId === undefined) {
380
+ return await (ctx.db as any).query("organization").collect();
381
+ }
382
+ return await (ctx.db as any)
383
+ .query("organization")
384
+ .withIndex("ownerUserId", (q: any) => q.eq("ownerUserId", ownerUserId))
385
+ .collect();
386
+ },
387
+ });
388
+
389
+ export const organizationUpdate = mutation({
390
+ args: { organizationId: v.id("organization"), data: v.any() },
391
+ handler: async (ctx, { organizationId, data }) => {
392
+ await (ctx.db as any).patch(organizationId, data);
393
+ },
394
+ });
395
+
396
+ export const organizationDelete = mutation({
397
+ args: { organizationId: v.id("organization") },
398
+ handler: async (ctx, { organizationId }) => {
399
+ await (ctx.db as any).delete(organizationId);
400
+ },
401
+ });
402
+
403
+ // Team
404
+ export const teamCreate = mutation({
405
+ args: {
406
+ organizationId: v.id("organization"),
407
+ name: v.string(),
408
+ slug: v.optional(v.string()),
409
+ parentTeamId: v.optional(v.id("team")),
410
+ metadata: v.optional(v.any()),
411
+ },
412
+ handler: async (ctx, args) => {
413
+ return await (ctx.db as any).insert("team", args);
414
+ },
415
+ });
416
+
417
+ export const teamGet = query({
418
+ args: { teamId: v.id("team") },
419
+ handler: async (ctx, { teamId }) => {
420
+ return await (ctx.db as any).get(teamId);
421
+ },
422
+ });
423
+
424
+ export const teamListByOrganization = query({
425
+ args: { organizationId: v.id("organization") },
426
+ handler: async (ctx, { organizationId }) => {
427
+ return await (ctx.db as any)
428
+ .query("team")
429
+ .withIndex("organizationId", (q: any) =>
430
+ q.eq("organizationId", organizationId),
431
+ )
432
+ .collect();
433
+ },
434
+ });
435
+
436
+ export const teamUpdate = mutation({
437
+ args: { teamId: v.id("team"), data: v.any() },
438
+ handler: async (ctx, { teamId, data }) => {
439
+ await (ctx.db as any).patch(teamId, data);
440
+ },
441
+ });
442
+
443
+ export const teamDelete = mutation({
444
+ args: { teamId: v.id("team") },
445
+ handler: async (ctx, { teamId }) => {
446
+ await (ctx.db as any).delete(teamId);
447
+ },
448
+ });
449
+
450
+ // Team relations
451
+ export const teamRelationCreate = mutation({
452
+ args: {
453
+ organizationId: v.id("organization"),
454
+ parentTeamId: v.id("team"),
455
+ childTeamId: v.id("team"),
456
+ relation: v.optional(v.string()),
457
+ },
458
+ handler: async (ctx, args) => {
459
+ return await (ctx.db as any).insert("teamRelation", args);
460
+ },
461
+ });
462
+
463
+ export const teamRelationGet = query({
464
+ args: { teamRelationId: v.id("teamRelation") },
465
+ handler: async (ctx, { teamRelationId }) => {
466
+ return await (ctx.db as any).get(teamRelationId);
467
+ },
468
+ });
469
+
470
+ export const teamRelationListByParent = query({
471
+ args: {
472
+ organizationId: v.id("organization"),
473
+ parentTeamId: v.id("team"),
474
+ },
475
+ handler: async (ctx, { organizationId, parentTeamId }) => {
476
+ return await (ctx.db as any)
477
+ .query("teamRelation")
478
+ .withIndex("organizationIdAndParentTeamId", (q: any) =>
479
+ q.eq("organizationId", organizationId).eq("parentTeamId", parentTeamId),
480
+ )
481
+ .collect();
482
+ },
483
+ });
484
+
485
+ export const teamRelationDelete = mutation({
486
+ args: { teamRelationId: v.id("teamRelation") },
487
+ handler: async (ctx, { teamRelationId }) => {
488
+ await (ctx.db as any).delete(teamRelationId);
489
+ },
490
+ });
491
+
492
+ // Members
493
+ export const memberAdd = mutation({
494
+ args: { data: v.any() },
495
+ handler: async (ctx, { data }) => {
496
+ return await (ctx.db as any).insert("member", data);
497
+ },
498
+ });
499
+
500
+ export const memberRemove = mutation({
501
+ args: { memberId: v.id("member") },
502
+ handler: async (ctx, { memberId }) => {
503
+ await (ctx.db as any).delete(memberId);
504
+ },
505
+ });
506
+
507
+ export const memberList = query({
508
+ args: {
509
+ organizationId: v.id("organization"),
510
+ teamId: v.optional(v.id("team")),
511
+ },
512
+ handler: async (ctx, { organizationId, teamId }) => {
513
+ if (teamId !== undefined) {
514
+ return await (ctx.db as any)
515
+ .query("member")
516
+ .withIndex("teamId", (q: any) => q.eq("teamId", teamId))
517
+ .collect();
518
+ }
519
+ return await (ctx.db as any)
520
+ .query("member")
521
+ .withIndex("organizationId", (q: any) =>
522
+ q.eq("organizationId", organizationId),
523
+ )
524
+ .collect();
525
+ },
526
+ });
527
+
528
+ export const memberRoleSet = mutation({
529
+ args: { memberId: v.id("member"), role: v.string() },
530
+ handler: async (ctx, { memberId, role }) => {
531
+ await (ctx.db as any).patch(memberId, { role });
532
+ },
533
+ });
534
+
535
+ export const memberRoleGet = query({
536
+ args: { memberId: v.id("member") },
537
+ handler: async (ctx, { memberId }) => {
538
+ const member = await (ctx.db as any).get(memberId);
539
+ return member?.role ?? null;
540
+ },
541
+ });
542
+
543
+ // Invites
544
+ export const inviteCreate = mutation({
545
+ args: { data: v.any() },
546
+ handler: async (ctx, { data }) => {
547
+ return await (ctx.db as any).insert("invite", data);
548
+ },
549
+ });
550
+
551
+ export const inviteGet = query({
552
+ args: { inviteId: v.id("invite") },
553
+ handler: async (ctx, { inviteId }) => {
554
+ return await (ctx.db as any).get(inviteId);
555
+ },
556
+ });
557
+
558
+ export const inviteList = query({
559
+ args: {
560
+ organizationId: v.optional(v.id("organization")),
561
+ status: v.optional(v.string()),
562
+ },
563
+ handler: async (ctx, { organizationId, status }) => {
564
+ if (organizationId !== undefined && status !== undefined) {
565
+ return await (ctx.db as any)
566
+ .query("invite")
567
+ .withIndex("organizationIdAndStatus", (q: any) =>
568
+ q.eq("organizationId", organizationId).eq("status", status),
569
+ )
570
+ .collect();
571
+ }
572
+ if (organizationId !== undefined) {
573
+ return await (ctx.db as any)
574
+ .query("invite")
575
+ .withIndex("organizationId", (q: any) =>
576
+ q.eq("organizationId", organizationId),
577
+ )
578
+ .collect();
579
+ }
580
+ if (status !== undefined) {
581
+ return await (ctx.db as any)
582
+ .query("invite")
583
+ .filter((q: any) => q.eq(q.field("status"), status))
584
+ .collect();
585
+ }
586
+ return await (ctx.db as any)
587
+ .query("invite")
588
+ .collect();
589
+ },
590
+ });
591
+
592
+ export const inviteAccept = mutation({
593
+ args: { inviteId: v.id("invite") },
594
+ handler: async (ctx, { inviteId }) => {
595
+ await (ctx.db as any).patch(inviteId, {
596
+ status: "accepted",
597
+ acceptedTime: Date.now(),
598
+ });
599
+ },
600
+ });
601
+
602
+ export const inviteRevoke = mutation({
603
+ args: { inviteId: v.id("invite") },
604
+ handler: async (ctx, { inviteId }) => {
605
+ await (ctx.db as any).patch(inviteId, { status: "revoked" });
606
+ },
607
+ });
@@ -0,0 +1,35 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ import { authTables } from "../server/implementation/types.js";
4
+
5
+ const {
6
+ user,
7
+ account,
8
+ session,
9
+ token,
10
+ verification,
11
+ verifier,
12
+ limit,
13
+ } = authTables;
14
+ void user;
15
+
16
+ export default defineSchema({
17
+ user: defineTable({
18
+ name: v.optional(v.string()),
19
+ image: v.optional(v.string()),
20
+ email: v.optional(v.string()),
21
+ emailVerificationTime: v.optional(v.number()),
22
+ phone: v.optional(v.string()),
23
+ phoneVerificationTime: v.optional(v.number()),
24
+ isAnonymous: v.optional(v.boolean()),
25
+ favoriteColor: v.optional(v.string()),
26
+ })
27
+ .index("email", ["email"])
28
+ .index("phone", ["phone"]),
29
+ account,
30
+ session,
31
+ token,
32
+ verification,
33
+ verifier,
34
+ limit,
35
+ });
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Configure {@link Anonymous} provider given an {@link AnonymousConfig}.
3
+ *
4
+ * ```ts
5
+ * import { Anonymous } from "@robelest/convex-auth/providers/Anonymous";
6
+ * import { convexAuth } from "@robelest/convex-auth/component";
7
+ *
8
+ * export const { auth, signIn, signOut, store } = convexAuth({
9
+ * providers: [Anonymous],
10
+ * });
11
+ * ```
12
+ *
13
+ * @module
14
+ */
15
+
16
+ import convexCredentials from "@robelest/convex-auth/providers/ConvexCredentials";
17
+ import {
18
+ GenericActionCtxWithAuthConfig,
19
+ createAccount,
20
+ } from "@robelest/convex-auth/component";
21
+ import {
22
+ DocumentByName,
23
+ GenericDataModel,
24
+ WithoutSystemFields,
25
+ } from "convex/server";
26
+ import { Value } from "convex/values";
27
+
28
+ /**
29
+ * The available options to an {@link Anonymous} provider for Convex Auth.
30
+ */
31
+ export interface AnonymousConfig<DataModel extends GenericDataModel> {
32
+ /**
33
+ * Uniquely identifies the provider, allowing to use
34
+ * multiple different {@link Anonymous} providers.
35
+ */
36
+ id?: string;
37
+ /**
38
+ * Perform checks on provided params and customize the user
39
+ * information stored after sign in.
40
+ */
41
+ profile?: (
42
+ /**
43
+ * The values passed to the `signIn` function.
44
+ */
45
+ params: Record<string, Value | undefined>,
46
+ /**
47
+ * Convex ActionCtx in case you want to read from or write to
48
+ * the database.
49
+ */
50
+ ctx: GenericActionCtxWithAuthConfig<DataModel>,
51
+ ) => WithoutSystemFields<DocumentByName<DataModel, "user">> & {
52
+ isAnonymous: true;
53
+ };
54
+ }
55
+
56
+ /**
57
+ * An anonymous authentication provider.
58
+ *
59
+ * This provider doesn't require any user-provided information.
60
+ */
61
+ export default function anonymous<DataModel extends GenericDataModel>(
62
+ config: AnonymousConfig<DataModel> = {},
63
+ ) {
64
+ const provider = config.id ?? "anonymous";
65
+ return convexCredentials<DataModel>({
66
+ id: "anonymous",
67
+ authorize: async (params, ctx) => {
68
+ const profile = config.profile?.(params, ctx) ?? { isAnonymous: true };
69
+ const { user } = await createAccount(ctx, {
70
+ provider,
71
+ account: { id: crypto.randomUUID() },
72
+ profile: profile as any,
73
+ });
74
+ // END
75
+ return { userId: user._id };
76
+ },
77
+ ...config,
78
+ });
79
+ }