@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.
- package/LICENSE +201 -0
- package/README.md +39 -0
- package/dist/auth-config.d.ts +43 -0
- package/dist/auth-config.d.ts.map +1 -0
- package/dist/auth-config.js +43 -0
- package/dist/auth-config.js.map +1 -0
- package/dist/auth-options.d.ts +3 -0
- package/dist/auth-options.d.ts.map +1 -0
- package/dist/auth-options.js +40 -0
- package/dist/auth-options.js.map +1 -0
- package/dist/auth.d.ts +2 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +4 -0
- package/dist/auth.js.map +1 -0
- package/dist/client/adapter-utils.d.ts +66 -0
- package/dist/client/adapter-utils.d.ts.map +1 -0
- package/dist/client/adapter-utils.js +437 -0
- package/dist/client/adapter-utils.js.map +1 -0
- package/dist/client/adapter.d.ts +14 -0
- package/dist/client/adapter.d.ts.map +1 -0
- package/dist/client/adapter.js +274 -0
- package/dist/client/adapter.js.map +1 -0
- package/dist/client/create-api.d.ts +141 -0
- package/dist/client/create-api.d.ts.map +1 -0
- package/dist/client/create-api.js +205 -0
- package/dist/client/create-api.js.map +1 -0
- package/dist/client/create-client.d.ts +183 -0
- package/dist/client/create-client.d.ts.map +1 -0
- package/dist/client/create-client.js +311 -0
- package/dist/client/create-client.js.map +1 -0
- package/dist/client/create-schema.d.ts +19 -0
- package/dist/client/create-schema.d.ts.map +1 -0
- package/dist/client/create-schema.js +114 -0
- package/dist/client/create-schema.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +10 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/plugins/index.d.ts +3 -0
- package/dist/client/plugins/index.d.ts.map +1 -0
- package/dist/client/plugins/index.js +3 -0
- package/dist/client/plugins/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +787 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/adapter.d.ts +130 -0
- package/dist/component/adapter.d.ts.map +1 -0
- package/dist/component/adapter.js +5 -0
- package/dist/component/adapter.js.map +1 -0
- package/dist/component/adapterTest.d.ts +10 -0
- package/dist/component/adapterTest.d.ts.map +1 -0
- package/dist/component/adapterTest.js +409 -0
- package/dist/component/adapterTest.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +4 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/schema.d.ts +474 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +139 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/nextjs/client.d.ts +4 -0
- package/dist/nextjs/client.d.ts.map +1 -0
- package/dist/nextjs/client.js +37 -0
- package/dist/nextjs/client.js.map +1 -0
- package/dist/nextjs/index.d.ts +22 -0
- package/dist/nextjs/index.d.ts.map +1 -0
- package/dist/nextjs/index.js +98 -0
- package/dist/nextjs/index.js.map +1 -0
- package/dist/plugins/convex/client.d.ts +6 -0
- package/dist/plugins/convex/client.d.ts.map +1 -0
- package/dist/plugins/convex/client.js +7 -0
- package/dist/plugins/convex/client.js.map +1 -0
- package/dist/plugins/convex/index.d.ts +322 -0
- package/dist/plugins/convex/index.d.ts.map +1 -0
- package/dist/plugins/convex/index.js +422 -0
- package/dist/plugins/convex/index.js.map +1 -0
- package/dist/plugins/cross-domain/client.d.ts +132 -0
- package/dist/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/plugins/cross-domain/client.js +192 -0
- package/dist/plugins/cross-domain/client.js.map +1 -0
- package/dist/plugins/cross-domain/index.d.ts +51 -0
- package/dist/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/plugins/cross-domain/index.js +173 -0
- package/dist/plugins/cross-domain/index.js.map +1 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/react/index.d.ts +80 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +190 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react-start/index.d.ts +13 -0
- package/dist/react-start/index.d.ts.map +1 -0
- package/dist/react-start/index.js +101 -0
- package/dist/react-start/index.js.map +1 -0
- package/dist/utils/index.d.ts +33 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +91 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +208 -0
- package/src/auth-config.ts +80 -0
- package/src/auth-options.ts +54 -0
- package/src/auth.ts +4 -0
- package/src/client/adapter-utils.ts +639 -0
- package/src/client/adapter.test.ts +83 -0
- package/src/client/adapter.ts +363 -0
- package/src/client/create-api.ts +339 -0
- package/src/client/create-client.ts +452 -0
- package/src/client/create-schema.ts +166 -0
- package/src/client/index.ts +22 -0
- package/src/client/plugins/index.ts +2 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +2008 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +161 -0
- package/src/component/adapter.ts +13 -0
- package/src/component/adapterTest.ts +505 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/schema.ts +142 -0
- package/src/nextjs/client.tsx +54 -0
- package/src/nextjs/index.ts +152 -0
- package/src/plugins/convex/client.ts +9 -0
- package/src/plugins/convex/index.ts +596 -0
- package/src/plugins/cross-domain/client.test.ts +217 -0
- package/src/plugins/cross-domain/client.ts +234 -0
- package/src/plugins/cross-domain/index.ts +199 -0
- package/src/plugins/index.ts +2 -0
- package/src/react/index.tsx +304 -0
- package/src/react-start/index.ts +153 -0
- package/src/react-start/vite-env.d.ts +2 -0
- package/src/test.ts +18 -0
- 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
|
+
};
|
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
|
+
};
|