nuxt-backend 0.1.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.
Files changed (106) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +744 -0
  3. package/dist/convex/auth.config.d.ts +9 -0
  4. package/dist/convex/auth.config.d.ts.map +1 -0
  5. package/dist/convex/auth.config.js +14 -0
  6. package/dist/convex/auth.config.js.map +1 -0
  7. package/dist/convex/client/index.d.ts +12784 -0
  8. package/dist/convex/client/index.d.ts.map +1 -0
  9. package/dist/convex/client/index.js +230 -0
  10. package/dist/convex/client/index.js.map +1 -0
  11. package/dist/convex/component/_generated/api.d.ts +36 -0
  12. package/dist/convex/component/_generated/api.d.ts.map +1 -0
  13. package/dist/convex/component/_generated/api.js +31 -0
  14. package/dist/convex/component/_generated/api.js.map +1 -0
  15. package/dist/convex/component/_generated/component.d.ts +549 -0
  16. package/dist/convex/component/_generated/component.d.ts.map +1 -0
  17. package/dist/convex/component/_generated/component.js +11 -0
  18. package/dist/convex/component/_generated/component.js.map +1 -0
  19. package/dist/convex/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/convex/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/convex/component/_generated/dataModel.js +11 -0
  22. package/dist/convex/component/_generated/dataModel.js.map +1 -0
  23. package/dist/convex/component/_generated/server.d.ts +121 -0
  24. package/dist/convex/component/_generated/server.d.ts.map +1 -0
  25. package/dist/convex/component/_generated/server.js +78 -0
  26. package/dist/convex/component/_generated/server.js.map +1 -0
  27. package/dist/convex/component/adapter.d.ts +3 -0
  28. package/dist/convex/component/adapter.d.ts.map +1 -0
  29. package/dist/convex/component/adapter.js +23 -0
  30. package/dist/convex/component/adapter.js.map +1 -0
  31. package/dist/convex/component/constants.d.ts +3 -0
  32. package/dist/convex/component/constants.d.ts.map +1 -0
  33. package/dist/convex/component/constants.js +3 -0
  34. package/dist/convex/component/constants.js.map +1 -0
  35. package/dist/convex/component/convex.config.d.ts +14 -0
  36. package/dist/convex/component/convex.config.d.ts.map +1 -0
  37. package/dist/convex/component/convex.config.js +15 -0
  38. package/dist/convex/component/convex.config.js.map +1 -0
  39. package/dist/convex/component/schema.d.ts +278 -0
  40. package/dist/convex/component/schema.d.ts.map +1 -0
  41. package/dist/convex/component/schema.js +91 -0
  42. package/dist/convex/component/schema.js.map +1 -0
  43. package/dist/module.d.ts +47 -0
  44. package/dist/module.js +275 -0
  45. package/dist/module.json +9 -0
  46. package/dist/runtime/nuxt/auth/middleware.d.ts +12 -0
  47. package/dist/runtime/nuxt/auth/middleware.js +31 -0
  48. package/dist/runtime/nuxt/auth/proxy.d.ts +6 -0
  49. package/dist/runtime/nuxt/auth/proxy.js +11 -0
  50. package/dist/runtime/nuxt/auth/server.d.ts +86 -0
  51. package/dist/runtime/nuxt/auth/server.js +110 -0
  52. package/dist/runtime/nuxt/config.d.ts +7 -0
  53. package/dist/runtime/nuxt/config.js +12 -0
  54. package/dist/runtime/nuxt/index.d.ts +102 -0
  55. package/dist/runtime/nuxt/index.js +77 -0
  56. package/dist/runtime/vue/auth/client.d.ts +14 -0
  57. package/dist/runtime/vue/auth/client.js +7 -0
  58. package/dist/runtime/vue/auth/cross-domain.d.ts +8 -0
  59. package/dist/runtime/vue/auth/cross-domain.js +34 -0
  60. package/dist/runtime/vue/auth/helpers.d.ts +52 -0
  61. package/dist/runtime/vue/auth/helpers.js +35 -0
  62. package/dist/runtime/vue/auth/hydration.d.ts +14 -0
  63. package/dist/runtime/vue/auth/hydration.js +23 -0
  64. package/dist/runtime/vue/auth/index.d.ts +79 -0
  65. package/dist/runtime/vue/auth/index.js +81 -0
  66. package/dist/runtime/vue/auth/plugin.client.d.ts +6 -0
  67. package/dist/runtime/vue/auth/plugin.client.js +30 -0
  68. package/dist/runtime/vue/auth/plugin.server.d.ts +17 -0
  69. package/dist/runtime/vue/auth/plugin.server.js +29 -0
  70. package/dist/runtime/vue/auth/use-auth.d.ts +84 -0
  71. package/dist/runtime/vue/auth/use-auth.js +77 -0
  72. package/dist/runtime/vue/client.d.ts +245 -0
  73. package/dist/runtime/vue/client.js +341 -0
  74. package/dist/runtime/vue/composables/use-action.d.ts +45 -0
  75. package/dist/runtime/vue/composables/use-action.js +10 -0
  76. package/dist/runtime/vue/composables/use-connection-state.d.ts +25 -0
  77. package/dist/runtime/vue/composables/use-connection-state.js +13 -0
  78. package/dist/runtime/vue/composables/use-mutation.d.ts +60 -0
  79. package/dist/runtime/vue/composables/use-mutation.js +36 -0
  80. package/dist/runtime/vue/composables/use-paginated-query.d.ts +101 -0
  81. package/dist/runtime/vue/composables/use-paginated-query.js +508 -0
  82. package/dist/runtime/vue/composables/use-queries.d.ts +45 -0
  83. package/dist/runtime/vue/composables/use-queries.js +32 -0
  84. package/dist/runtime/vue/composables/use-query.d.ts +82 -0
  85. package/dist/runtime/vue/composables/use-query.js +63 -0
  86. package/dist/runtime/vue/composables/use-subscription.d.ts +14 -0
  87. package/dist/runtime/vue/composables/use-subscription.js +15 -0
  88. package/dist/runtime/vue/hydration.d.ts +42 -0
  89. package/dist/runtime/vue/hydration.js +18 -0
  90. package/dist/runtime/vue/index.d.ts +29 -0
  91. package/dist/runtime/vue/index.js +38 -0
  92. package/dist/runtime/vue/queries-observer.d.ts +38 -0
  93. package/dist/runtime/vue/queries-observer.js +127 -0
  94. package/dist/types.d.ts +3 -0
  95. package/package.json +157 -0
  96. package/src/convex/auth.config.ts +21 -0
  97. package/src/convex/client/index.ts +329 -0
  98. package/src/convex/component/_generated/api.ts +52 -0
  99. package/src/convex/component/_generated/component.ts +1338 -0
  100. package/src/convex/component/_generated/dataModel.ts +60 -0
  101. package/src/convex/component/_generated/server.ts +156 -0
  102. package/src/convex/component/adapter.ts +42 -0
  103. package/src/convex/component/constants.ts +2 -0
  104. package/src/convex/component/convex.config.ts +16 -0
  105. package/src/convex/component/schema.ts +93 -0
  106. package/src/convex/test.ts +34 -0
