@vite-hub/auth 0.1.0 → 0.3.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.
- package/README.md +333 -49
- package/dist/abilities-DfAnFYwG.d.ts +93 -0
- package/dist/abilities.d.ts +2 -0
- package/dist/abilities.js +43 -0
- package/dist/ability-error-CqgBrBfR.js +91 -0
- package/dist/ability-error-DcgChwCN.d.ts +34 -0
- package/dist/admin-claim-code-CDipqeAU.js +22 -0
- package/dist/agent.d.ts +5 -5
- package/dist/agent.js +17 -9
- package/dist/application-path-DuTpfGFj.js +62 -0
- package/dist/auth-instance-CR6b1W80.js +1168 -0
- package/dist/bun-sqlite.d.ts +8 -0
- package/dist/cli-ubYJpAL_.d.ts +163 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +47 -7
- package/dist/console-runtime-DTopBP-x.d.ts +71 -0
- package/dist/console-runtime-J9_Vx9pT.js +1166 -0
- package/dist/console-runtime.d.ts +3 -3
- package/dist/console-runtime.js +2 -819
- package/dist/console.d.ts +215 -3
- package/dist/console.js +1 -1
- package/dist/database-options-BRNef9oy.js +29 -0
- package/dist/database-schema-options-ENuRMduM.js +12 -0
- package/dist/database.d.ts +12 -0
- package/dist/database.js +45 -0
- package/dist/definition-CqQMcDEK.js +280 -0
- package/dist/error-diagnostics-C8XtVz0i.js +138 -0
- package/dist/{events-P7mI4IQb.js → events-ByzenEVj.js} +122 -4
- package/dist/forward.d.ts +50 -0
- package/dist/forward.js +120 -0
- package/dist/host-declarations.d.ts +2 -1
- package/dist/index.d.ts +24 -3
- package/dist/index.js +3 -102
- package/dist/internal/server.d.ts +5 -0
- package/dist/internal/server.js +858 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/mcp.d.ts +187 -0
- package/dist/mcp.js +353 -0
- package/dist/members-D9lfaEO_.js +533 -0
- package/dist/nuxt-bridge-Cl988STZ.js +44 -0
- package/dist/nuxt.d.ts +13 -5
- package/dist/nuxt.js +90 -13
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/page-tools-shared-l4eyj53D.js +19 -0
- package/dist/presets.js +1 -1
- package/dist/request-z6DHHmrb.js +47 -0
- package/dist/runtime/console.d.ts +3 -7
- package/dist/runtime/console.js +4 -42
- package/dist/runtime/empty-abilities.d.ts +5 -0
- package/dist/runtime/empty-abilities.js +4 -0
- package/dist/runtime/empty-client.d.ts +6 -0
- package/dist/runtime/empty-client.js +5 -0
- package/dist/runtime/empty-definition.d.ts +1 -1
- package/dist/runtime/nuxt.js +1 -1
- package/dist/runtime/server.d.ts +2 -0
- package/dist/runtime/server.js +77 -0
- package/dist/runtime/vue-nuxt.d.ts +3 -2
- package/dist/runtime/vue-nuxt.js +157 -5
- package/dist/runtime-state-ojQCzKOc.js +25 -0
- package/dist/schema.d.ts +137 -0
- package/dist/schema.js +147 -0
- package/dist/server-C0qnKN2W.d.ts +143 -0
- package/dist/server-bQGzVQ--.d.ts +41 -0
- package/dist/server.d.ts +3 -2
- package/dist/server.js +3 -3
- package/dist/session-cookies-BbH3UR2m.js +227 -0
- package/dist/{set-cookie-2as_DbHe.js → set-cookie-C8NOhdRE.js} +27 -12
- package/dist/{shared-DnpgYiLF.js → shared-BwcdRGxD.js} +2 -5
- package/dist/table-specs-8gq_FCaq.js +771 -0
- package/dist/table-specs-DbXyaXWs.d.ts +842 -0
- package/dist/test.d.ts +26 -0
- package/dist/test.js +25 -0
- package/dist/{events-BkmK2dFY.d.ts → types-CNaeH2Fd.d.ts} +240 -157
- package/dist/vite-CV400XXY.js +1556 -0
- package/dist/vite.d.ts +138 -20
- package/dist/vite.js +2 -2
- package/dist/vue.d.ts +330 -21
- package/dist/vue.js +583 -36
- package/package.json +84 -15
- package/dist/application-path-C290OAIs.js +0 -18
- package/dist/cli-mgXvsBtA.d.ts +0 -85
- package/dist/console-B7eERmGv.d.ts +0 -180
- package/dist/console-runtime-bZQtnX2Y.d.ts +0 -39
- package/dist/error-diagnostics-CL0yjHiM.js +0 -79
- package/dist/events-config-CjcFOhTE.js +0 -177
- package/dist/inspect-DPtsW8dB.d.ts +0 -36
- package/dist/server-B7LU19dS.js +0 -1730
- package/dist/server-CTD_JAww.d.ts +0 -77
- package/dist/vite-COWkwxsj.js +0 -990
package/dist/vite.d.ts
CHANGED
|
@@ -1,46 +1,164 @@
|
|
|
1
1
|
/// <reference path="./host-declarations.d.ts" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { n as ViteHubDefinitionSummary, r as ViteHubInspectionPluginMetadata } from "./inspect-DPtsW8dB.js";
|
|
2
|
+
import { S as AuthOptions, V as ResolvedAuthViteConfig } from "./types-CNaeH2Fd.js";
|
|
3
|
+
import { n as ViteHubCliPluginMetadata } from "./cli-ubYJpAL_.js";
|
|
5
4
|
import { Plugin } from "vite";
|
|
5
|
+
//#region ../internal/src/inspect.d.ts
|
|
6
|
+
interface ViteHubDefinitionField {
|
|
7
|
+
label: string;
|
|
8
|
+
value: string;
|
|
9
|
+
}
|
|
10
|
+
/** Serializable summary of one discovered Definition. Files are relative to the project root. */
|
|
11
|
+
interface ViteHubDefinitionSummary {
|
|
12
|
+
fields: readonly ViteHubDefinitionField[];
|
|
13
|
+
file: string;
|
|
14
|
+
name: string;
|
|
15
|
+
source: string;
|
|
16
|
+
}
|
|
17
|
+
/** Lists the Definitions that one owner package discovered for the resolved Vite config. */
|
|
18
|
+
interface ViteHubDefinitionInspector {
|
|
19
|
+
/** Stable singular kind, for example `queue` or `rate-limit`. */
|
|
20
|
+
kind: string;
|
|
21
|
+
label: string;
|
|
22
|
+
list: () => Promise<readonly ViteHubDefinitionSummary[]> | readonly ViteHubDefinitionSummary[];
|
|
23
|
+
}
|
|
24
|
+
/** One generated Provider Output file or directory that an owner package can write. */
|
|
25
|
+
interface ViteHubProviderOutputEntry {
|
|
26
|
+
description: string;
|
|
27
|
+
owner: string;
|
|
28
|
+
/** Absolute path. */
|
|
29
|
+
path: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Where an inspected resource id came from. `name` means the host binds the resource by its name and needs no id.
|
|
33
|
+
* The CLI sets `provider-output` when only a generated Wrangler config names the id.
|
|
34
|
+
*/
|
|
35
|
+
type ViteHubResourceIdSource = "config" | "env" | "name" | "provider-output" | "provision-state";
|
|
36
|
+
/** One host resource that a Definition or store maps to. Owner packages read only local config and Provision State. */
|
|
37
|
+
interface ViteHubResourceSummary {
|
|
38
|
+
/** Cloudflare binding name, so the CLI can find the id in generated Wrangler config. */
|
|
39
|
+
binding?: string;
|
|
40
|
+
/** Definition or store name. */
|
|
41
|
+
definition: string;
|
|
42
|
+
/** Env variable that supplied the id when `source` is `env`. */
|
|
43
|
+
env?: string;
|
|
44
|
+
host: "cloudflare" | "vercel";
|
|
45
|
+
id?: string;
|
|
46
|
+
/** Same resource kind names as Provision actions, for example `cloudflare-d1`. */
|
|
47
|
+
kind: string;
|
|
48
|
+
/** Resource name on the host, when the config names it. */
|
|
49
|
+
name?: string;
|
|
50
|
+
/** Explains how the host finds the resource when no id or name is known locally. */
|
|
51
|
+
note?: string;
|
|
52
|
+
owner: string;
|
|
53
|
+
/** Absent when no id is known yet. */
|
|
54
|
+
source?: ViteHubResourceIdSource;
|
|
55
|
+
}
|
|
56
|
+
interface ViteHubInspectionContributor {
|
|
57
|
+
definitions?: readonly ViteHubDefinitionInspector[];
|
|
58
|
+
providerOutput?: readonly ViteHubProviderOutputEntry[];
|
|
59
|
+
resources?: () => readonly ViteHubResourceSummary[] | Promise<readonly ViteHubResourceSummary[]>;
|
|
60
|
+
}
|
|
61
|
+
type ViteHubInspectionContributorFactory = () => ViteHubInspectionContributor | undefined | Promise<ViteHubInspectionContributor | undefined>;
|
|
62
|
+
interface ViteHubInspectionPluginMetadata {
|
|
63
|
+
inspect?: ViteHubInspectionContributor | ViteHubInspectionContributorFactory;
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
6
66
|
//#region src/config.d.ts
|
|
7
|
-
export declare function resolveAuthViteConfig(options?:
|
|
67
|
+
export declare function resolveAuthViteConfig(options?: AuthOptions, rootDir?: string, discovery?: {
|
|
8
68
|
serverDirs?: string[];
|
|
9
69
|
}): ResolvedAuthViteConfig | undefined;
|
|
10
70
|
//#endregion
|
|
71
|
+
//#region ../internal/src/deployment.d.ts
|
|
72
|
+
/** The host that ViteHub builds for: the `host` option of `vitehub()` and the value of `VITEHUB_HOST`. */
|
|
73
|
+
type ViteHubHost = "cloudflare" | "deno" | "netlify" | "node" | "vercel";
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region ../internal/src/build/deployment-plan-output.d.ts
|
|
76
|
+
/**
|
|
77
|
+
* One set of host variables that the deployed app reads, as `deployment.json` lists it: a Server Env declaration, or
|
|
78
|
+
* a variable that a primitive reads itself, such as `VITEHUB_SECRET` or `BLOB_READ_WRITE_TOKEN`.
|
|
79
|
+
* It holds names only, so `vitehub deploy` can check the host without booting Vite. It never holds a value.
|
|
80
|
+
*/
|
|
81
|
+
interface DeploymentEnvEntry {
|
|
82
|
+
/** The declaration path, such as `env.server.github.token`, when it is safe to show. Primitive entries have none. */
|
|
83
|
+
path?: string;
|
|
84
|
+
/** The host variable names, in read order. The first name that is set supplies the value. */
|
|
85
|
+
names: readonly string[];
|
|
86
|
+
/** Any variable whose name starts with this prefix supplies the value, such as `VITEHUB_CONSOLE_KEY_`. */
|
|
87
|
+
prefix?: string;
|
|
88
|
+
required: boolean;
|
|
89
|
+
secret: boolean;
|
|
90
|
+
/** A default supplies the value when no name is set. */
|
|
91
|
+
hasDefault: boolean;
|
|
92
|
+
/** The primitives that read these names, such as `blob`. Server Env declarations have none. */
|
|
93
|
+
owners?: readonly string[];
|
|
94
|
+
}
|
|
95
|
+
/** What a primitive needs to know to name the variables it reads in the deployed app. */
|
|
96
|
+
interface DeploymentEnvContext {
|
|
97
|
+
host: ViteHubHost;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The host variables that a primitive reads itself, outside the Server Env registry, for one host and its resolved
|
|
101
|
+
* config. A Vite plugin exposes it as `api.deploymentEnv`, and the build writes the result to `deployment.json`.
|
|
102
|
+
* Return names only, never values.
|
|
103
|
+
*/
|
|
104
|
+
type DeploymentEnvContribution = (context: DeploymentEnvContext) => readonly DeploymentEnvEntry[];
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region ../internal/src/contributions.d.ts
|
|
107
|
+
/** Roots that ViteHub passes to every contribution factory. */
|
|
108
|
+
interface ContributionContext {
|
|
109
|
+
/** Vite root. */
|
|
110
|
+
root: string;
|
|
111
|
+
/** ViteHub project root. It can differ from the Vite root, for example in a Nuxt app with a `srcDir`. */
|
|
112
|
+
projectRoot: string;
|
|
113
|
+
/** Server directories that the host forwards, for example Nuxt's `serverDir` and the server directories of layers. */
|
|
114
|
+
serverDirs?: readonly string[];
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
11
117
|
//#region src/vite.d.ts
|
|
12
|
-
export declare
|
|
118
|
+
export declare const AUTH_ABILITIES_ID = "#vitehub/auth/abilities";
|
|
119
|
+
export declare const AUTH_CLIENT_ID = "#vitehub/auth/client";
|
|
13
120
|
export declare const AUTH_DEFINITION_ID = "#vitehub/auth/definition";
|
|
14
121
|
export declare const AUTH_SERVER_ID = "#vitehub/auth/server";
|
|
15
122
|
export declare const AUTH_VITE_PLUGIN_NAME = "@vite-hub/auth/vite";
|
|
16
123
|
export interface AuthVitePluginAPI {
|
|
124
|
+
/** The variables that Auth reads on the deployed host. See `authDeploymentEnv()`. */
|
|
125
|
+
deploymentEnv: DeploymentEnvContribution;
|
|
17
126
|
getConfig: () => ResolvedAuthViteConfig | undefined;
|
|
18
127
|
refresh: () => ResolvedAuthViteConfig | undefined;
|
|
19
128
|
}
|
|
129
|
+
/** The roots that the Database integration passes to `vitehub.databases`. */
|
|
130
|
+
export interface AuthDatabaseContributionContext extends ContributionContext {
|
|
131
|
+
/** The root that Database Definitions are discovered from. */
|
|
132
|
+
rootDir: string;
|
|
133
|
+
plugins?: readonly unknown[];
|
|
134
|
+
}
|
|
135
|
+
/** The Better Auth tables that Auth adds to the Database that `authDatabase()` names. */
|
|
136
|
+
export interface AuthDatabaseContribution {
|
|
137
|
+
name: "auth";
|
|
138
|
+
database: string;
|
|
139
|
+
dialect: "postgresql" | "sqlite";
|
|
140
|
+
/** The schema module of the package, or a generated module when the Definition adds plugins or fields. */
|
|
141
|
+
definition: {
|
|
142
|
+
module: string;
|
|
143
|
+
export: "authPostgresSchema" | "authSqliteSchema";
|
|
144
|
+
};
|
|
145
|
+
/** The schema keys and SQL names of the tables, so the Database lists them without loading the schema module. */
|
|
146
|
+
tables: Readonly<Record<string, string>>;
|
|
147
|
+
reason: string;
|
|
148
|
+
}
|
|
20
149
|
export type AuthVitePlugin = Plugin & {
|
|
21
150
|
api: AuthVitePluginAPI;
|
|
22
|
-
vitehub: ViteHubInspectionPluginMetadata & ViteHubCliPluginMetadata
|
|
151
|
+
vitehub: ViteHubInspectionPluginMetadata & ViteHubCliPluginMetadata & {
|
|
152
|
+
databases: (context: AuthDatabaseContributionContext) => Promise<AuthDatabaseContribution[]>;
|
|
153
|
+
};
|
|
23
154
|
};
|
|
24
|
-
export interface AuthInspectionOptions {
|
|
25
|
-
projectRoot: string;
|
|
26
|
-
rootDir: string;
|
|
27
|
-
serverDirs?: string[];
|
|
28
|
-
}
|
|
29
|
-
/** Lists the Auth Definition as a serializable inspection summary. */
|
|
30
|
-
export declare function inspectAuthDefinitions(options: AuthInspectionOptions): ViteHubDefinitionSummary[];
|
|
31
155
|
export declare function createAuthNitroConfig(plugin: AuthVitePlugin, options: {
|
|
32
156
|
nitro: Record<string, unknown>;
|
|
33
157
|
projectRoot: string;
|
|
34
158
|
serverDirs?: string[];
|
|
35
|
-
viteAuth?: AuthModuleOptions;
|
|
36
159
|
}): Record<string, unknown>;
|
|
37
160
|
interface InternalAuthModuleOptions {
|
|
38
161
|
importBase?: string;
|
|
39
162
|
}
|
|
40
|
-
export declare function hubAuth(options?:
|
|
41
|
-
declare module "vite" {
|
|
42
|
-
interface UserConfig {
|
|
43
|
-
auth?: AuthModuleOptions;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
163
|
+
export declare function hubAuth(options?: AuthOptions, internalOptions?: InternalAuthModuleOptions): AuthVitePlugin;
|
|
46
164
|
//#endregion
|
package/dist/vite.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { AUTH_DEFINITION_ID, AUTH_SERVER_ID, AUTH_VITE_PLUGIN_NAME, createAuthNitroConfig, hubAuth,
|
|
1
|
+
import { a as AUTH_VITE_PLUGIN_NAME, c as resolveAuthViteConfig, i as AUTH_SERVER_ID, n as AUTH_CLIENT_ID, o as createAuthNitroConfig, r as AUTH_DEFINITION_ID, s as hubAuth, t as AUTH_ABILITIES_ID } from "./vite-CV400XXY.js";
|
|
2
|
+
export { AUTH_ABILITIES_ID, AUTH_CLIENT_ID, AUTH_DEFINITION_ID, AUTH_SERVER_ID, AUTH_VITE_PLUGIN_NAME, createAuthNitroConfig, hubAuth, resolveAuthViteConfig };
|
package/dist/vue.d.ts
CHANGED
|
@@ -1,25 +1,190 @@
|
|
|
1
1
|
/// <reference path="./host-declarations.d.ts" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { b as AuthFor, g as AuthDefinition } from "./types-CNaeH2Fd.js";
|
|
3
|
+
import { a as AbilityRefusal, i as AbilityName, t as AbilityArgs } from "./abilities-DfAnFYwG.js";
|
|
4
|
+
import { n as AbilityErrorData } from "./ability-error-DcgChwCN.js";
|
|
5
|
+
import { BetterAuthClientOptions, SessionQueryParams } from "better-auth";
|
|
6
|
+
import { ComputedRef, DeepReadonly, Ref, VNode } from "vue";
|
|
7
|
+
import { inferAdditionalFields } from "better-auth/client/plugins";
|
|
4
8
|
import { BetterFetchError, VueAuthClient, VueAuthClient as VueAuthClient$1, createAuthClient as createAuthClient$1 } from "better-auth/vue";
|
|
5
|
-
//#region src/
|
|
6
|
-
|
|
9
|
+
//#region src/nuxt-bridge.d.ts
|
|
10
|
+
/** The members of the Nuxt app that the composables read. */
|
|
11
|
+
type AuthNuxtApp = {
|
|
7
12
|
runWithContext<T>(run: () => T): T;
|
|
13
|
+
isHydrating?: boolean;
|
|
14
|
+
$vitehubAuthClient?: unknown;
|
|
15
|
+
};
|
|
16
|
+
/** What `authorize()` passes to Nuxt's `createError()`: the refusal, as the server sends it, and `fatal`. */
|
|
17
|
+
type AuthNuxtErrorInput = {
|
|
18
|
+
status: number;
|
|
19
|
+
statusText: string;
|
|
20
|
+
message: string;
|
|
21
|
+
data: AbilityErrorData;
|
|
22
|
+
fatal: true;
|
|
23
|
+
};
|
|
24
|
+
/** The fetch of one server request, which Better Auth's client uses for the calls of that request. */
|
|
25
|
+
interface RequestFetch {
|
|
26
|
+
/** The same object for every call of one request, such as the Nuxt app of the request. */
|
|
27
|
+
request: object;
|
|
28
|
+
/** The origin of the request. On the server, Better Auth's client needs an absolute URL. */
|
|
29
|
+
origin: string;
|
|
30
|
+
/** Calls the app in-process with the headers of the request, such as the cookie. */
|
|
31
|
+
fetch: typeof fetch;
|
|
32
|
+
}
|
|
33
|
+
/** Internal bridge installed by the Nuxt module, without making `#app` a dependency of the Vue entry. */
|
|
34
|
+
export declare function setNuxtAppResolver(resolver: (() => AuthNuxtApp | undefined) | undefined): void;
|
|
35
|
+
/** Internal bridge installed by the Nuxt module: Nuxt's `createError()`, so a refusal of `authorize()` shows the error page. */
|
|
36
|
+
export declare function setNuxtErrorFactory(factory: ((input: AuthNuxtErrorInput) => Error) | undefined): void;
|
|
37
|
+
/**
|
|
38
|
+
* Internal bridge installed by the Nuxt module in the browser. `signOut()` of `useUserSession()` calls it after a
|
|
39
|
+
* sign-out. It forgets the session that the server read, and opens `redirectTo`, or the login page when the page on
|
|
40
|
+
* screen is only for signed-in visitors.
|
|
41
|
+
*/
|
|
42
|
+
export declare function setSignOutHandler(handler: ((redirectTo: string | undefined) => unknown) | undefined): void;
|
|
43
|
+
/**
|
|
44
|
+
* Internal bridge installed by the Nuxt module: the path of Better Auth for the default `authClient`, such as
|
|
45
|
+
* `/app/auth` for `defineAuth({ basePath: '/auth' })` below Nuxt `app.baseURL` `/app/`. The client reads it when it
|
|
46
|
+
* loads Better Auth. Without it, the client uses `/api/auth` below the base path of the build.
|
|
47
|
+
*/
|
|
48
|
+
export declare function setDefaultAuthBasePath(path: string | undefined): void;
|
|
49
|
+
/**
|
|
50
|
+
* Internal bridge installed by the Nuxt module. The resolver returns the session that the server read for this
|
|
51
|
+
* request, `null` when the request had none, or `undefined` when nobody knows yet, such as on a prerendered page.
|
|
52
|
+
* With `read` on the server, it reads the session if the request did not read it yet, and can return a promise of it.
|
|
53
|
+
*/
|
|
54
|
+
export declare function setSessionSeedResolver(resolver: ((read: boolean) => unknown) | undefined): void;
|
|
55
|
+
/**
|
|
56
|
+
* Internal bridge installed by the Nuxt module. On the server, the resolver returns the fetch of the current request, or
|
|
57
|
+
* `undefined` outside a request.
|
|
58
|
+
*/
|
|
59
|
+
export declare function setRequestFetchResolver(resolver: (() => RequestFetch | undefined) | undefined): void;
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/vue.d.ts
|
|
62
|
+
/**
|
|
63
|
+
* The members of a client that the composables read. Every Better Auth Vue client has them. `VueAuthClient<any>`
|
|
64
|
+
* rejects a client with a required additional field, because its `signUp.email()` needs that field.
|
|
65
|
+
*/
|
|
66
|
+
type AuthClientLike = Pick<VueAuthClient$1<{}>, "$fetch" | "getSession"> & {
|
|
67
|
+
$Infer: {
|
|
68
|
+
Session: {
|
|
69
|
+
session: object;
|
|
70
|
+
user: object;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
signIn: unknown;
|
|
74
|
+
signOut: unknown;
|
|
75
|
+
signUp: unknown;
|
|
76
|
+
useSession: () => unknown;
|
|
8
77
|
};
|
|
9
|
-
/** Internal bridge installed by the Nuxt module, without making `#app` a dependency of this entry. */
|
|
10
|
-
export declare function setNuxtAppResolver(resolver: (() => NuxtApp | undefined) | undefined): void;
|
|
11
78
|
/**
|
|
12
79
|
* Better Auth's Vue client with the same options, types, and calls, which loads `better-auth/vue` the first time it is
|
|
13
80
|
* used. Creating one costs nothing, so an app can create it up front and only pages that sign in pay for Better Auth.
|
|
14
81
|
* Calls such as `signIn.social()` wait for the load. Hooks such as `useSession()` return a ref at once that is
|
|
15
|
-
* pending until then
|
|
82
|
+
* pending until then. Every `useSession()` call of one client in one app returns the same ref. In Nuxt, a call in a
|
|
83
|
+
* component starts with the session that the server read for the request, so it is settled during server rendering and
|
|
84
|
+
* hydration. The browser then loads Better Auth and reads the session again only when the session can change: a call,
|
|
85
|
+
* `refetch()`, the tab shows again, the network comes back, another tab signs out or updates the user, or the session
|
|
86
|
+
* expires. Use `loadAuthClient()` for anything else, such as `$store`. Without `basePath`, it uses the same default
|
|
87
|
+
* as `authClient`: in Nuxt, the Auth Definition's `basePath` below `app.baseURL`.
|
|
88
|
+
* During Nuxt server rendering, a call such as `listSessions()` reaches the app in-process with the visitor's cookie.
|
|
16
89
|
*/
|
|
17
90
|
export declare const createAuthClient: typeof createAuthClient$1;
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Better Auth's own Vue client behind one that `createAuthClient()` returned, once it has loaded. During Nuxt server
|
|
93
|
+
* rendering, it is the client of the current request.
|
|
94
|
+
*/
|
|
95
|
+
export declare function loadAuthClient<Client extends AuthClientLike>(client: Client): Promise<Client>;
|
|
96
|
+
/**
|
|
97
|
+
* The options of the app's Auth client, such as Better Auth's client plugins. Export them from `app/auth.client.ts` in
|
|
98
|
+
* a Nuxt app, or from `auth.client.ts` in the Vite root, and the default client, `useUserSession()`, and the Nuxt page
|
|
99
|
+
* guard use them.
|
|
100
|
+
*/
|
|
101
|
+
export declare function defineAuthClient<Option extends BetterAuthClientOptions>(options: Option): Option;
|
|
102
|
+
declare global {
|
|
103
|
+
namespace ViteHub {
|
|
104
|
+
/** Generated declarations set `client` to the type of the options in the app's `auth.client.ts`. */
|
|
105
|
+
interface AuthRegistry {}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** The client options that generated declarations register from the app's `auth.client.ts`. Without one, none. */
|
|
109
|
+
type RegisteredClientOptions<TRegistry> = TRegistry extends {
|
|
110
|
+
client: infer TOptions;
|
|
111
|
+
} ? 0 extends 1 & TOptions ? {} : TOptions extends BetterAuthClientOptions ? TOptions : {} : {};
|
|
112
|
+
/** The fields that a server plugin's `schema` adds to one table. A plugin without a `schema`, such as `bearer()`, adds none. */
|
|
113
|
+
type ServerPluginTableFields<TSchema, TTable extends "session" | "user"> = TSchema extends { [Key in TTable]: {
|
|
114
|
+
fields: infer TFields extends Record<string, unknown>;
|
|
115
|
+
}; } ? TFields : {};
|
|
116
|
+
/**
|
|
117
|
+
* The endpoints of a server plugin that change the session response, such as the `getSession` of `customSession()`.
|
|
118
|
+
* Better Auth marks them with `CUSTOM_SESSION`. The client keeps no other plugin endpoint.
|
|
119
|
+
*/
|
|
120
|
+
type ServerPluginSessionEndpoints<TEndpoints> = { [Key in keyof TEndpoints as TEndpoints[Key] extends {
|
|
121
|
+
options: {
|
|
122
|
+
metadata: {
|
|
123
|
+
CUSTOM_SESSION: boolean;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
} ? Key : never]: TEndpoints[Key]; };
|
|
127
|
+
/**
|
|
128
|
+
* A type-only client plugin with the user and session fields of one server plugin, such as the `role` of `admin()`,
|
|
129
|
+
* and the session response of `customSession()`. It keeps only these, so every server plugin gives a valid client plugin.
|
|
130
|
+
*/
|
|
131
|
+
type ServerPluginFields<TPlugin> = TPlugin extends {
|
|
132
|
+
id: infer Id extends string;
|
|
133
|
+
schema?: infer Schema;
|
|
134
|
+
endpoints?: infer Endpoints;
|
|
135
|
+
} ? {
|
|
136
|
+
id: Id;
|
|
137
|
+
$InferServerPlugin: {
|
|
138
|
+
endpoints: ServerPluginSessionEndpoints<Endpoints>;
|
|
139
|
+
schema: {
|
|
140
|
+
session: {
|
|
141
|
+
fields: ServerPluginTableFields<Schema, "session">;
|
|
142
|
+
};
|
|
143
|
+
user: {
|
|
144
|
+
fields: ServerPluginTableFields<Schema, "user">;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
} : never;
|
|
149
|
+
/**
|
|
150
|
+
* Client options with type-only plugins: Better Auth's `inferAdditionalFields()` for `user.additionalFields` and
|
|
151
|
+
* `session.additionalFields`, and the fields of each server plugin.
|
|
152
|
+
*/
|
|
153
|
+
type ServerFieldsClientOptions<TAuth extends {
|
|
154
|
+
options: {
|
|
155
|
+
plugins?: readonly unknown[];
|
|
156
|
+
};
|
|
157
|
+
}> = {
|
|
158
|
+
plugins: Array<ReturnType<typeof inferAdditionalFields<TAuth>> | ServerPluginFields<NonNullable<TAuth["options"]["plugins"]>[number]>>;
|
|
159
|
+
};
|
|
160
|
+
/** The plugins of client options, such as the `adminClient()` of `auth.client.ts`. */
|
|
161
|
+
type ClientOptionsPlugins<TOptions> = TOptions extends {
|
|
162
|
+
plugins?: infer TPlugins;
|
|
163
|
+
} ? TPlugins extends readonly (infer TPlugin)[] ? TPlugin : never : never;
|
|
164
|
+
/** The type-only plugins with the fields of the registered Auth Definition and its server plugins. Without one, none. */
|
|
165
|
+
type RegisteredServerFieldsPlugins<TRegistry> = TRegistry extends {
|
|
166
|
+
definition: infer TDefinition extends AuthDefinition;
|
|
167
|
+
} ? 0 extends 1 & TDefinition ? never : ServerFieldsClientOptions<AuthFor<TDefinition>>["plugins"][number] : never;
|
|
168
|
+
/**
|
|
169
|
+
* The type of the default client: Better Auth's Vue client with the options of the app's `auth.client.ts`. Generated
|
|
170
|
+
* declarations also register the app's Auth Definition, so `user` and `session` have the fields that the Definition
|
|
171
|
+
* and its plugins add. Without them, it has the plain Better Auth fields.
|
|
172
|
+
*/
|
|
173
|
+
export type RegisteredAuthClient<TRegistry = ViteHub.AuthRegistry> = ServerFieldsClient<Omit<RegisteredClientOptions<TRegistry>, "plugins"> & {
|
|
174
|
+
plugins: Array<ClientOptionsPlugins<RegisteredClientOptions<TRegistry>> | RegisteredServerFieldsPlugins<TRegistry>>;
|
|
175
|
+
}>;
|
|
176
|
+
/** A client that Better Auth cannot type with these options keeps the plain fields, so `user` never becomes `never`. */
|
|
177
|
+
type ServerFieldsClient<TOptions> = TOptions extends BetterAuthClientOptions ? VueAuthClient$1<TOptions> : VueAuthClient$1<{}>;
|
|
178
|
+
/**
|
|
179
|
+
* The client that the composables and the Nuxt page guard share. On first use, it loads `better-auth/vue` and the app's
|
|
180
|
+
* `auth.client.ts` together, and the options of that file replace the defaults. In Nuxt, the default `basePath` is the
|
|
181
|
+
* Auth Definition's `basePath` below the `app.baseURL` of the page, also when `NUXT_APP_BASE_URL` sets it at runtime.
|
|
182
|
+
*/
|
|
183
|
+
export declare const authClient: RegisteredAuthClient;
|
|
184
|
+
/** Provide the app's configured client to Auth composables and `<Can>` in descendant components. */
|
|
185
|
+
export declare function provideAuthClient(client: AuthClientLike): void;
|
|
21
186
|
export declare function useAuthClient(): typeof authClient;
|
|
22
|
-
export interface AuthSessionState<Client extends
|
|
187
|
+
export interface AuthSessionState<Client extends AuthClientLike = typeof authClient> {
|
|
23
188
|
data: Client["$Infer"]["Session"] | null;
|
|
24
189
|
error: BetterFetchError | null;
|
|
25
190
|
isPending: boolean;
|
|
@@ -28,28 +193,172 @@ export interface AuthSessionState<Client extends VueAuthClient$1<any> = typeof a
|
|
|
28
193
|
query?: SessionQueryParams;
|
|
29
194
|
}) => Promise<void>;
|
|
30
195
|
}
|
|
31
|
-
type AuthSessionRef<Client extends
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
196
|
+
type AuthSessionRef<Client extends AuthClientLike = typeof authClient> = DeepReadonly<Ref<AuthSessionState<Client>>>;
|
|
197
|
+
/**
|
|
198
|
+
* The session of `client`. Every call for the same client in the same app returns the same ref, so a list of
|
|
199
|
+
* components that call it costs one watcher. In Nuxt, the page guard reads the session into the same ref.
|
|
200
|
+
*/
|
|
201
|
+
export declare function useSession<Client extends AuthClientLike = typeof authClient>(client?: Client): AuthSessionRef<Client>;
|
|
202
|
+
type ClientSession<Client extends AuthClientLike> = Client["$Infer"]["Session"];
|
|
203
|
+
/** Better Auth's `signOut()` of `Client`. */
|
|
204
|
+
type ClientSignOut<Client extends AuthClientLike> = Extract<Client["signOut"], (...args: never) => unknown>;
|
|
205
|
+
/** The parameters of Better Auth's `signOut()` of `Client`. */
|
|
206
|
+
type SignOutParameters<Client extends AuthClientLike> = Parameters<ClientSignOut<Client>>;
|
|
207
|
+
/** The result of Better Auth's `signOut()` of `Client`. */
|
|
208
|
+
type SignOutResult<Client extends AuthClientLike> = ReturnType<ClientSignOut<Client>>;
|
|
209
|
+
/** The options of `signOut()` from `useUserSession()`: the options of Better Auth's `signOut()`, and `redirectTo`. */
|
|
210
|
+
export type SignOutOptions<Client extends AuthClientLike = typeof authClient> = NonNullable<SignOutParameters<Client>[0]> & {
|
|
211
|
+
/**
|
|
212
|
+
* The page to open after the sign-out. Without it, in Nuxt, a page that only signed-in visitors may open sends the
|
|
213
|
+
* visitor to the login page, as its page guard does.
|
|
214
|
+
*/
|
|
215
|
+
redirectTo?: string;
|
|
216
|
+
};
|
|
217
|
+
export interface UserSession<Client extends AuthClientLike = typeof authClient> {
|
|
218
|
+
/** The Auth client of this session, with the endpoints of its client plugins, such as `client.admin.listUsers()`. */
|
|
219
|
+
client: Client;
|
|
35
220
|
data: ComputedRef<ClientSession<Client> | null>;
|
|
36
221
|
error: ComputedRef<BetterFetchError | null>;
|
|
37
222
|
/** The id of the administrator who opened this session as `user`, or `null`. Better Auth's admin plugin sets it. */
|
|
38
223
|
impersonatedBy: ComputedRef<string | null>;
|
|
39
224
|
loggedIn: ComputedRef<boolean>;
|
|
40
225
|
pending: ComputedRef<boolean>;
|
|
226
|
+
/** `true` once the session was read. It stays `true` while a later read runs, and after a read that failed. */
|
|
41
227
|
ready: ComputedRef<boolean>;
|
|
42
|
-
|
|
228
|
+
/**
|
|
229
|
+
* Reads the session again, updates every ref, and returns the session, or `null` without one. Callers at the same
|
|
230
|
+
* time share one read. On the server, it returns the session that the server read for the request.
|
|
231
|
+
*/
|
|
232
|
+
refresh: () => Promise<ClientSession<Client> | null>;
|
|
43
233
|
session: ComputedRef<ClientSession<Client>["session"] | null>;
|
|
44
234
|
signIn: Client["signIn"];
|
|
45
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Signs out with Better Auth, sets the session to `null` at once, waits for the `onSignOut()` listeners, then opens
|
|
237
|
+
* the provider's sign-out page, such as for OpenID Connect logout, or else `redirectTo`. Returns Better Auth's
|
|
238
|
+
* result. When the sign-out fails, the session and the page stay. Better Auth gets `fetchOptions`, as its own
|
|
239
|
+
* `signOut()` does.
|
|
240
|
+
*/
|
|
241
|
+
signOut: (options?: SignOutOptions<Client>, fetchOptions?: SignOutParameters<Client>[1]) => SignOutResult<Client>;
|
|
46
242
|
signUp: Client["signUp"];
|
|
47
243
|
/** End the impersonation session and restore the administrator's own session. */
|
|
48
244
|
stopImpersonating: () => Promise<void>;
|
|
49
245
|
user: ComputedRef<ClientSession<Client>["user"] | null>;
|
|
50
246
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
247
|
+
/**
|
|
248
|
+
* The current user and session of `client`, with sign-in and sign-out actions. Every call for the same client in the
|
|
249
|
+
* same app returns the same object, so a list of components that call it costs nothing more.
|
|
250
|
+
*/
|
|
251
|
+
export declare function useUserSession<Client extends AuthClientLike = typeof authClient>(client?: Client): UserSession<Client>;
|
|
252
|
+
/**
|
|
253
|
+
* Calls `listener` in the browser after each `signOut()` of `useUserSession()`, once the server ended the session and
|
|
254
|
+
* before the next page opens. `signOut()` waits for it. Use it to forget data of the person, such as `clearSync()`.
|
|
255
|
+
* Returns a function that stops it.
|
|
256
|
+
*/
|
|
257
|
+
export declare function onSignOut(listener: () => unknown): () => void;
|
|
258
|
+
/** What `useAbility()` returns. */
|
|
259
|
+
export interface Ability {
|
|
260
|
+
/**
|
|
261
|
+
* Whether a rule from `shared/abilities.ts` allows the visitor: `undefined` until the session is known. The arguments
|
|
262
|
+
* are passed as they are, so load the data before you ask.
|
|
263
|
+
*/
|
|
264
|
+
can: <const Name extends AbilityName>(name: Name, ...args: AbilityArgs<Name>) => boolean | undefined;
|
|
265
|
+
/** Whether a rule refuses the visitor: the opposite of `can()`, and `undefined` until the session is known. */
|
|
266
|
+
cannot: <const Name extends AbilityName>(name: Name, ...args: AbilityArgs<Name>) => boolean | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* Checks a rule and throws when it refuses, with the status and the `{ code, details }` data that `requireAbility()`
|
|
269
|
+
* sends. In Nuxt, the error is fatal, so the page shows the error page. In the browser, except while Nuxt hydrates,
|
|
270
|
+
* it asks Better Auth's `/get-session` for the current session. On the server, a session that the server could not
|
|
271
|
+
* read counts as signed out.
|
|
272
|
+
*/
|
|
273
|
+
authorize: <const Name extends AbilityName>(name: Name, ...args: AbilityArgs<Name>) => Promise<void>;
|
|
274
|
+
/** `true` once the session is known. */
|
|
275
|
+
ready: ComputedRef<boolean>;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Checks the rules of `shared/abilities.ts` in the app, with the same rules the server runs. Read `can()` and
|
|
279
|
+
* `cannot()` in a template or a `computed()`, so they follow the session. Call `authorize()` in a page that the visitor
|
|
280
|
+
* must not see when the rule refuses.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* const { can, authorize } = useAbility()
|
|
284
|
+
* const editable = computed(() => post.value && can("posts.edit", post.value))
|
|
285
|
+
* // In a page, after the post loads:
|
|
286
|
+
* await authorize("posts.edit", loadedPost)
|
|
287
|
+
*/
|
|
288
|
+
export declare function useAbility(client?: AuthClientLike): Ability;
|
|
289
|
+
/**
|
|
290
|
+
* How `<Can>` passes the arguments of a rule: no prop for a rule without arguments, `with` for a rule with one
|
|
291
|
+
* (required or optional), and `args` with the list for every other rule, such as two arguments, a default after the
|
|
292
|
+
* first, or a rest parameter.
|
|
293
|
+
*/
|
|
294
|
+
type CanArguments<Args extends unknown[]> = Args extends [] ? {
|
|
295
|
+
with?: never;
|
|
296
|
+
args?: never;
|
|
297
|
+
} : number extends Args["length"] ? {
|
|
298
|
+
with?: never;
|
|
299
|
+
args: Args;
|
|
300
|
+
} : Exclude<Args["length"], 0> extends 1 ? (0 extends Args["length"] ? {
|
|
301
|
+
with?: Args[0];
|
|
302
|
+
args?: never;
|
|
303
|
+
} : {
|
|
304
|
+
with: Args[0];
|
|
305
|
+
args?: never;
|
|
306
|
+
}) : {
|
|
307
|
+
with?: never;
|
|
308
|
+
args: Args;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* The props of `<Can>`: a rule name in `do`, its arguments in `with` or `args`, and `pending` to show nothing while
|
|
312
|
+
* the data for the arguments loads.
|
|
313
|
+
*/
|
|
314
|
+
export type CanProps<Name extends AbilityName> = {
|
|
315
|
+
do: Name;
|
|
316
|
+
pending?: boolean;
|
|
317
|
+
} & CanArguments<AbilityArgs<Name>>;
|
|
318
|
+
/** The slots of `<Can>`: the default slot when the rule allows, `#denied` with the refusal when it refuses. */
|
|
319
|
+
export interface CanSlots<Name extends AbilityName> {
|
|
320
|
+
default?: () => VNode[];
|
|
321
|
+
denied?: (refusal: AbilityRefusal<Name>) => VNode[];
|
|
322
|
+
}
|
|
323
|
+
/** The type that vue-tsc reads for a generic component, so `do` checks the name and `with`, `args` and `#denied` follow it. */
|
|
324
|
+
export type CanComponent = <const Name extends AbilityName>(props: CanProps<Name>) => VNode & {
|
|
325
|
+
__ctx?: {
|
|
326
|
+
props: CanProps<Name>;
|
|
327
|
+
slots: CanSlots<Name>;
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Shows its content when a rule from `shared/abilities.ts` allows the visitor, the `#denied` slot with the refusal when
|
|
332
|
+
* it refuses, and nothing while the session is unknown or `pending` is `true`.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* <Can do="posts.edit" :with="post">
|
|
336
|
+
* <button>Edit</button>
|
|
337
|
+
* <template #denied="{ code }">{{ code }}</template>
|
|
338
|
+
* </Can>
|
|
339
|
+
* <Can do="posts.move" :args="[from, to]">Move</Can>
|
|
340
|
+
*/
|
|
341
|
+
export declare const Can: CanComponent;
|
|
342
|
+
/** The slot of `<Cannot>`: its content, with the refusal, when the rule refuses. */
|
|
343
|
+
export interface CannotSlots<Name extends AbilityName> {
|
|
344
|
+
default?: (refusal: AbilityRefusal<Name>) => VNode[];
|
|
345
|
+
}
|
|
346
|
+
/** The type that vue-tsc reads for `<Cannot>`: the props of `<Can>`, and the refusal in the default slot. */
|
|
347
|
+
export type CannotComponent = <const Name extends AbilityName>(props: CanProps<Name>) => VNode & {
|
|
348
|
+
__ctx?: {
|
|
349
|
+
props: CanProps<Name>;
|
|
350
|
+
slots: CannotSlots<Name>;
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* The mirror of `<Can>`: shows its content, with the refusal, only when a rule from `shared/abilities.ts` refuses the
|
|
355
|
+
* visitor. It shows nothing when the rule allows, while the session is unknown, or while `pending` is `true`.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* <Cannot do="billing.upgrade" v-slot="{ code, details }">
|
|
359
|
+
* <UpgradeHint :code="code" :limit="details.limit" />
|
|
360
|
+
* </Cannot>
|
|
361
|
+
*/
|
|
362
|
+
export declare const Cannot: CannotComponent;
|
|
54
363
|
//#endregion
|
|
55
|
-
export type { VueAuthClient };
|
|
364
|
+
export type { RequestFetch, VueAuthClient };
|