kitcn 0.15.0 → 0.15.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.
|
@@ -2,10 +2,36 @@ import { t as GetTokenOptions } from "../../token-B9Bjcqug.js";
|
|
|
2
2
|
import { FunctionReference, FunctionReturnType, OptionalRestArgs } from "convex/server";
|
|
3
3
|
|
|
4
4
|
//#region src/auth-start/index.d.ts
|
|
5
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
6
|
+
type StartLoaderAuthClient = {
|
|
7
|
+
clearAuth: () => void;
|
|
8
|
+
setAuth: (fetchToken: () => Promise<string | null>) => void;
|
|
9
|
+
};
|
|
10
|
+
type StartLoaderServerHttpClient = {
|
|
11
|
+
clearAuth?: () => void;
|
|
12
|
+
setAuth: (token: string) => void;
|
|
13
|
+
};
|
|
14
|
+
type StartLoaderConvexQueryClient = {
|
|
15
|
+
convexClient: StartLoaderAuthClient;
|
|
16
|
+
serverHttpClient?: StartLoaderServerHttpClient;
|
|
17
|
+
};
|
|
18
|
+
type StartLoaderAuthTarget = StartLoaderAuthClient | StartLoaderConvexQueryClient;
|
|
19
|
+
type SyncConvexAuthForStartLoaderOptions = {
|
|
20
|
+
convex: StartLoaderAuthTarget;
|
|
21
|
+
getToken: () => MaybePromise<null | string | undefined>;
|
|
22
|
+
};
|
|
23
|
+
type StartLoaderAuthState = {
|
|
24
|
+
isAuthenticated: boolean;
|
|
25
|
+
token: null | string;
|
|
26
|
+
};
|
|
5
27
|
type ConvexBetterAuthReactStartOptions = Omit<GetTokenOptions, 'forceRefresh'> & {
|
|
6
28
|
convexSiteUrl: string;
|
|
7
29
|
convexUrl: string;
|
|
8
30
|
};
|
|
31
|
+
declare const syncConvexAuthForStartLoader: ({
|
|
32
|
+
convex,
|
|
33
|
+
getToken
|
|
34
|
+
}: SyncConvexAuthForStartLoaderOptions) => Promise<StartLoaderAuthState>;
|
|
9
35
|
declare const convexBetterAuthReactStart: (opts: ConvexBetterAuthReactStartOptions) => {
|
|
10
36
|
getToken: () => Promise<string | undefined>;
|
|
11
37
|
handler: (request: Request) => Promise<Response>;
|
|
@@ -14,4 +40,4 @@ declare const convexBetterAuthReactStart: (opts: ConvexBetterAuthReactStartOptio
|
|
|
14
40
|
fetchAuthAction: <Action extends FunctionReference<"action">>(action: Action, ...args: OptionalRestArgs<Action>) => Promise<FunctionReturnType<Action>>;
|
|
15
41
|
};
|
|
16
42
|
//#endregion
|
|
17
|
-
export { convexBetterAuthReactStart };
|
|
43
|
+
export { MaybePromise, StartLoaderAuthClient, StartLoaderAuthState, StartLoaderAuthTarget, StartLoaderConvexQueryClient, StartLoaderServerHttpClient, SyncConvexAuthForStartLoaderOptions, convexBetterAuthReactStart, syncConvexAuthForStartLoader };
|
package/dist/auth/start/index.js
CHANGED
|
@@ -8,6 +8,32 @@ const fallbackCache = (fn) => fn;
|
|
|
8
8
|
const cache = React.cache ?? fallbackCache;
|
|
9
9
|
const TANSTACK_REACT_START_SERVER = "@tanstack/react-start/server";
|
|
10
10
|
const TRAILING_COLON_RE = /:$/;
|
|
11
|
+
const startLoaderAuthTokens = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
const isStartLoaderConvexQueryClient = (target) => "convexClient" in target;
|
|
13
|
+
const syncConvexAuthForStartLoader = async ({ convex, getToken }) => {
|
|
14
|
+
const authClient = isStartLoaderConvexQueryClient(convex) ? convex.convexClient : convex;
|
|
15
|
+
const serverHttpClient = isStartLoaderConvexQueryClient(convex) ? convex.serverHttpClient : void 0;
|
|
16
|
+
const token = await getToken() ?? null;
|
|
17
|
+
if (startLoaderAuthTokens.get(convex) === token) return {
|
|
18
|
+
isAuthenticated: token !== null,
|
|
19
|
+
token
|
|
20
|
+
};
|
|
21
|
+
startLoaderAuthTokens.set(convex, token);
|
|
22
|
+
if (token === null) {
|
|
23
|
+
authClient.clearAuth();
|
|
24
|
+
serverHttpClient?.clearAuth?.();
|
|
25
|
+
return {
|
|
26
|
+
isAuthenticated: false,
|
|
27
|
+
token
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
authClient.setAuth(async () => token);
|
|
31
|
+
serverHttpClient?.setAuth(token);
|
|
32
|
+
return {
|
|
33
|
+
isAuthenticated: true,
|
|
34
|
+
token
|
|
35
|
+
};
|
|
36
|
+
};
|
|
11
37
|
function setupClient(options) {
|
|
12
38
|
const client = new ConvexHttpClient(options.convexUrl);
|
|
13
39
|
if (options.token !== void 0) client.setAuth(options.token);
|
|
@@ -124,4 +150,4 @@ const convexBetterAuthReactStart = (opts) => {
|
|
|
124
150
|
};
|
|
125
151
|
|
|
126
152
|
//#endregion
|
|
127
|
-
export { convexBetterAuthReactStart };
|
|
153
|
+
export { convexBetterAuthReactStart, syncConvexAuthForStartLoader };
|
package/package.json
CHANGED
|
@@ -48,6 +48,40 @@ export const {
|
|
|
48
48
|
});
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
For client-side route loaders that fetch protected Convex queries through the
|
|
52
|
+
router `queryClient`, prime the shared Convex client in the root `beforeLoad`
|
|
53
|
+
before child loaders run:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
57
|
+
import { createRootRouteWithContext } from "@tanstack/react-router";
|
|
58
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
59
|
+
import { syncConvexAuthForStartLoader } from "kitcn/auth/start";
|
|
60
|
+
import type { ConvexQueryClient } from "kitcn/react";
|
|
61
|
+
|
|
62
|
+
import { getToken } from "@/lib/convex/auth-server";
|
|
63
|
+
|
|
64
|
+
const getLoaderToken = createServerFn({ method: "GET" }).handler(async () => {
|
|
65
|
+
return await getToken();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const Route = createRootRouteWithContext<{
|
|
69
|
+
convexQueryClient: ConvexQueryClient;
|
|
70
|
+
queryClient: QueryClient;
|
|
71
|
+
}>()({
|
|
72
|
+
beforeLoad: async ({ context }) => {
|
|
73
|
+
return await syncConvexAuthForStartLoader({
|
|
74
|
+
convex: context.convexQueryClient,
|
|
75
|
+
getToken: getLoaderToken,
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Use `runServerCall` or `fetchAuthQuery` for server-side loaders. Use
|
|
82
|
+
`syncConvexAuthForStartLoader` only for client/router loaders that execute
|
|
83
|
+
shared `ConvexQueryClient` queries before `ConvexAuthProvider` mounts.
|
|
84
|
+
|
|
51
85
|
### 8.B.2 Auth API endpoint
|
|
52
86
|
|
|
53
87
|
**Create:** `src/routes/api/auth/$.ts`
|