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.
- package/LICENSE +21 -0
- package/README.md +744 -0
- package/dist/convex/auth.config.d.ts +9 -0
- package/dist/convex/auth.config.d.ts.map +1 -0
- package/dist/convex/auth.config.js +14 -0
- package/dist/convex/auth.config.js.map +1 -0
- package/dist/convex/client/index.d.ts +12784 -0
- package/dist/convex/client/index.d.ts.map +1 -0
- package/dist/convex/client/index.js +230 -0
- package/dist/convex/client/index.js.map +1 -0
- package/dist/convex/component/_generated/api.d.ts +36 -0
- package/dist/convex/component/_generated/api.d.ts.map +1 -0
- package/dist/convex/component/_generated/api.js +31 -0
- package/dist/convex/component/_generated/api.js.map +1 -0
- package/dist/convex/component/_generated/component.d.ts +549 -0
- package/dist/convex/component/_generated/component.d.ts.map +1 -0
- package/dist/convex/component/_generated/component.js +11 -0
- package/dist/convex/component/_generated/component.js.map +1 -0
- package/dist/convex/component/_generated/dataModel.d.ts +46 -0
- package/dist/convex/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/convex/component/_generated/dataModel.js +11 -0
- package/dist/convex/component/_generated/dataModel.js.map +1 -0
- package/dist/convex/component/_generated/server.d.ts +121 -0
- package/dist/convex/component/_generated/server.d.ts.map +1 -0
- package/dist/convex/component/_generated/server.js +78 -0
- package/dist/convex/component/_generated/server.js.map +1 -0
- package/dist/convex/component/adapter.d.ts +3 -0
- package/dist/convex/component/adapter.d.ts.map +1 -0
- package/dist/convex/component/adapter.js +23 -0
- package/dist/convex/component/adapter.js.map +1 -0
- package/dist/convex/component/constants.d.ts +3 -0
- package/dist/convex/component/constants.d.ts.map +1 -0
- package/dist/convex/component/constants.js +3 -0
- package/dist/convex/component/constants.js.map +1 -0
- package/dist/convex/component/convex.config.d.ts +14 -0
- package/dist/convex/component/convex.config.d.ts.map +1 -0
- package/dist/convex/component/convex.config.js +15 -0
- package/dist/convex/component/convex.config.js.map +1 -0
- package/dist/convex/component/schema.d.ts +278 -0
- package/dist/convex/component/schema.d.ts.map +1 -0
- package/dist/convex/component/schema.js +91 -0
- package/dist/convex/component/schema.js.map +1 -0
- package/dist/module.d.ts +47 -0
- package/dist/module.js +275 -0
- package/dist/module.json +9 -0
- package/dist/runtime/nuxt/auth/middleware.d.ts +12 -0
- package/dist/runtime/nuxt/auth/middleware.js +31 -0
- package/dist/runtime/nuxt/auth/proxy.d.ts +6 -0
- package/dist/runtime/nuxt/auth/proxy.js +11 -0
- package/dist/runtime/nuxt/auth/server.d.ts +86 -0
- package/dist/runtime/nuxt/auth/server.js +110 -0
- package/dist/runtime/nuxt/config.d.ts +7 -0
- package/dist/runtime/nuxt/config.js +12 -0
- package/dist/runtime/nuxt/index.d.ts +102 -0
- package/dist/runtime/nuxt/index.js +77 -0
- package/dist/runtime/vue/auth/client.d.ts +14 -0
- package/dist/runtime/vue/auth/client.js +7 -0
- package/dist/runtime/vue/auth/cross-domain.d.ts +8 -0
- package/dist/runtime/vue/auth/cross-domain.js +34 -0
- package/dist/runtime/vue/auth/helpers.d.ts +52 -0
- package/dist/runtime/vue/auth/helpers.js +35 -0
- package/dist/runtime/vue/auth/hydration.d.ts +14 -0
- package/dist/runtime/vue/auth/hydration.js +23 -0
- package/dist/runtime/vue/auth/index.d.ts +79 -0
- package/dist/runtime/vue/auth/index.js +81 -0
- package/dist/runtime/vue/auth/plugin.client.d.ts +6 -0
- package/dist/runtime/vue/auth/plugin.client.js +30 -0
- package/dist/runtime/vue/auth/plugin.server.d.ts +17 -0
- package/dist/runtime/vue/auth/plugin.server.js +29 -0
- package/dist/runtime/vue/auth/use-auth.d.ts +84 -0
- package/dist/runtime/vue/auth/use-auth.js +77 -0
- package/dist/runtime/vue/client.d.ts +245 -0
- package/dist/runtime/vue/client.js +341 -0
- package/dist/runtime/vue/composables/use-action.d.ts +45 -0
- package/dist/runtime/vue/composables/use-action.js +10 -0
- package/dist/runtime/vue/composables/use-connection-state.d.ts +25 -0
- package/dist/runtime/vue/composables/use-connection-state.js +13 -0
- package/dist/runtime/vue/composables/use-mutation.d.ts +60 -0
- package/dist/runtime/vue/composables/use-mutation.js +36 -0
- package/dist/runtime/vue/composables/use-paginated-query.d.ts +101 -0
- package/dist/runtime/vue/composables/use-paginated-query.js +508 -0
- package/dist/runtime/vue/composables/use-queries.d.ts +45 -0
- package/dist/runtime/vue/composables/use-queries.js +32 -0
- package/dist/runtime/vue/composables/use-query.d.ts +82 -0
- package/dist/runtime/vue/composables/use-query.js +63 -0
- package/dist/runtime/vue/composables/use-subscription.d.ts +14 -0
- package/dist/runtime/vue/composables/use-subscription.js +15 -0
- package/dist/runtime/vue/hydration.d.ts +42 -0
- package/dist/runtime/vue/hydration.js +18 -0
- package/dist/runtime/vue/index.d.ts +29 -0
- package/dist/runtime/vue/index.js +38 -0
- package/dist/runtime/vue/queries-observer.d.ts +38 -0
- package/dist/runtime/vue/queries-observer.js +127 -0
- package/dist/types.d.ts +3 -0
- package/package.json +157 -0
- package/src/convex/auth.config.ts +21 -0
- package/src/convex/client/index.ts +329 -0
- package/src/convex/component/_generated/api.ts +52 -0
- package/src/convex/component/_generated/component.ts +1338 -0
- package/src/convex/component/_generated/dataModel.ts +60 -0
- package/src/convex/component/_generated/server.ts +156 -0
- package/src/convex/component/adapter.ts +42 -0
- package/src/convex/component/constants.ts +2 -0
- package/src/convex/component/convex.config.ts +16 -0
- package/src/convex/component/schema.ts +93 -0
- package/src/convex/test.ts +34 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ConvexHttpClient } from "convex/browser";
|
|
2
|
+
import { getFunctionName } from "convex/server";
|
|
3
|
+
import { convexToJson, jsonToConvex } from "convex/values";
|
|
4
|
+
import { getBackendRuntimeConfig } from "./config.js";
|
|
5
|
+
function getConvexUrl(deploymentUrl, skipConvexDeploymentUrlCheck) {
|
|
6
|
+
const runtimeUrl = getBackendRuntimeConfig().url;
|
|
7
|
+
const url = deploymentUrl ?? runtimeUrl ?? process.env.NUXT_PUBLIC_CONVEX_URL;
|
|
8
|
+
if (typeof url !== "string" || !url) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
deploymentUrl === void 0 ? "Environment variable NUXT_PUBLIC_CONVEX_URL is not set." : "Convex function called with invalid deployment address."
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
if (!skipConvexDeploymentUrlCheck) {
|
|
14
|
+
if (!(url.startsWith("http:") || url.startsWith("https:"))) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Invalid deployment address: Must start with "https://" or "http://". Found "${url}".`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
new URL(url);
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Invalid deployment address: "${url}" is not a valid URL. If you believe this URL is correct, use the \`skipConvexDeploymentUrlCheck\` option to bypass this.`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
if (url.endsWith(".convex.site")) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Invalid deployment address: "${url}" ends with .convex.site, which is used for HTTP Actions. Convex deployment URLs typically end with .convex.cloud? If you believe this URL is correct, use the \`skipConvexDeploymentUrlCheck\` option to bypass this.`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
function setupClient(options) {
|
|
35
|
+
if ("url" in options && options.url === void 0) {
|
|
36
|
+
console.error(
|
|
37
|
+
"deploymentUrl is undefined, are your environment variables set? In the future explicitly passing undefined will cause an error. To explicitly use the default, pass `process.env.NUXT_PUBLIC_CONVEX_URL`."
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const skip = options.skipConvexDeploymentUrlCheck ?? false;
|
|
41
|
+
const client = new ConvexHttpClient(getConvexUrl(options.url, skip));
|
|
42
|
+
if (options.token !== void 0) {
|
|
43
|
+
client.setAuth(options.token);
|
|
44
|
+
}
|
|
45
|
+
if (options.adminToken !== void 0) {
|
|
46
|
+
client.setAdminAuth(options.adminToken);
|
|
47
|
+
}
|
|
48
|
+
client.setFetchOptions({ cache: "no-store" });
|
|
49
|
+
return client;
|
|
50
|
+
}
|
|
51
|
+
export async function preloadQuery(query, ...args) {
|
|
52
|
+
const value = await fetchQuery(query, ...args);
|
|
53
|
+
const preloaded = {
|
|
54
|
+
_name: getFunctionName(query),
|
|
55
|
+
_argsJSON: convexToJson(args[0] ?? {}),
|
|
56
|
+
_valueJSON: convexToJson(value)
|
|
57
|
+
};
|
|
58
|
+
return preloaded;
|
|
59
|
+
}
|
|
60
|
+
export function preloadedQueryResult(preloaded) {
|
|
61
|
+
return jsonToConvex(preloaded._valueJSON);
|
|
62
|
+
}
|
|
63
|
+
export async function fetchQuery(query, ...args) {
|
|
64
|
+
const [fnArgs, options] = args;
|
|
65
|
+
const client = setupClient(options ?? {});
|
|
66
|
+
return client.query(query, fnArgs || {});
|
|
67
|
+
}
|
|
68
|
+
export async function fetchMutation(mutation, ...args) {
|
|
69
|
+
const [fnArgs, options] = args;
|
|
70
|
+
const client = setupClient(options ?? {});
|
|
71
|
+
return client.mutation(mutation, fnArgs || {});
|
|
72
|
+
}
|
|
73
|
+
export async function fetchAction(action, ...args) {
|
|
74
|
+
const [fnArgs, options] = args;
|
|
75
|
+
const client = setupClient(options ?? {});
|
|
76
|
+
return client.action(action, fnArgs || {});
|
|
77
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BetterAuthClientPlugin } from 'better-auth';
|
|
2
|
+
import { convexClient } from '@convex-dev/better-auth/client/plugins';
|
|
3
|
+
import { emailOTPClient } from 'better-auth/client/plugins';
|
|
4
|
+
import { passkeyClient } from '@better-auth/passkey/client';
|
|
5
|
+
import { createAuthClient } from 'better-auth/vue';
|
|
6
|
+
type ConvexPlugin = ReturnType<typeof convexClient>;
|
|
7
|
+
type EmailOTPPlugin = ReturnType<typeof emailOTPClient>;
|
|
8
|
+
type PasskeyPlugin = ReturnType<typeof passkeyClient>;
|
|
9
|
+
type AuthClientWithPlugins<Plugins extends (ConvexPlugin | BetterAuthClientPlugin)[]> = ReturnType<typeof createAuthClient<BetterAuthClientPlugin & {
|
|
10
|
+
plugins: Plugins;
|
|
11
|
+
}>>;
|
|
12
|
+
export type AuthClient = AuthClientWithPlugins<[ConvexPlugin, EmailOTPPlugin, PasskeyPlugin]>;
|
|
13
|
+
export declare const authClient: AuthClient;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { convexClient } from "@convex-dev/better-auth/client/plugins";
|
|
2
|
+
import { emailOTPClient } from "better-auth/client/plugins";
|
|
3
|
+
import { passkeyClient } from "@better-auth/passkey/client";
|
|
4
|
+
import { createAuthClient } from "better-auth/vue";
|
|
5
|
+
export const authClient = createAuthClient({
|
|
6
|
+
plugins: [convexClient(), emailOTPClient(), passkeyClient()]
|
|
7
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exchange a `?ott=...` one-time token (set by the Better Auth cross-domain
|
|
3
|
+
* plugin when redirecting from an auth origin) for a full session.
|
|
4
|
+
*
|
|
5
|
+
* Safe to call unconditionally — returns early when the URL has no `ott`
|
|
6
|
+
* parameter or the cross-domain plugin is not installed on the auth client.
|
|
7
|
+
*/
|
|
8
|
+
export declare function consumeCrossDomainOneTimeToken(): Promise<void>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { authClient } from "./client.js";
|
|
2
|
+
const OTT_QUERY_PARAM = "ott";
|
|
3
|
+
function readOneTimeTokenFromUrl() {
|
|
4
|
+
if (typeof window === "undefined" || !window.location?.href) return null;
|
|
5
|
+
const url = new URL(window.location.href);
|
|
6
|
+
const token = url.searchParams.get(OTT_QUERY_PARAM);
|
|
7
|
+
if (!token) return null;
|
|
8
|
+
url.searchParams.delete(OTT_QUERY_PARAM);
|
|
9
|
+
window.history.replaceState({}, "", url);
|
|
10
|
+
return token;
|
|
11
|
+
}
|
|
12
|
+
async function exchangeOneTimeTokenForSession(token) {
|
|
13
|
+
const client = authClient;
|
|
14
|
+
const verify = client.crossDomain?.oneTimeToken?.verify;
|
|
15
|
+
if (!verify) return;
|
|
16
|
+
const result = await verify({ token });
|
|
17
|
+
const sessionToken = result?.data?.session?.token;
|
|
18
|
+
if (!sessionToken) return;
|
|
19
|
+
await authClient.getSession({
|
|
20
|
+
fetchOptions: {
|
|
21
|
+
headers: { Authorization: `Bearer ${sessionToken}` }
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
client.crossDomain?.updateSession?.();
|
|
25
|
+
}
|
|
26
|
+
export async function consumeCrossDomainOneTimeToken() {
|
|
27
|
+
const token = readOneTimeTokenFromUrl();
|
|
28
|
+
if (!token) return;
|
|
29
|
+
try {
|
|
30
|
+
await exchangeOneTimeTokenForSession(token);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.warn("[nuxt-backend] failed to consume cross-domain one-time token", error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type SlotsType, type VNode } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Renders slot content only if the client is authenticated.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```vue
|
|
7
|
+
* <Authenticated>
|
|
8
|
+
* <p>You are logged in!</p>
|
|
9
|
+
* </Authenticated>
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export declare const Authenticated: import("vue").DefineComponent<{}, () => VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}>[] | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, SlotsType<{
|
|
17
|
+
default: () => VNode[];
|
|
18
|
+
}>, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
19
|
+
/**
|
|
20
|
+
* Renders slot content only if the client is unauthenticated (and not loading).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```vue
|
|
24
|
+
* <Unauthenticated>
|
|
25
|
+
* <p>Please log in.</p>
|
|
26
|
+
* </Unauthenticated>
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export declare const Unauthenticated: import("vue").DefineComponent<{}, () => VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}>[] | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, SlotsType<{
|
|
34
|
+
default: () => VNode[];
|
|
35
|
+
}>, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
36
|
+
/**
|
|
37
|
+
* Renders slot content only while the auth state is loading.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```vue
|
|
41
|
+
* <AuthLoading>
|
|
42
|
+
* <p>Checking authentication...</p>
|
|
43
|
+
* </AuthLoading>
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare const AuthLoading: import("vue").DefineComponent<{}, () => VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
49
|
+
[key: string]: any;
|
|
50
|
+
}>[] | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, SlotsType<{
|
|
51
|
+
default: () => VNode[];
|
|
52
|
+
}>, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineComponent } from "vue";
|
|
2
|
+
import { useConvexAuth } from "./index.js";
|
|
3
|
+
export const Authenticated = defineComponent({
|
|
4
|
+
name: "Authenticated",
|
|
5
|
+
slots: Object,
|
|
6
|
+
setup(_, { slots }) {
|
|
7
|
+
const auth = useConvexAuth();
|
|
8
|
+
return () => {
|
|
9
|
+
if (auth.isLoading || !auth.isAuthenticated) return null;
|
|
10
|
+
return slots.default?.();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
export const Unauthenticated = defineComponent({
|
|
15
|
+
name: "Unauthenticated",
|
|
16
|
+
slots: Object,
|
|
17
|
+
setup(_, { slots }) {
|
|
18
|
+
const auth = useConvexAuth();
|
|
19
|
+
return () => {
|
|
20
|
+
if (auth.isLoading || auth.isAuthenticated) return null;
|
|
21
|
+
return slots.default?.();
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
export const AuthLoading = defineComponent({
|
|
26
|
+
name: "AuthLoading",
|
|
27
|
+
slots: Object,
|
|
28
|
+
setup(_, { slots }) {
|
|
29
|
+
const auth = useConvexAuth();
|
|
30
|
+
return () => {
|
|
31
|
+
if (!auth.isLoading) return null;
|
|
32
|
+
return slots.default?.();
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FunctionReference, FunctionReturnType } from 'convex/server';
|
|
2
|
+
import { type ShallowRef } from 'vue';
|
|
3
|
+
import type { Preloaded } from '../hydration.js';
|
|
4
|
+
/**
|
|
5
|
+
* Auth-aware version of {@link usePreloadedQuery} for payloads returned by
|
|
6
|
+
* `backendAuth(event).preloadAuthQuery(...)`.
|
|
7
|
+
*
|
|
8
|
+
* Keeps the server-rendered result visible while client auth is still
|
|
9
|
+
* loading, skips the live query while unauthenticated, and switches to live
|
|
10
|
+
* data once Convex confirms the authenticated state.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export declare function usePreloadedAuthQuery<Query extends FunctionReference<'query'>>(preloadedQuery: Preloaded<Query>): ShallowRef<FunctionReturnType<Query> | null>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
|
+
import { jsonToConvex } from "convex/values";
|
|
3
|
+
import { computed, shallowRef } from "vue";
|
|
4
|
+
import { useQuery } from "../composables/use-query.js";
|
|
5
|
+
import { useConvexAuth } from "./index.js";
|
|
6
|
+
export function usePreloadedAuthQuery(preloadedQuery) {
|
|
7
|
+
const args = jsonToConvex(preloadedQuery._argsJSON);
|
|
8
|
+
const preloadedResult = jsonToConvex(preloadedQuery._valueJSON);
|
|
9
|
+
const query = makeFunctionReference(preloadedQuery._name);
|
|
10
|
+
if (import.meta.server) {
|
|
11
|
+
return shallowRef(preloadedResult);
|
|
12
|
+
}
|
|
13
|
+
const auth = useConvexAuth();
|
|
14
|
+
const liveResult = useQuery(
|
|
15
|
+
query,
|
|
16
|
+
computed(() => auth.isAuthenticated ? args : "skip")
|
|
17
|
+
);
|
|
18
|
+
return computed(() => {
|
|
19
|
+
if (auth.isLoading) return preloadedResult;
|
|
20
|
+
if (!auth.isAuthenticated) return null;
|
|
21
|
+
return liveResult.value !== void 0 ? liveResult.value : preloadedResult;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { AuthTokenFetcher } from 'convex/browser';
|
|
2
|
+
import { type EffectScope, type InjectionKey, type MaybeRefOrGetter } from 'vue';
|
|
3
|
+
import type { ConvexVueClient } from '../client.js';
|
|
4
|
+
/**
|
|
5
|
+
* Reactive auth state provided by {@link provideConvexAuth}.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export interface ConvexAuthState {
|
|
10
|
+
isLoading: boolean;
|
|
11
|
+
isAuthenticated: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const ConvexAuthStateKey: InjectionKey<ConvexAuthState>;
|
|
14
|
+
/**
|
|
15
|
+
* Access the Convex auth state injected by {@link provideConvexAuth}.
|
|
16
|
+
*
|
|
17
|
+
* Reads the current `isLoading` and `isAuthenticated` values. Throws if
|
|
18
|
+
* `provideConvexAuth` has not been called in an ancestor component.
|
|
19
|
+
*
|
|
20
|
+
* @returns The current {@link ConvexAuthState}.
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export declare function useConvexAuth(): ConvexAuthState;
|
|
25
|
+
/**
|
|
26
|
+
* Options for {@link provideConvexAuth}.
|
|
27
|
+
*/
|
|
28
|
+
export interface ConvexAuthProviderOptions {
|
|
29
|
+
client: ConvexVueClient;
|
|
30
|
+
useAuth: () => {
|
|
31
|
+
isLoading: MaybeRefOrGetter<boolean>;
|
|
32
|
+
isAuthenticated: MaybeRefOrGetter<boolean>;
|
|
33
|
+
fetchAccessToken: AuthTokenFetcher;
|
|
34
|
+
authVersion?: MaybeRefOrGetter<unknown>;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Wire an external auth provider into the Convex client and make auth state
|
|
39
|
+
* available to all descendant components via {@link useConvexAuth}.
|
|
40
|
+
*
|
|
41
|
+
* Call this in a top-level layout component's `setup` function, passing the
|
|
42
|
+
* auth provider's loading/authenticated flags and its token fetcher.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```vue
|
|
46
|
+
* <script setup lang="ts">
|
|
47
|
+
* import { useAuth } from '~/composables/useAuth' // your auth provider
|
|
48
|
+
*
|
|
49
|
+
* const { client } = useNuxtApp().$convex
|
|
50
|
+
* const authState = provideConvexAuth({ client, useAuth })
|
|
51
|
+
* </script>
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @param options - Auth integration options including the Convex client and
|
|
55
|
+
* an `useAuth` factory that returns loading state and a token fetcher.
|
|
56
|
+
* @returns The reactive auth state.
|
|
57
|
+
*
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export declare function provideConvexAuth(options: ConvexAuthProviderOptions): ConvexAuthState;
|
|
61
|
+
/**
|
|
62
|
+
* Build reactive {@link ConvexAuthState} and wire watchers between the
|
|
63
|
+
* external auth provider and the Convex client, without calling `provide()`.
|
|
64
|
+
*
|
|
65
|
+
* Use this when you need to install the state at the Nuxt app level
|
|
66
|
+
* (`nuxtApp.vueApp.provide`) rather than from a component's setup function.
|
|
67
|
+
*
|
|
68
|
+
* When `scope` is provided, all watchers are created inside that
|
|
69
|
+
* {@link EffectScope} so callers can dispose them later.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createConvexAuthState(options: ConvexAuthProviderOptions, scope?: EffectScope): ConvexAuthState;
|
|
72
|
+
/**
|
|
73
|
+
* Create a fresh {@link EffectScope} and build a Convex auth state inside it.
|
|
74
|
+
* The scope is returned so the caller can `.stop()` it on teardown.
|
|
75
|
+
*/
|
|
76
|
+
export declare function createScopedConvexAuthState(options: ConvexAuthProviderOptions): {
|
|
77
|
+
state: ConvexAuthState;
|
|
78
|
+
scope: EffectScope;
|
|
79
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { effectScope, inject, provide, ref, toValue, watch, watchEffect } from "vue";
|
|
2
|
+
export const ConvexAuthStateKey = Symbol("ConvexAuthState");
|
|
3
|
+
export function useConvexAuth() {
|
|
4
|
+
const authContext = inject(ConvexAuthStateKey);
|
|
5
|
+
if (authContext === void 0) {
|
|
6
|
+
throw new Error(
|
|
7
|
+
"Could not find Convex auth context. Ensure `provideConvexAuth` has been called in an ancestor component or that the Convex auth plugin is installed."
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
return authContext;
|
|
11
|
+
}
|
|
12
|
+
export function provideConvexAuth(options) {
|
|
13
|
+
const authState = createConvexAuthState(options);
|
|
14
|
+
provide(ConvexAuthStateKey, authState);
|
|
15
|
+
return authState;
|
|
16
|
+
}
|
|
17
|
+
export function createConvexAuthState(options, scope) {
|
|
18
|
+
const { client, useAuth } = options;
|
|
19
|
+
const {
|
|
20
|
+
isLoading: authProviderLoading,
|
|
21
|
+
isAuthenticated: authProviderAuthenticated,
|
|
22
|
+
fetchAccessToken,
|
|
23
|
+
authVersion: authProviderVersion
|
|
24
|
+
} = useAuth();
|
|
25
|
+
const isAuthProviderLoading = () => toValue(authProviderLoading);
|
|
26
|
+
const isAuthProviderAuthenticated = () => toValue(authProviderAuthenticated);
|
|
27
|
+
const currentAuthProviderVersion = () => toValue(authProviderVersion);
|
|
28
|
+
const isConvexAuthenticated = ref(null);
|
|
29
|
+
const authState = {
|
|
30
|
+
get isLoading() {
|
|
31
|
+
return isConvexAuthenticated.value === null;
|
|
32
|
+
},
|
|
33
|
+
get isAuthenticated() {
|
|
34
|
+
return isAuthProviderAuthenticated() && (isConvexAuthenticated.value ?? false);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const register = () => {
|
|
38
|
+
watch(
|
|
39
|
+
() => [
|
|
40
|
+
isAuthProviderLoading(),
|
|
41
|
+
isAuthProviderAuthenticated(),
|
|
42
|
+
isConvexAuthenticated.value
|
|
43
|
+
],
|
|
44
|
+
([loading, authenticated]) => {
|
|
45
|
+
if (loading && isConvexAuthenticated.value !== null) {
|
|
46
|
+
isConvexAuthenticated.value = null;
|
|
47
|
+
} else if (!loading && !authenticated && isConvexAuthenticated.value !== false) {
|
|
48
|
+
isConvexAuthenticated.value = false;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{ immediate: true }
|
|
52
|
+
);
|
|
53
|
+
watchEffect((onCleanup) => {
|
|
54
|
+
const providerLoading = isAuthProviderLoading();
|
|
55
|
+
const providerAuthenticated = isAuthProviderAuthenticated();
|
|
56
|
+
const authVersion = currentAuthProviderVersion();
|
|
57
|
+
void authVersion;
|
|
58
|
+
if (providerLoading || !providerAuthenticated) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
client.setAuth(fetchAccessToken, (backendIsAuthenticated) => {
|
|
62
|
+
isConvexAuthenticated.value = backendIsAuthenticated;
|
|
63
|
+
});
|
|
64
|
+
onCleanup(() => {
|
|
65
|
+
client.clearAuth();
|
|
66
|
+
isConvexAuthenticated.value = null;
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
if (scope) {
|
|
71
|
+
scope.run(register);
|
|
72
|
+
} else {
|
|
73
|
+
register();
|
|
74
|
+
}
|
|
75
|
+
return authState;
|
|
76
|
+
}
|
|
77
|
+
export function createScopedConvexAuthState(options) {
|
|
78
|
+
const scope = effectScope(true);
|
|
79
|
+
const state = createConvexAuthState(options, scope);
|
|
80
|
+
return { state, scope };
|
|
81
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useRuntimeConfig, useState } from "#app";
|
|
2
|
+
import { ConvexVueClient, ConvexClientKey } from "../client.js";
|
|
3
|
+
import { ConvexAuthStateKey, createScopedConvexAuthState } from "./index.js";
|
|
4
|
+
import { useAuth } from "./use-auth.js";
|
|
5
|
+
import { consumeCrossDomainOneTimeToken } from "./cross-domain.js";
|
|
6
|
+
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
7
|
+
const url = useRuntimeConfig().public.backend.url;
|
|
8
|
+
if (!url) {
|
|
9
|
+
console.warn("[nuxt-backend] No Convex URL configured for client plugin.");
|
|
10
|
+
return { provide: {} };
|
|
11
|
+
}
|
|
12
|
+
const client = new ConvexVueClient(url);
|
|
13
|
+
nuxtApp.vueApp.provide(ConvexClientKey, client);
|
|
14
|
+
await consumeCrossDomainOneTimeToken();
|
|
15
|
+
const initialToken = useState("backend:initialToken", () => null);
|
|
16
|
+
const { state, scope } = createScopedConvexAuthState({
|
|
17
|
+
client,
|
|
18
|
+
useAuth: () => useAuth(initialToken.value)
|
|
19
|
+
});
|
|
20
|
+
nuxtApp.vueApp.provide(ConvexAuthStateKey, state);
|
|
21
|
+
window.addEventListener("beforeunload", () => {
|
|
22
|
+
scope.stop();
|
|
23
|
+
client.close();
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
provide: {
|
|
27
|
+
convex: client
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ConvexVueClient } from '../client.js';
|
|
2
|
+
type ConvexNuxtInjection = {
|
|
3
|
+
convex?: ConvexVueClient;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Server-side Better Auth bootstrap.
|
|
7
|
+
*
|
|
8
|
+
* Mirrors the Next.js parity layer that calls `getToken()` from the root
|
|
9
|
+
* server layout and passes it as `initialToken` to `ConvexBetterAuthProvider`.
|
|
10
|
+
*
|
|
11
|
+
* The token is stashed into a Nuxt `useState('backend:initialToken')` so the
|
|
12
|
+
* client plugin can hand it to `useAuth(initialToken)` before the first
|
|
13
|
+
* Convex `setAuth` call — avoiding an extra Better Auth round-trip on first
|
|
14
|
+
* paint.
|
|
15
|
+
*/
|
|
16
|
+
declare const _default: import("#app").Plugin<ConvexNuxtInjection> & import("#app").ObjectPlugin<ConvexNuxtInjection>;
|
|
17
|
+
export default _default;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useRuntimeConfig, useState, useRequestEvent } from "#app";
|
|
2
|
+
import { ConvexVueClient, ConvexClientKey } from "../client.js";
|
|
3
|
+
import { backendAuth } from "../../nuxt/auth/server.js";
|
|
4
|
+
import { ConvexAuthStateKey } from "./index.js";
|
|
5
|
+
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
6
|
+
const initialToken = useState("backend:initialToken", () => null);
|
|
7
|
+
const url = useRuntimeConfig().public.backend.url;
|
|
8
|
+
let ssrClient;
|
|
9
|
+
if (url) {
|
|
10
|
+
ssrClient = new ConvexVueClient(url);
|
|
11
|
+
nuxtApp.vueApp.provide(ConvexClientKey, ssrClient);
|
|
12
|
+
}
|
|
13
|
+
const ssrAuthState = {
|
|
14
|
+
isLoading: true,
|
|
15
|
+
isAuthenticated: false
|
|
16
|
+
};
|
|
17
|
+
nuxtApp.vueApp.provide(ConvexAuthStateKey, ssrAuthState);
|
|
18
|
+
const event = useRequestEvent();
|
|
19
|
+
if (!event) {
|
|
20
|
+
return ssrClient ? { provide: { convex: ssrClient } } : { provide: {} };
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const token = await backendAuth(event).getToken();
|
|
24
|
+
initialToken.value = token ?? null;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.warn("[nuxt-backend] Failed to prefetch auth token for SSR:", error);
|
|
27
|
+
}
|
|
28
|
+
return ssrClient ? { provide: { convex: ssrClient } } : { provide: {} };
|
|
29
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { AuthTokenFetcher } from 'convex/browser';
|
|
2
|
+
import { type ComputedRef } from 'vue';
|
|
3
|
+
import { type AuthClient } from './client.js';
|
|
4
|
+
declare const useClientSession: () => Readonly<import("vue").Ref<{
|
|
5
|
+
readonly data: {
|
|
6
|
+
readonly user: {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly createdAt: Date;
|
|
9
|
+
readonly updatedAt: Date;
|
|
10
|
+
readonly email: string;
|
|
11
|
+
readonly emailVerified: boolean;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly image?: string | null | undefined;
|
|
14
|
+
readonly userId?: string | null | undefined;
|
|
15
|
+
};
|
|
16
|
+
readonly session: {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly createdAt: Date;
|
|
19
|
+
readonly updatedAt: Date;
|
|
20
|
+
readonly userId: string;
|
|
21
|
+
readonly expiresAt: Date;
|
|
22
|
+
readonly token: string;
|
|
23
|
+
readonly ipAddress?: string | null | undefined;
|
|
24
|
+
readonly userAgent?: string | null | undefined;
|
|
25
|
+
};
|
|
26
|
+
} | null;
|
|
27
|
+
readonly isPending: boolean;
|
|
28
|
+
readonly isRefetching: boolean;
|
|
29
|
+
readonly error: import("@better-fetch/fetch").BetterFetchError | null;
|
|
30
|
+
readonly refetch: (queryParams?: {
|
|
31
|
+
query?: import("better-auth").SessionQueryParams;
|
|
32
|
+
} | undefined) => Promise<void>;
|
|
33
|
+
}, {
|
|
34
|
+
readonly data: {
|
|
35
|
+
readonly user: {
|
|
36
|
+
readonly id: string;
|
|
37
|
+
readonly createdAt: Date;
|
|
38
|
+
readonly updatedAt: Date;
|
|
39
|
+
readonly email: string;
|
|
40
|
+
readonly emailVerified: boolean;
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly image?: string | null | undefined;
|
|
43
|
+
readonly userId?: string | null | undefined;
|
|
44
|
+
};
|
|
45
|
+
readonly session: {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly createdAt: Date;
|
|
48
|
+
readonly updatedAt: Date;
|
|
49
|
+
readonly userId: string;
|
|
50
|
+
readonly expiresAt: Date;
|
|
51
|
+
readonly token: string;
|
|
52
|
+
readonly ipAddress?: string | null | undefined;
|
|
53
|
+
readonly userAgent?: string | null | undefined;
|
|
54
|
+
};
|
|
55
|
+
} | null;
|
|
56
|
+
readonly isPending: boolean;
|
|
57
|
+
readonly isRefetching: boolean;
|
|
58
|
+
readonly error: import("@better-fetch/fetch").BetterFetchError | null;
|
|
59
|
+
readonly refetch: (queryParams?: {
|
|
60
|
+
query?: import("better-auth").SessionQueryParams;
|
|
61
|
+
} | undefined) => Promise<void>;
|
|
62
|
+
}>>;
|
|
63
|
+
export type AuthSession = ReturnType<typeof useClientSession>;
|
|
64
|
+
export interface UseAuthService {
|
|
65
|
+
client: AuthClient;
|
|
66
|
+
session: AuthSession;
|
|
67
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
68
|
+
isLoading: ComputedRef<boolean>;
|
|
69
|
+
fetchAccessToken: AuthTokenFetcher;
|
|
70
|
+
authVersion: ComputedRef<string | null>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Unified Better Auth service for the Vue/Nuxt runtime.
|
|
74
|
+
*
|
|
75
|
+
* Returns the full Better Auth client, the reactive session wrapper, and the
|
|
76
|
+
* Convex-compatible auth state used by the packaged auth plugin.
|
|
77
|
+
*
|
|
78
|
+
* @param initialToken - Optional preloaded token, used once per app lifetime
|
|
79
|
+
* to avoid a round-trip on initial load (e.g. from SSR).
|
|
80
|
+
*/
|
|
81
|
+
export declare function useAuth(initialToken?: string | null): UseAuthService;
|
|
82
|
+
/** Reset module-level token cache — intended for tests only. */
|
|
83
|
+
export declare function __resetUseAuthForTests(): void;
|
|
84
|
+
export {};
|