@neutralauth/internal-auth 0.10.11

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 (147) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +39 -0
  3. package/dist/auth-config.d.ts +43 -0
  4. package/dist/auth-config.d.ts.map +1 -0
  5. package/dist/auth-config.js +43 -0
  6. package/dist/auth-config.js.map +1 -0
  7. package/dist/auth-options.d.ts +3 -0
  8. package/dist/auth-options.d.ts.map +1 -0
  9. package/dist/auth-options.js +40 -0
  10. package/dist/auth-options.js.map +1 -0
  11. package/dist/auth.d.ts +2 -0
  12. package/dist/auth.d.ts.map +1 -0
  13. package/dist/auth.js +4 -0
  14. package/dist/auth.js.map +1 -0
  15. package/dist/client/adapter-utils.d.ts +66 -0
  16. package/dist/client/adapter-utils.d.ts.map +1 -0
  17. package/dist/client/adapter-utils.js +437 -0
  18. package/dist/client/adapter-utils.js.map +1 -0
  19. package/dist/client/adapter.d.ts +14 -0
  20. package/dist/client/adapter.d.ts.map +1 -0
  21. package/dist/client/adapter.js +274 -0
  22. package/dist/client/adapter.js.map +1 -0
  23. package/dist/client/create-api.d.ts +141 -0
  24. package/dist/client/create-api.d.ts.map +1 -0
  25. package/dist/client/create-api.js +205 -0
  26. package/dist/client/create-api.js.map +1 -0
  27. package/dist/client/create-client.d.ts +183 -0
  28. package/dist/client/create-client.d.ts.map +1 -0
  29. package/dist/client/create-client.js +311 -0
  30. package/dist/client/create-client.js.map +1 -0
  31. package/dist/client/create-schema.d.ts +19 -0
  32. package/dist/client/create-schema.d.ts.map +1 -0
  33. package/dist/client/create-schema.js +114 -0
  34. package/dist/client/create-schema.js.map +1 -0
  35. package/dist/client/index.d.ts +7 -0
  36. package/dist/client/index.d.ts.map +1 -0
  37. package/dist/client/index.js +10 -0
  38. package/dist/client/index.js.map +1 -0
  39. package/dist/client/plugins/index.d.ts +3 -0
  40. package/dist/client/plugins/index.d.ts.map +1 -0
  41. package/dist/client/plugins/index.js +3 -0
  42. package/dist/client/plugins/index.js.map +1 -0
  43. package/dist/component/_generated/api.d.ts +36 -0
  44. package/dist/component/_generated/api.d.ts.map +1 -0
  45. package/dist/component/_generated/api.js +31 -0
  46. package/dist/component/_generated/api.js.map +1 -0
  47. package/dist/component/_generated/component.d.ts +787 -0
  48. package/dist/component/_generated/component.d.ts.map +1 -0
  49. package/dist/component/_generated/component.js +11 -0
  50. package/dist/component/_generated/component.js.map +1 -0
  51. package/dist/component/_generated/dataModel.d.ts +46 -0
  52. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  53. package/dist/component/_generated/dataModel.js +11 -0
  54. package/dist/component/_generated/dataModel.js.map +1 -0
  55. package/dist/component/_generated/server.d.ts +121 -0
  56. package/dist/component/_generated/server.d.ts.map +1 -0
  57. package/dist/component/_generated/server.js +78 -0
  58. package/dist/component/_generated/server.js.map +1 -0
  59. package/dist/component/adapter.d.ts +130 -0
  60. package/dist/component/adapter.d.ts.map +1 -0
  61. package/dist/component/adapter.js +5 -0
  62. package/dist/component/adapter.js.map +1 -0
  63. package/dist/component/adapterTest.d.ts +10 -0
  64. package/dist/component/adapterTest.d.ts.map +1 -0
  65. package/dist/component/adapterTest.js +409 -0
  66. package/dist/component/adapterTest.js.map +1 -0
  67. package/dist/component/convex.config.d.ts +3 -0
  68. package/dist/component/convex.config.d.ts.map +1 -0
  69. package/dist/component/convex.config.js +4 -0
  70. package/dist/component/convex.config.js.map +1 -0
  71. package/dist/component/schema.d.ts +474 -0
  72. package/dist/component/schema.d.ts.map +1 -0
  73. package/dist/component/schema.js +139 -0
  74. package/dist/component/schema.js.map +1 -0
  75. package/dist/nextjs/client.d.ts +4 -0
  76. package/dist/nextjs/client.d.ts.map +1 -0
  77. package/dist/nextjs/client.js +37 -0
  78. package/dist/nextjs/client.js.map +1 -0
  79. package/dist/nextjs/index.d.ts +22 -0
  80. package/dist/nextjs/index.d.ts.map +1 -0
  81. package/dist/nextjs/index.js +98 -0
  82. package/dist/nextjs/index.js.map +1 -0
  83. package/dist/plugins/convex/client.d.ts +6 -0
  84. package/dist/plugins/convex/client.d.ts.map +1 -0
  85. package/dist/plugins/convex/client.js +7 -0
  86. package/dist/plugins/convex/client.js.map +1 -0
  87. package/dist/plugins/convex/index.d.ts +322 -0
  88. package/dist/plugins/convex/index.d.ts.map +1 -0
  89. package/dist/plugins/convex/index.js +422 -0
  90. package/dist/plugins/convex/index.js.map +1 -0
  91. package/dist/plugins/cross-domain/client.d.ts +132 -0
  92. package/dist/plugins/cross-domain/client.d.ts.map +1 -0
  93. package/dist/plugins/cross-domain/client.js +192 -0
  94. package/dist/plugins/cross-domain/client.js.map +1 -0
  95. package/dist/plugins/cross-domain/index.d.ts +51 -0
  96. package/dist/plugins/cross-domain/index.d.ts.map +1 -0
  97. package/dist/plugins/cross-domain/index.js +173 -0
  98. package/dist/plugins/cross-domain/index.js.map +1 -0
  99. package/dist/plugins/index.d.ts +3 -0
  100. package/dist/plugins/index.d.ts.map +1 -0
  101. package/dist/plugins/index.js +3 -0
  102. package/dist/plugins/index.js.map +1 -0
  103. package/dist/react/index.d.ts +80 -0
  104. package/dist/react/index.d.ts.map +1 -0
  105. package/dist/react/index.js +190 -0
  106. package/dist/react/index.js.map +1 -0
  107. package/dist/react-start/index.d.ts +13 -0
  108. package/dist/react-start/index.d.ts.map +1 -0
  109. package/dist/react-start/index.js +101 -0
  110. package/dist/react-start/index.js.map +1 -0
  111. package/dist/utils/index.d.ts +33 -0
  112. package/dist/utils/index.d.ts.map +1 -0
  113. package/dist/utils/index.js +91 -0
  114. package/dist/utils/index.js.map +1 -0
  115. package/package.json +208 -0
  116. package/src/auth-config.ts +80 -0
  117. package/src/auth-options.ts +54 -0
  118. package/src/auth.ts +4 -0
  119. package/src/client/adapter-utils.ts +639 -0
  120. package/src/client/adapter.test.ts +83 -0
  121. package/src/client/adapter.ts +363 -0
  122. package/src/client/create-api.ts +339 -0
  123. package/src/client/create-client.ts +452 -0
  124. package/src/client/create-schema.ts +166 -0
  125. package/src/client/index.ts +22 -0
  126. package/src/client/plugins/index.ts +2 -0
  127. package/src/component/_generated/api.ts +52 -0
  128. package/src/component/_generated/component.ts +2008 -0
  129. package/src/component/_generated/dataModel.ts +60 -0
  130. package/src/component/_generated/server.ts +161 -0
  131. package/src/component/adapter.ts +13 -0
  132. package/src/component/adapterTest.ts +505 -0
  133. package/src/component/convex.config.ts +5 -0
  134. package/src/component/schema.ts +142 -0
  135. package/src/nextjs/client.tsx +54 -0
  136. package/src/nextjs/index.ts +152 -0
  137. package/src/plugins/convex/client.ts +9 -0
  138. package/src/plugins/convex/index.ts +596 -0
  139. package/src/plugins/cross-domain/client.test.ts +217 -0
  140. package/src/plugins/cross-domain/client.ts +234 -0
  141. package/src/plugins/cross-domain/index.ts +199 -0
  142. package/src/plugins/index.ts +2 -0
  143. package/src/react/index.tsx +304 -0
  144. package/src/react-start/index.ts +153 -0
  145. package/src/react-start/vite-env.d.ts +2 -0
  146. package/src/test.ts +18 -0
  147. package/src/utils/index.ts +171 -0