@@ -0,0 +1,329 @@
1
+ import { createClient, type GenericCtx } from '@convex-dev/better-auth'
2
+ import { convex } from '@convex-dev/better-auth/plugins'
3
+ import { passkey } from '@better-auth/passkey'
4
+ import { APIError, getSessionFromCtx } from 'better-auth/api'
5
+ import { setSessionCookie } from 'better-auth/cookies'
6
+ import { betterAuth, type BetterAuthOptions } from 'better-auth/minimal'
7
+ import { emailOTP } from 'better-auth/plugins'
8
+ import type { AnyComponents, AuthConfig, GenericDataModel, GenericSchema, QueryBuilder, SchemaDefinition } from 'convex/server'
9
+ import authConfig from '../auth.config'
10
+ import { DEFAULT_AUTH_ROUTE } from '../component/constants'
11
+ import { authSchema } from '../component/schema'
12
+
13
+ /**
14
+ * Client-supplied details for passkey-first registration.
15
+ *
16
+ * The Better Auth passkey endpoint forwards this as an opaque `context`
17
+ * string. We default to a small JSON shape: `{ email, name }`.
18
+ */
19
+ interface PasskeyRegistrationContext {
20
+ email: string
21
+ name: string
22
+ }
23
+
24
+ function parsePasskeyContext(context?: string | null): PasskeyRegistrationContext {
25
+ if (!context) {
26
+ throw APIError.from('BAD_REQUEST', {
27
+ code: 'INVALID_PASSKEY_REGISTRATION',
28
+ message: 'Passkey registration details are missing.',
29
+ })
30
+ }
31
+ let parsed: unknown
32
+ try {
33
+ parsed = JSON.parse(context)
34
+ }
35
+ catch {
36
+ throw APIError.from('BAD_REQUEST', {
37
+ code: 'INVALID_PASSKEY_REGISTRATION',
38
+ message: 'Passkey registration details are invalid.',
39
+ })
40
+ }
41
+ const { email, name } = (parsed ?? {}) as Partial<PasskeyRegistrationContext>
42
+ const normalizedEmail = typeof email === 'string' ? email.trim().toLowerCase() : ''
43
+ const normalizedName = typeof name === 'string' ? name.trim() : ''
44
+ if (!normalizedEmail || !normalizedEmail.includes('@')) {
45
+ throw APIError.from('BAD_REQUEST', {
46
+ code: 'INVALID_PASSKEY_REGISTRATION',
47
+ message: 'Enter a valid email address.',
48
+ })
49
+ }
50
+ if (!normalizedName) {
51
+ throw APIError.from('BAD_REQUEST', {
52
+ code: 'INVALID_PASSKEY_REGISTRATION',
53
+ message: 'Enter your name to register a passkey.',
54
+ })
55
+ }
56
+ return { email: normalizedEmail, name: normalizedName }
57
+ }
58
+
59
+ function accountExistsError() {
60
+ return APIError.from('UNPROCESSABLE_ENTITY', {
61
+ code: 'PASSKEY_REGISTRATION_ACCOUNT_EXISTS',
62
+ message: 'An account already exists for this email. Sign in first, then add a passkey.',
63
+ })
64
+ }
65
+
66
+ /**
67
+ * Default passkey plugin: enables passkey-first (pre-auth) registration so
68
+ * users can create an account with just a passkey. When unauthenticated, the
69
+ * client passes `{ email, name }` as the `context` argument when calling
70
+ * `addPasskey`. When the user already has a session, the passkey is attached
71
+ * to the current user and `context` is ignored.
72
+ *
73
+ * Override by passing your own `passkey(...)` plugin in `authOptions.plugins`.
74
+ */
75
+ function defaultPasskey() {
76
+ return passkey({
77
+ registration: {
78
+ requireSession: false,
79
+ resolveUser: async ({ ctx, context }) => {
80
+ const { email, name } = parsePasskeyContext(context)
81
+ const existing = await ctx.context.internalAdapter.findUserByEmail(email)
82
+ if (existing) throw accountExistsError()
83
+ return { id: `passkey-registration:${email}`, name: email, displayName: name }
84
+ },
85
+ afterVerification: async ({ ctx, context }) => {
86
+ // If a session already exists, this is an "add passkey to current
87
+ // user" flow — the plugin will associate the new passkey with the
88
+ // session user, so we have nothing to do here.
89
+ const existingSession = await getSessionFromCtx(ctx)
90
+ if (existingSession?.user?.id) return
91
+
92
+ const { email, name } = parsePasskeyContext(context)
93
+ const existing = await ctx.context.internalAdapter.findUserByEmail(email)
94
+ if (existing) throw accountExistsError()
95
+ const user = await ctx.context.internalAdapter.createUser({
96
+ email,
97
+ emailVerified: false,
98
+ name,
99
+ })
100
+ const session = await ctx.context.internalAdapter.createSession(user.id)
101
+ await setSessionCookie(ctx, { session, user })
102
+ return { userId: user.id }
103
+ },
104
+ },
105
+ })
106
+ }
107
+
108
+ type DefaultAuthSchema = typeof authSchema
109
+
110
+ export interface CreateBetterAuthOptions {
111
+ /** Override the default auth config (e.g. to add custom providers) */
112
+ authConfig?: AuthConfig
113
+ /** Override Better Auth options (merged with defaults) */
114
+ authOptions?: BetterAuthOptions
115
+ /** Override Better Auth basePath and matching Convex auth route */
116
+ basePath?: string
117
+ }
118
+
119
+ export interface SetupAuthOptions<Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema> extends CreateBetterAuthOptions {
120
+ /** Local Better Auth schema for hybrid/local component installs */
121
+ schema?: Schema
122
+ /** Enable verbose logs in the Better Auth Convex component client */
123
+ verbose?: boolean
124
+ }
125
+
126
+ /** Component API shape expected by the Better Auth client. */
127
+ type AuthComponentApi = Parameters<typeof createClient>[0]
128
+ type PublicAuthComponentRef = AuthComponentApi | AnyComponents[string]
129
+
130
+ type EnvHost = typeof globalThis & {
131
+ process?: {
132
+ env?: Record<string, string | undefined>
133
+ }
134
+ }
135
+
136
+ function toAuthComponentApi(componentRef: PublicAuthComponentRef): AuthComponentApi {
137
+ return componentRef as AuthComponentApi
138
+ }
139
+
140
+ function readEnv(name: string) {
141
+ return (globalThis as EnvHost).process?.env?.[name]
142
+ }
143
+
144
+ function createAuthComponent<DM extends GenericDataModel, Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema>(
145
+ componentRef: PublicAuthComponentRef,
146
+ options?: SetupAuthOptions<Schema>,
147
+ ) {
148
+ const schema = options?.schema ?? authSchema
149
+ const config = {
150
+ local: { schema },
151
+ ...(options?.verbose !== undefined ? { verbose: options.verbose } : {}),
152
+ }
153
+
154
+ return createClient<DM, Schema>(
155
+ toAuthComponentApi(componentRef),
156
+ config as never,
157
+ )
158
+ }
159
+
160
+ export function createBetterAuthOptions(
161
+ database: ReturnType<ReturnType<typeof createClient>['adapter']>,
162
+ options: CreateBetterAuthOptions = {},
163
+ ) {
164
+ const resolvedAuthConfig = options.authConfig ?? authConfig
165
+ const resolvedAuthOptions = options.authOptions ?? {}
166
+ const resolvedBasePath = options.basePath ?? resolvedAuthOptions.basePath ?? DEFAULT_AUTH_ROUTE
167
+ const siteUrl = resolvedAuthOptions.baseURL ?? readEnv('SITE_URL') ?? readEnv('BETTER_AUTH_URL')
168
+ const secret = resolvedAuthOptions.secret ?? readEnv('BETTER_AUTH_SECRET')
169
+
170
+ const userPlugins = resolvedAuthOptions.plugins ?? []
171
+ const userPluginIds = new Set(userPlugins.map(p => p.id))
172
+ const defaultPlugins = [
173
+ emailOTP({
174
+ async sendVerificationOTP({ email, otp, type }) {
175
+ console.warn(
176
+ `[nuxt-backend] No sendVerificationOTP configured. Email OTP (${type}) for ${email}: ${otp}`,
177
+ )
178
+ },
179
+ }),
180
+ defaultPasskey(),
181
+ ].filter(plugin => !userPluginIds.has(plugin.id))
182
+
183
+ return {
184
+ ...resolvedAuthOptions,
185
+ ...(siteUrl ? { baseURL: siteUrl } : {}),
186
+ ...(secret ? { secret } : {}),
187
+ basePath: resolvedBasePath,
188
+ database,
189
+ emailAndPassword: {
190
+ enabled: false,
191
+ requireEmailVerification: false,
192
+ ...resolvedAuthOptions.emailAndPassword,
193
+ },
194
+ plugins: [
195
+ convex({
196
+ authConfig: resolvedAuthConfig,
197
+ options: {
198
+ basePath: resolvedBasePath,
199
+ },
200
+ }),
201
+ ...defaultPlugins,
202
+ ...userPlugins,
203
+ ],
204
+ } satisfies BetterAuthOptions
205
+ }
206
+
207
+ /**
208
+ * Shared factory that creates a Better Auth instance from a database adapter.
209
+ *
210
+ * Used by the public client bridge and by the component HTTP router so the
211
+ * packaged component and app-facing wrapper stay aligned.
212
+ */
213
+ export function createBetterAuth(
214
+ database: ReturnType<ReturnType<typeof createClient>['adapter']>,
215
+ options: CreateBetterAuthOptions = {},
216
+ ) {
217
+ return betterAuth(createBetterAuthOptions(database, options))
218
+ }
219
+
220
+ export function createAuthOptions<
221
+ DM extends GenericDataModel,
222
+ Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema,
223
+ >(
224
+ ctx: GenericCtx<DM>,
225
+ componentRef: PublicAuthComponentRef,
226
+ options?: SetupAuthOptions<Schema>,
227
+ ) {
228
+ const authComponent = createAuthComponent<DM, Schema>(componentRef, options)
229
+ return createBetterAuthOptions(authComponent.adapter(ctx), {
230
+ authConfig: options?.authConfig,
231
+ authOptions: options?.authOptions,
232
+ basePath: options?.basePath,
233
+ })
234
+ }
235
+
236
+ /**
237
+ * Simple wrapper around the packaged auth component that runs in the app
238
+ * environment.
239
+ *
240
+ * This follows Convex's simple function wrapper pattern: app code can pass the
241
+ * component reference and Convex context in directly, while this helper handles
242
+ * the cross-boundary adapter wiring and environment-backed auth creation.
243
+ */
244
+ export function createAuth<
245
+ DM extends GenericDataModel,
246
+ Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema,
247
+ >(
248
+ ctx: GenericCtx<DM>,
249
+ componentRef: PublicAuthComponentRef,
250
+ options?: SetupAuthOptions<Schema>,
251
+ ) {
252
+ return betterAuth(createAuthOptions(ctx, componentRef, options))
253
+ }
254
+
255
+ /**
256
+ * Ready-made app query wrappers for re-exporting component functionality.
257
+ *
258
+ * This follows Convex's API remounting pattern for component client code.
259
+ */
260
+ export function makeAuthApi<
261
+ DM extends GenericDataModel,
262
+ Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema,
263
+ >(
264
+ componentRef: PublicAuthComponentRef,
265
+ queryBuilder: QueryBuilder<DM, 'public'>,
266
+ options?: SetupAuthOptions<Schema>,
267
+ ) {
268
+ const authComponent = createAuthComponent<DM, Schema>(componentRef, options)
269
+
270
+ const getAuthUser = queryBuilder({
271
+ args: {},
272
+ handler: async (ctx) => {
273
+ return authComponent.getAuthUser(ctx)
274
+ },
275
+ })
276
+
277
+ return {
278
+ getAuthUser,
279
+ }
280
+ }
281
+
282
+ /**
283
+ * App-facing client bridge for the packaged Convex component.
284
+ *
285
+ * This convenience helper composes the simple wrapper and API remounting
286
+ * patterns exported from this module.
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * import { setupAuth } from 'nuxt-backend/convex'
291
+ * import { components } from './_generated/api'
292
+ * import { query } from './_generated/server'
293
+ *
294
+ * export const { authComponent, createAuth, getAuthUser } = setupAuth(
295
+ * components.backend, query,
296
+ * )
297
+ * ```
298
+ */
299
+ export function setupAuth<
300
+ DM extends GenericDataModel,
301
+ Schema extends SchemaDefinition<GenericSchema, true> = DefaultAuthSchema,
302
+ >(
303
+ componentRef: PublicAuthComponentRef,
304
+ queryBuilder: QueryBuilder<DM, 'public'>,
305
+ options?: SetupAuthOptions<Schema>,
306
+ ) {
307
+ const authComponent = createAuthComponent<DM, Schema>(componentRef, options)
308
+ const { getAuthUser } = makeAuthApi(componentRef, queryBuilder, options)
309
+
310
+ const createAuthOptionsForContext = (ctx: GenericCtx<DM>) => {
311
+ return createBetterAuthOptions(authComponent.adapter(ctx), {
312
+ authConfig: options?.authConfig,
313
+ authOptions: options?.authOptions,
314
+ basePath: options?.basePath,
315
+ })
316
+ }
317
+
318
+ const createAuthForContext = (ctx: GenericCtx<DM>) => {
319
+ return betterAuth(createAuthOptionsForContext(ctx))
320
+ }
321
+
322
+ return {
323
+ authComponent,
324
+ createAuthOptions: createAuthOptionsForContext,
325
+ options: createAuthOptionsForContext({} as GenericCtx<DM>),
326
+ createAuth: createAuthForContext,
327
+ getAuthUser,
328
+ }
329
+ }
@@ -0,0 +1,52 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated `api` utility.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type * as adapter from "../adapter.js";
12
+ import type * as constants from "../constants.js";
13
+
14
+ import type {
15
+ ApiFromModules,
16
+ FilterApi,
17
+ FunctionReference,
18
+ } from "convex/server";
19
+ import { anyApi, componentsGeneric } from "convex/server";
20
+
21
+ const fullApi: ApiFromModules<{
22
+ adapter: typeof adapter;
23
+ constants: typeof constants;
24
+ }> = anyApi as any;
25
+
26
+ /**
27
+ * A utility for referencing Convex functions in your app's public API.
28
+ *
29
+ * Usage:
30
+ * ```js
31
+ * const myFunctionReference = api.myModule.myFunction;
32
+ * ```
33
+ */
34
+ export const api: FilterApi<
35
+ typeof fullApi,
36
+ FunctionReference<any, "public">
37
+ > = anyApi as any;
38
+
39
+ /**
40
+ * A utility for referencing Convex functions in your app's internal API.
41
+ *
42
+ * Usage:
43
+ * ```js
44
+ * const myFunctionReference = internal.myModule.myFunction;
45
+ * ```
46
+ */
47
+ export const internal: FilterApi<
48
+ typeof fullApi,
49
+ FunctionReference<any, "internal">
50
+ > = anyApi as any;
51
+
52
+ export const components = componentsGeneric() as unknown as {};