@@ -0,0 +1,304 @@
1
+ import type { PropsWithChildren, ReactNode } from "react";
2
+ import { Component, useCallback, useEffect, useMemo, useState } from "react";
3
+ import type { AuthTokenFetcher } from "convex/browser";
4
+ import {
5
+ Authenticated,
6
+ ConvexProviderWithAuth,
7
+ useConvexAuth,
8
+ useQuery,
9
+ } from "convex/react";
10
+ import type { FunctionReference } from "convex/server";
11
+ import type { BetterAuthClientPlugin } from "better-auth";
12
+ import type { createAuthClient } from "better-auth/react";
13
+ import type {
14
+ convexClient,
15
+ crossDomainClient,
16
+ } from "../client/plugins/index.js";
17
+ import type { EmptyObject } from "convex-helpers";
18
+
19
+ type CrossDomainClient = ReturnType<typeof crossDomainClient>;
20
+ type ConvexClient = ReturnType<typeof convexClient>;
21
+ type PluginsWithCrossDomain = (
22
+ | CrossDomainClient
23
+ | ConvexClient
24
+ | BetterAuthClientPlugin
25
+ )[];
26
+ type PluginsWithoutCrossDomain = (ConvexClient | BetterAuthClientPlugin)[];
27
+ type AuthClientWithPlugins<
28
+ Plugins extends PluginsWithCrossDomain | PluginsWithoutCrossDomain,
29
+ > = ReturnType<
30
+ typeof createAuthClient<
31
+ BetterAuthClientPlugin & {
32
+ plugins: Plugins;
33
+ }
34
+ >
35
+ >;
36
+ export type AuthClient =
37
+ | AuthClientWithPlugins<PluginsWithCrossDomain>
38
+ | AuthClientWithPlugins<PluginsWithoutCrossDomain>;
39
+
40
+ // Until we can import from our own entry points (requires TypeScript 4.7),
41
+ // just describe the interface enough to help users pass the right type.
42
+ type IConvexReactClient = {
43
+ setAuth(fetchToken: AuthTokenFetcher): void;
44
+ clearAuth(): void;
45
+ };
46
+
47
+ /**
48
+ * A wrapper React component which provides a {@link react.ConvexReactClient}
49
+ * authenticated with Better Auth.
50
+ *
51
+ * @public
52
+ */
53
+ export function ConvexBetterAuthProvider({
54
+ children,
55
+ client,
56
+ authClient,
57
+ initialToken,
58
+ }: {
59
+ children: ReactNode;
60
+ client: IConvexReactClient;
61
+ authClient: AuthClient;
62
+ initialToken?: string | null;
63
+ }) {
64
+ const useBetterAuth = useUseAuthFromBetterAuth(authClient, initialToken);
65
+ useEffect(() => {
66
+ (async () => {
67
+ const url = new URL(window.location?.href);
68
+ const token = url.searchParams.get("ott");
69
+ if (token) {
70
+ const authClientWithCrossDomain =
71
+ authClient as AuthClientWithPlugins<PluginsWithCrossDomain>;
72
+ url.searchParams.delete("ott");
73
+ const result =
74
+ await authClientWithCrossDomain.crossDomain.oneTimeToken.verify({
75
+ token,
76
+ });
77
+ const session = result.data?.session;
78
+ if (session) {
79
+ await authClient.getSession({
80
+ fetchOptions: {
81
+ headers: {
82
+ Authorization: `Bearer ${session.token}`,
83
+ },
84
+ },
85
+ });
86
+ authClientWithCrossDomain.updateSession();
87
+ }
88
+ window.history.replaceState({}, "", url);
89
+ }
90
+ })();
91
+ }, [authClient]);
92
+ return (
93
+ <ConvexProviderWithAuth client={client} useAuth={useBetterAuth}>
94
+ <>{children}</>
95
+ </ConvexProviderWithAuth>
96
+ );
97
+ }
98
+
99
+ let initialTokenUsed = false;
100
+
101
+ function useUseAuthFromBetterAuth(
102
+ authClient: AuthClient,
103
+ initialToken?: string | null
104
+ ) {
105
+ const [cachedToken, setCachedToken] = useState<string | null>(
106
+ initialTokenUsed ? null : (initialToken ?? null)
107
+ );
108
+ useEffect(() => {
109
+ if (!initialTokenUsed) {
110
+ initialTokenUsed = true;
111
+ }
112
+ }, []);
113
+
114
+ return useMemo(
115
+ () =>
116
+ function useAuthFromBetterAuth() {
117
+ const { data: session, isPending: isSessionPending } =
118
+ authClient.useSession();
119
+ const sessionId = session?.session?.id;
120
+ useEffect(() => {
121
+ if (!session && !isSessionPending && cachedToken) {
122
+ setCachedToken(null);
123
+ }
124
+ }, [session, isSessionPending]);
125
+ const fetchAccessToken = useCallback(
126
+ async ({
127
+ forceRefreshToken = false,
128
+ }: { forceRefreshToken?: boolean } = {}) => {
129
+ if (cachedToken && !forceRefreshToken) {
130
+ return cachedToken;
131
+ }
132
+ try {
133
+ const { data } = await authClient.convex.token();
134
+ const token = data?.token || null;
135
+ setCachedToken(token);
136
+ return token;
137
+ } catch {
138
+ setCachedToken(null);
139
+ return null;
140
+ }
141
+ },
142
+ // Build a new fetchAccessToken to trigger setAuth() whenever the
143
+ // session changes.
144
+ // eslint-disable-next-line react-hooks/exhaustive-deps
145
+ [sessionId]
146
+ );
147
+ return useMemo(
148
+ () => ({
149
+ isLoading: isSessionPending && !cachedToken,
150
+ isAuthenticated: Boolean(session?.session) || cachedToken !== null,
151
+ fetchAccessToken,
152
+ }),
153
+ // eslint-disable-next-line react-hooks/exhaustive-deps
154
+ [isSessionPending, sessionId, fetchAccessToken, cachedToken]
155
+ );
156
+ },
157
+ [authClient]
158
+ );
159
+ }
160
+
161
+ interface ErrorBoundaryProps {
162
+ children: React.ReactNode;
163
+ onUnauth: () => void | Promise<void>;
164
+ renderFallback?: () => React.ReactNode;
165
+ isAuthError: (error: unknown) => boolean;
166
+ }
167
+ interface ErrorBoundaryState {
168
+ error?: unknown;
169
+ }
170
+
171
+ class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
172
+ constructor(props: ErrorBoundaryProps) {
173
+ super(props);
174
+ this.state = {};
175
+ }
176
+ static defaultProps: Partial<ErrorBoundaryProps> = {
177
+ renderFallback: () => null,
178
+ };
179
+ static getDerivedStateFromError(error: Error) {
180
+ return { error };
181
+ }
182
+ async componentDidCatch(error: Error) {
183
+ if (this.props.isAuthError(error)) {
184
+ await this.props.onUnauth();
185
+ }
186
+ }
187
+ render() {
188
+ if (this.state.error && this.props.isAuthError(this.state.error)) {
189
+ return this.props.renderFallback?.();
190
+ }
191
+ return this.props.children;
192
+ }
193
+ }
194
+
195
+ // Subscribe to the session validated user to keep this check reactive to
196
+ // actual user auth state at the provider level (rather than just jwt validity state).
197
+ const UserSubscription = ({
198
+ getAuthUserFn,
199
+ }: {
200
+ getAuthUserFn: FunctionReference<"query">;
201
+ }) => {
202
+ useQuery(getAuthUserFn);
203
+ return null;
204
+ };
205
+
206
+ /**
207
+ * _Experimental_
208
+ *
209
+ * A wrapper React component which provides error handling for auth related errors.
210
+ * This is typically used to redirect the user to the login page when they are
211
+ * unauthenticated, and does so reactively based on the getAuthUserFn query.
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * // convex/auth.ts
216
+ * export const { getAuthUser } = authComponent.clientApi();
217
+ *
218
+ * // auth-client.tsx
219
+ * import { AuthBoundary } from "@convex-dev/react";
220
+ * import { api } from '../../convex/_generated/api'
221
+ * import { isAuthError } from '../lib/utils'
222
+ *
223
+ * export const ClientAuthBoundary = ({ children }: PropsWithChildren) => {
224
+ * return (
225
+ * <AuthBoundary
226
+ * onUnauth={() => redirect("/sign-in")}
227
+ * authClient={authClient}
228
+ * getAuthUserFn={api.auth.getAuthUser}
229
+ * isAuthError={isAuthError}
230
+ * >
231
+ * <>{children}</>
232
+ * </AuthBoundary>
233
+ * )
234
+ * ```
235
+ * @param props.children - Children to render.
236
+ * @param props.onUnauth - Function to call when the user is
237
+ * unauthenticated. Typically a redirect to the login page.
238
+ * @param props.authClient - Better Auth authClient to use.
239
+ * @param props.renderFallback - Fallback component to render when the user is
240
+ * unauthenticated. Defaults to null. Generally not rendered as error handling
241
+ * is typically a redirect.
242
+ * @param props.getAuthUserFn - Reference to a Convex query that returns user.
243
+ * The component provides a query for this via `export const { getAuthUser } = authComponent.clientApi()`.
244
+ * @param props.isAuthError - Function to check if the error is auth related.
245
+ */
246
+ export const AuthBoundary = ({
247
+ children,
248
+ /**
249
+ * The function to call when the user is unauthenticated. Typically a redirect
250
+ * to the login page.
251
+ */
252
+ onUnauth,
253
+ /**
254
+ * The Better Auth authClient to use.
255
+ */
256
+ authClient,
257
+ /**
258
+ * The fallback to render when the user is unauthenticated. Defaults to null.
259
+ * Generally not rendered as error handling is typically a redirect.
260
+ */
261
+ renderFallback,
262
+ /**
263
+ * The function to call to get the auth user.
264
+ */
265
+ getAuthUserFn,
266
+ /**
267
+ * The function to call to check if the error is auth related.
268
+ */
269
+ isAuthError,
270
+ }: PropsWithChildren<{
271
+ onUnauth: () => void | Promise<void>;
272
+ authClient: AuthClient;
273
+ renderFallback?: () => React.ReactNode;
274
+ getAuthUserFn: FunctionReference<"query", "public", EmptyObject>;
275
+ isAuthError: (error: unknown) => boolean;
276
+ }>) => {
277
+ const { isAuthenticated, isLoading } = useConvexAuth();
278
+ const handleUnauth = useCallback(async () => {
279
+ // Auth request that will clear cookies if session is invalid
280
+ await authClient.getSession();
281
+ await onUnauth();
282
+ }, [onUnauth]);
283
+
284
+ useEffect(() => {
285
+ void (async () => {
286
+ if (!isLoading && !isAuthenticated) {
287
+ await handleUnauth();
288
+ }
289
+ })();
290
+ }, [isLoading, isAuthenticated]);
291
+
292
+ return (
293
+ <ErrorBoundary
294
+ onUnauth={handleUnauth}
295
+ isAuthError={isAuthError}
296
+ renderFallback={renderFallback}
297
+ >
298
+ <Authenticated>
299
+ <UserSubscription getAuthUserFn={getAuthUserFn} />
300
+ </Authenticated>
301
+ {children}
302
+ </ErrorBoundary>
303
+ );
304
+ };
@@ -0,0 +1,153 @@
1
+ import { stripIndent } from "common-tags";
2
+ import type {
3
+ FunctionReference,
4
+ FunctionReturnType,
5
+ OptionalRestArgs,
6
+ } from "convex/server";
7
+ import { ConvexHttpClient } from "convex/browser";
8
+ import { getToken } from "../utils/index.js";
9
+ import type { GetTokenOptions } from "../utils/index.js";
10
+ import React from "react";
11
+
12
+ // Caching supported for React 19+ only
13
+ const cache =
14
+ (React as typeof React & {
15
+ cache?: <Fn extends (...args: any[]) => any>(fn: Fn) => Fn;
16
+ }).cache ||
17
+ ((fn: (...args: any[]) => any) => {
18
+ return (...args: any[]) => fn(...args);
19
+ });
20
+
21
+ type ClientOptions = {
22
+ /**
23
+ * The URL of the Convex deployment to use for the function call.
24
+ */
25
+ convexUrl: string;
26
+ /**
27
+ * The HTTP Actions URL of the Convex deployment to use for the function call.
28
+ */
29
+ convexSiteUrl: string;
30
+ /**
31
+ * The JWT-encoded OpenID Connect authentication token to use for the function call.
32
+ * Just an optional override for edge cases, you probably don't need this.
33
+ */
34
+ token?: string;
35
+ };
36
+
37
+ function setupClient(options: ClientOptions) {
38
+ const client = new ConvexHttpClient(options.convexUrl);
39
+ if (options.token !== undefined) {
40
+ client.setAuth(options.token);
41
+ }
42
+ // @ts-expect-error - setFetchOptions is internal
43
+ client.setFetchOptions({ cache: "no-store" });
44
+ return client;
45
+ }
46
+
47
+ const parseConvexSiteUrl = (url: string) => {
48
+ if (!url) {
49
+ throw new Error(stripIndent`
50
+ CONVEX_SITE_URL is not set.
51
+ This is automatically set in the Convex backend, but must be set in the TanStack Start environment.
52
+ For local development, this can be set in the .env.local file.
53
+ `);
54
+ }
55
+ if (url.endsWith(".convex.cloud")) {
56
+ throw new Error(stripIndent`
57
+ CONVEX_SITE_URL should be set to your Convex Site URL, which ends in .convex.site.
58
+ Currently set to ${url}.
59
+ `);
60
+ }
61
+ return url;
62
+ };
63
+
64
+ const handler = (request: Request, opts: { convexSiteUrl: string }) => {
65
+ const requestUrl = new URL(request.url);
66
+ const nextUrl = `${opts.convexSiteUrl}${requestUrl.pathname}${requestUrl.search}`;
67
+ const headers = new Headers(request.headers);
68
+ headers.set("accept-encoding", "application/json");
69
+ headers.set("host", new URL(opts.convexSiteUrl).host);
70
+ return fetch(nextUrl, {
71
+ method: request.method,
72
+ headers,
73
+ redirect: "manual",
74
+ body: request.body,
75
+ // @ts-expect-error - duplex is required for streaming request bodies in modern fetch
76
+ duplex: "half",
77
+ });
78
+ };
79
+
80
+ export const convexBetterAuthReactStart = (
81
+ opts: Omit<GetTokenOptions, "forceRefresh"> & {
82
+ convexUrl: string;
83
+ convexSiteUrl: string;
84
+ }
85
+ ) => {
86
+ const siteUrl = parseConvexSiteUrl(opts.convexSiteUrl);
87
+
88
+ const cachedGetToken = cache(async (opts: GetTokenOptions) => {
89
+ const { getRequestHeaders } = await import("@tanstack/react-start/server");
90
+ const headers = getRequestHeaders();
91
+ return getToken(siteUrl, headers, opts);
92
+ });
93
+
94
+ const callWithToken = async <
95
+ FnType extends "query" | "mutation" | "action",
96
+ Fn extends FunctionReference<FnType>,
97
+ >(
98
+ fn: (token?: string) => Promise<FunctionReturnType<Fn>>
99
+ ): Promise<FunctionReturnType<Fn>> => {
100
+ const token = (await cachedGetToken(opts)) ?? {};
101
+ try {
102
+ return await fn(token?.token);
103
+ } catch (error) {
104
+ if (
105
+ !opts?.jwtCache?.enabled ||
106
+ token.isFresh ||
107
+ opts.jwtCache?.isAuthError(error)
108
+ ) {
109
+ throw error;
110
+ }
111
+ const newToken = await cachedGetToken({
112
+ ...opts,
113
+ forceRefresh: true,
114
+ });
115
+ return await fn(newToken.token);
116
+ }
117
+ };
118
+
119
+ return {
120
+ getToken: async () => {
121
+ const token = await cachedGetToken(opts);
122
+ return token.token;
123
+ },
124
+ handler: (request: Request) => handler(request, opts),
125
+ fetchAuthQuery: async <Query extends FunctionReference<"query">>(
126
+ query: Query,
127
+ ...args: OptionalRestArgs<Query>
128
+ ): Promise<FunctionReturnType<Query>> => {
129
+ return callWithToken((token?: string) => {
130
+ const client = setupClient({ ...opts, token });
131
+ return client.query(query, ...args);
132
+ });
133
+ },
134
+ fetchAuthMutation: async <Mutation extends FunctionReference<"mutation">>(
135
+ mutation: Mutation,
136
+ ...args: OptionalRestArgs<Mutation>
137
+ ): Promise<FunctionReturnType<Mutation>> => {
138
+ return callWithToken((token?: string) => {
139
+ const client = setupClient({ ...opts, token });
140
+ return client.mutation(mutation, ...args);
141
+ });
142
+ },
143
+ fetchAuthAction: async <Action extends FunctionReference<"action">>(
144
+ action: Action,
145
+ ...args: OptionalRestArgs<Action>
146
+ ): Promise<FunctionReturnType<Action>> => {
147
+ return callWithToken((token?: string) => {
148
+ const client = setupClient({ ...opts, token });
149
+ return client.action(action, ...args);
150
+ });
151
+ },
152
+ };
153
+ };
@@ -0,0 +1,2 @@
1
+ /// <reference types="vite/client" />
2
+ /// <reference types="vite/types/importMeta.d.ts" />
package/src/test.ts ADDED
@@ -0,0 +1,18 @@
1
+ /// <reference types="vite/client" />
2
+ import type { TestConvex } from "convex-test";
3
+ import type { GenericSchema, SchemaDefinition } from "convex/server";
4
+ import schema from "./component/schema.js";
5
+ const modules = import.meta.glob("./component/**/*.ts");
6
+
7
+ /**
8
+ * Register the component with the test convex instance.
9
+ * @param t - The test convex instance, e.g. from calling `convexTest`.
10
+ * @param name - The name of the component, as registered in convex.config.ts.
11
+ */
12
+ export function register(
13
+ t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
14
+ name: string = "betterAuth"
15
+ ) {
16
+ t.registerComponent(name, schema, modules);
17
+ }
18
+ export default { register, schema, modules };
@@ -0,0 +1,171 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { Auth } from "better-auth";
3
+ import type { betterAuth } from "better-auth/minimal";
4
+ import { getSessionCookie } from "better-auth/cookies";
5
+ import type {
6
+ AuthProvider,
7
+ DefaultFunctionArgs,
8
+ FunctionReference,
9
+ GenericActionCtx,
10
+ GenericDataModel,
11
+ GenericMutationCtx,
12
+ GenericQueryCtx,
13
+ } from "convex/server";
14
+ import { JWT_COOKIE_NAME } from "../plugins/convex/index.js";
15
+ import * as jose from "jose";
16
+ import type { Jwk } from "better-auth/plugins/jwt";
17
+
18
+ export type CreateAuth<
19
+ DataModel extends GenericDataModel,
20
+ A extends ReturnType<typeof betterAuth> = Auth,
21
+ > = (ctx: GenericCtx<DataModel>) => A;
22
+
23
+ export type EventFunction<T extends DefaultFunctionArgs> = FunctionReference<
24
+ "mutation",
25
+ "internal" | "public",
26
+ T
27
+ >;
28
+
29
+ export type GenericCtx<DataModel extends GenericDataModel = GenericDataModel> =
30
+ | GenericQueryCtx<DataModel>
31
+ | GenericMutationCtx<DataModel>
32
+ | GenericActionCtx<DataModel>;
33
+
34
+ export type RunMutationCtx<DataModel extends GenericDataModel> = (
35
+ | GenericMutationCtx<DataModel>
36
+ | GenericActionCtx<DataModel>
37
+ ) & {
38
+ runMutation: GenericMutationCtx<DataModel>["runMutation"];
39
+ };
40
+
41
+ export const isQueryCtx = <DataModel extends GenericDataModel>(
42
+ ctx: GenericCtx<DataModel>
43
+ ): ctx is GenericQueryCtx<DataModel> => {
44
+ return "db" in ctx;
45
+ };
46
+
47
+ export const isMutationCtx = <DataModel extends GenericDataModel>(
48
+ ctx: GenericCtx<DataModel>
49
+ ): ctx is GenericMutationCtx<DataModel> => {
50
+ return "db" in ctx && "scheduler" in ctx;
51
+ };
52
+
53
+ export const isActionCtx = <DataModel extends GenericDataModel>(
54
+ ctx: GenericCtx<DataModel>
55
+ ): ctx is GenericActionCtx<DataModel> => {
56
+ return "runAction" in ctx;
57
+ };
58
+
59
+ export const isRunMutationCtx = <DataModel extends GenericDataModel>(
60
+ ctx: GenericCtx<DataModel>
61
+ ): ctx is RunMutationCtx<DataModel> => {
62
+ return "runMutation" in ctx;
63
+ };
64
+
65
+ export const requireQueryCtx = <DataModel extends GenericDataModel>(
66
+ ctx: GenericCtx<DataModel>
67
+ ): GenericQueryCtx<DataModel> => {
68
+ if (!isQueryCtx(ctx)) {
69
+ throw new Error("Query context required");
70
+ }
71
+ return ctx;
72
+ };
73
+
74
+ export const requireMutationCtx = <DataModel extends GenericDataModel>(
75
+ ctx: GenericCtx<DataModel>
76
+ ): GenericMutationCtx<DataModel> => {
77
+ if (!isMutationCtx(ctx)) {
78
+ throw new Error("Mutation context required");
79
+ }
80
+ return ctx;
81
+ };
82
+
83
+ export const requireActionCtx = <DataModel extends GenericDataModel>(
84
+ ctx: GenericCtx<DataModel>
85
+ ): GenericActionCtx<DataModel> => {
86
+ if (!isActionCtx(ctx)) {
87
+ throw new Error("Action context required");
88
+ }
89
+ return ctx;
90
+ };
91
+
92
+ export const requireRunMutationCtx = <DataModel extends GenericDataModel>(
93
+ ctx: GenericCtx<DataModel>
94
+ ): RunMutationCtx<DataModel> => {
95
+ if (!isRunMutationCtx(ctx)) {
96
+ throw new Error("Mutation or action context required");
97
+ }
98
+ return ctx;
99
+ };
100
+
101
+ export type GetTokenOptions = {
102
+ forceRefresh?: boolean;
103
+ cookiePrefix?: string;
104
+ jwtCache?: {
105
+ enabled: boolean;
106
+ expirationToleranceSeconds?: number;
107
+ isAuthError: (error: unknown) => boolean;
108
+ };
109
+ };
110
+
111
+ export const getToken = async (
112
+ siteUrl: string,
113
+ headers: Headers,
114
+ opts?: GetTokenOptions
115
+ ) => {
116
+ const fetchToken = async () => {
117
+ const { data } = await betterFetch<{ token: string }>(
118
+ "/api/auth/convex/token",
119
+ {
120
+ baseURL: siteUrl,
121
+ headers,
122
+ }
123
+ );
124
+ return { isFresh: true, token: data?.token };
125
+ };
126
+ if (!opts?.jwtCache?.enabled || opts.forceRefresh) {
127
+ return await fetchToken();
128
+ }
129
+ const token = getSessionCookie(new Headers(headers), {
130
+ cookieName: JWT_COOKIE_NAME,
131
+ cookiePrefix: opts?.cookiePrefix,
132
+ });
133
+ if (!token) {
134
+ return await fetchToken();
135
+ }
136
+ try {
137
+ const claims = jose.decodeJwt(token);
138
+ const exp = claims?.exp;
139
+ const now = Math.floor(new Date().getTime() / 1000);
140
+ const isExpired = exp
141
+ ? now > exp + (opts?.jwtCache?.expirationToleranceSeconds ?? 60)
142
+ : true;
143
+ if (!isExpired) {
144
+ return { isFresh: false, token };
145
+ }
146
+ } catch (error) {
147
+ // eslint-disable-next-line no-console
148
+ console.error("Error decoding JWT", error);
149
+ }
150
+ return await fetchToken();
151
+ };
152
+
153
+ export const parseJwks = (providerConfig: AuthProvider) => {
154
+ const staticJwksString =
155
+ "jwks" in providerConfig && providerConfig.jwks?.startsWith("data:text/")
156
+ ? atob(providerConfig.jwks.split("base64,")[1])
157
+ : undefined;
158
+
159
+ if (!staticJwksString) {
160
+ return;
161
+ }
162
+ const parsed = JSON.parse(
163
+ staticJwksString?.slice(1, -1).replaceAll(/[\s\\]/g, "") || "{}"
164
+ );
165
+ const staticJwks = {
166
+ ...parsed,
167
+ privateKey: `"${parsed.privateKey}"`,
168
+ publicKey: `"${parsed.publicKey}"`,
169
+ } as Jwk;
170
+ return staticJwks;
171
+ };