@vonall/auth 0.1.0 → 0.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @vonall/auth
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - The plugins a Product passes to `createAuth()` and `createAuthClient()` are part of the returned types (`auth.api.…`, `authClient.…` of the MCP endpoint's plugins).
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
@@ -1,17 +1,24 @@
1
+ /** The better-auth client for a Product's browser code; `src/lib/auth-client.ts` creates it once. */
2
+ import type { BetterAuthClientPlugin } from "better-auth/client";
3
+ import { adminClient, twoFactorClient } from "better-auth/client/plugins";
1
4
  import { createAuthClient as createClient } from "better-auth/svelte";
2
- type ClientPlugin = NonNullable<Parameters<typeof createClient>[0]>["plugins"] extends (infer P)[] | undefined ? P : never;
3
- export type CreateAuthClientOptions = {
5
+ import { ac, roles } from "../permissions.js";
6
+ export type CreateAuthClientOptions<P extends BetterAuthClientPlugin[] = []> = {
4
7
  /** PUBLIC_ORIGIN. */
5
8
  baseURL: string;
6
9
  /** The Admin option (matches the server). */
7
10
  admin?: boolean;
8
- /** More client plugins, e.g. the MCP endpoint's (`@vonall/mcp/client`). */
9
- plugins?: ClientPlugin[];
11
+ /** More client plugins, e.g. the MCP endpoint's (`@vonall/mcp/client`); they are part of the client's type. */
12
+ plugins?: P;
10
13
  };
11
- declare function build({ baseURL, admin, plugins }: CreateAuthClientOptions): import("better-auth/svelte").SvelteAuthClient<{
14
+ type BasePlugins = [ReturnType<typeof twoFactorClient>, ReturnType<typeof adminClient<{
15
+ ac: typeof ac;
16
+ roles: typeof roles;
17
+ }>>];
18
+ /** The client with this package's plugins and the Product's. */
19
+ export type AuthClient<P extends BetterAuthClientPlugin[] = []> = ReturnType<typeof createClient<{
12
20
  baseURL: string;
13
- plugins: any[];
14
- }>;
15
- export type AuthClient = ReturnType<typeof build>;
16
- export declare function createAuthClient(options: CreateAuthClientOptions): AuthClient;
21
+ plugins: [...BasePlugins, ...P];
22
+ }>>;
23
+ export declare function createAuthClient<P extends BetterAuthClientPlugin[] = []>({ baseURL, admin, plugins }: CreateAuthClientOptions<P>): AuthClient<P>;
17
24
  export {};
@@ -1,18 +1,9 @@
1
- /** The better-auth client for a Product's browser code; `src/lib/auth-client.ts` creates it once. */
2
1
  import { adminClient, twoFactorClient } from "better-auth/client/plugins";
3
2
  import { createAuthClient as createClient } from "better-auth/svelte";
4
3
  import { ac, roles } from "../permissions.js";
5
- function build({ baseURL, admin = false, plugins = [] }) {
4
+ export function createAuthClient({ baseURL, admin = false, plugins }) {
6
5
  return createClient({
7
6
  baseURL: baseURL || "http://localhost:5173",
8
- plugins: [
9
- twoFactorClient(),
10
- // The admin client's types are part of the client's type whether or not the option is on
11
- ...(admin ? [adminClient({ ac, roles })] : []),
12
- ...plugins,
13
- ],
7
+ plugins: [twoFactorClient(), ...(admin ? [adminClient({ ac, roles })] : []), ...(plugins ?? [])],
14
8
  });
15
9
  }
16
- export function createAuthClient(options) {
17
- return build(options);
18
- }
@@ -1,8 +1,8 @@
1
1
  export declare const ac: {
2
- newRole<const TRoleStatements extends import("better-auth/plugins").Statements>(statements: import("better-auth/plugins").RoleInput<{
2
+ newRole<const TRoleStatements extends import("better-auth/client").Statements>(statements: import("better-auth/client").RoleInput<{
3
3
  readonly user: readonly ["create", "list", "set-role", "ban", "impersonate", "impersonate-admins", "delete", "set-password", "set-email", "get", "update"];
4
4
  readonly session: readonly ["list", "revoke", "delete"];
5
- }, TRoleStatements>): import("better-auth/plugins").Role<import("better-auth/plugins").ExactRoleStatements<TRoleStatements>, {
5
+ }, TRoleStatements>): import("better-auth/client").Role<import("better-auth/client").ExactRoleStatements<TRoleStatements>, {
6
6
  readonly user: readonly ["create", "list", "set-role", "ban", "impersonate", "impersonate-admins", "delete", "set-password", "set-email", "get", "update"];
7
7
  readonly session: readonly ["list", "revoke", "delete"];
8
8
  }>;
@@ -15,14 +15,14 @@ export declare const ac: {
15
15
  * The app admin manages accounts but can't act as or take over a friend's account: no impersonation, no setting a
16
16
  * password or an email, no creating accounts (signup only through an Einladung).
17
17
  */
18
- export declare const adminRole: import("better-auth/plugins").Role<import("better-auth/plugins").ExactRoleStatements<{
18
+ export declare const adminRole: import("better-auth/client").Role<import("better-auth/client").ExactRoleStatements<{
19
19
  readonly user: ["list", "get", "set-role", "ban", "delete"];
20
20
  readonly session: ["list", "revoke"];
21
21
  }>, {
22
22
  readonly user: readonly ["create", "list", "set-role", "ban", "impersonate", "impersonate-admins", "delete", "set-password", "set-email", "get", "update"];
23
23
  readonly session: readonly ["list", "revoke", "delete"];
24
24
  }>;
25
- export declare const userRole: import("better-auth/plugins").Role<import("better-auth/plugins").ExactRoleStatements<{
25
+ export declare const userRole: import("better-auth/client").Role<import("better-auth/client").ExactRoleStatements<{
26
26
  readonly user: [];
27
27
  readonly session: [];
28
28
  }>, {
@@ -31,14 +31,14 @@ export declare const userRole: import("better-auth/plugins").Role<import("better
31
31
  }>;
32
32
  /** The roles as better-auth's admin plugin and client take them. */
33
33
  export declare const roles: {
34
- admin: import("better-auth/plugins").Role<import("better-auth/plugins").ExactRoleStatements<{
34
+ admin: import("better-auth/client").Role<import("better-auth/client").ExactRoleStatements<{
35
35
  readonly user: ["list", "get", "set-role", "ban", "delete"];
36
36
  readonly session: ["list", "revoke"];
37
37
  }>, {
38
38
  readonly user: readonly ["create", "list", "set-role", "ban", "impersonate", "impersonate-admins", "delete", "set-password", "set-email", "get", "update"];
39
39
  readonly session: readonly ["list", "revoke", "delete"];
40
40
  }>;
41
- user: import("better-auth/plugins").Role<import("better-auth/plugins").ExactRoleStatements<{
41
+ user: import("better-auth/client").Role<import("better-auth/client").ExactRoleStatements<{
42
42
  readonly user: [];
43
43
  readonly session: [];
44
44
  }>, {
@@ -17,7 +17,7 @@ export type SocialProvider = {
17
17
  clientId: string;
18
18
  clientSecret: string;
19
19
  };
20
- export type CreateAuthOptions = {
20
+ export type CreateAuthOptions<P extends BetterAuthPlugin[] = []> = {
21
21
  /** The Product's Drizzle database. */
22
22
  db: Db;
23
23
  /** The Product's tables from `authSchema()`, the same objects its `schema/` re-exports. */
@@ -39,8 +39,8 @@ export type CreateAuthOptions = {
39
39
  github?: SocialProvider | null;
40
40
  google?: SocialProvider | null;
41
41
  };
42
- /** More better-auth plugins, e.g. the MCP endpoint's (`@vonall/mcp`). */
43
- plugins?: BetterAuthPlugin[];
42
+ /** More better-auth plugins, e.g. the MCP endpoint's (`@vonall/mcp`); they are part of the instance's type. */
43
+ plugins?: P;
44
44
  /** More rate limit rules, by path (`@vonall/mcp` adds `/oauth2/register`). */
45
45
  rateLimitRules?: Record<string, {
46
46
  window: number;
@@ -53,12 +53,12 @@ export type CreateAuthOptions = {
53
53
  * plugin is in it whether or not the option is on, so `user.role` exists; without the option it is left out at runtime
54
54
  * and nobody has a role.
55
55
  */
56
- type FullPlugins = [ReturnType<typeof twoFactor>, ReturnType<typeof adminPlugin<{
56
+ type BasePlugins = [ReturnType<typeof twoFactor>, ReturnType<typeof adminPlugin<{
57
57
  ac: typeof ac;
58
58
  roles: typeof roles;
59
- }>>, ReturnType<typeof sveltekitCookies>];
60
- export type Auth = ReturnType<typeof betterAuth<{
61
- plugins: FullPlugins;
59
+ }>>];
60
+ export type Auth<P extends BetterAuthPlugin[] = []> = ReturnType<typeof betterAuth<{
61
+ plugins: [...BasePlugins, ...P, ReturnType<typeof sveltekitCookies>];
62
62
  }>>;
63
63
  /** The admin plugin's columns, named here because the plugin's type doesn't carry them into the session's user. */
64
64
  export type AdminFields = {
@@ -70,9 +70,9 @@ export type AdminFields = {
70
70
  /** The user and the session as the hooks put them into `locals` (a Product's `app.d.ts` uses these). */
71
71
  export type AuthUser = Auth["$Infer"]["Session"]["user"] & AdminFields;
72
72
  export type AuthSession = Auth["$Infer"]["Session"]["session"];
73
- export type CreatedAuth = {
73
+ export type CreatedAuth<P extends BetterAuthPlugin[] = []> = {
74
74
  /** The better-auth instance: `auth.api`, `auth.handler`, `auth.$context`. */
75
- auth: Auth;
75
+ auth: Auth<P>;
76
76
  /** The hooks handle: the session into `locals.user`/`locals.session`, and better-auth's own routes (/api/auth/*). */
77
77
  handle: Handle;
78
78
  /** For `src/routes/api/auth/[...auth]/+server.ts`: `export const GET = apiHandler, POST = apiHandler`. */
@@ -83,5 +83,5 @@ export type CreatedAuth = {
83
83
  google: boolean;
84
84
  };
85
85
  };
86
- export declare function createAuth(options: CreateAuthOptions): CreatedAuth;
86
+ export declare function createAuth<P extends BetterAuthPlugin[] = []>(options: CreateAuthOptions<P>): CreatedAuth<P>;
87
87
  export {};
@@ -25,7 +25,7 @@ function einladungAusCookie() {
25
25
  }
26
26
  const hasKeys = (p) => Boolean(p?.clientId && p.clientSecret && p.clientId !== "dummy");
27
27
  function build(options) {
28
- const { secret, origin, appName, signupMode, admin = false, social = {}, plugins = [], rateLimitRules = {}, log = consoleLog } = options;
28
+ const { secret, origin, appName, signupMode, admin = false, social = {}, plugins, rateLimitRules = {}, log = consoleLog } = options;
29
29
  const baseURL = origin || "http://localhost";
30
30
  return betterAuth({
31
31
  appName,
@@ -123,12 +123,13 @@ function build(options) {
123
123
  plugins: [
124
124
  twoFactor(),
125
125
  ...(admin ? [adminPlugin({ ac, roles })] : []),
126
- ...plugins,
126
+ ...(plugins ?? []),
127
127
  sveltekitCookies(getRequestEvent), // Ensure cookies are populated in SvelteKit
128
128
  ],
129
129
  });
130
130
  }
131
131
  export function createAuth(options) {
132
+ // Inside, the instance is used with this package's plugins only; the Product gets it with its own plugins' types too
132
133
  const auth = build(options);
133
134
  const { social = {} } = options;
134
135
  setRegistry({
@@ -154,5 +155,5 @@ export function createAuth(options) {
154
155
  return svelteKitHandler({ event, resolve, auth, building });
155
156
  };
156
157
  const apiHandler = ({ request }) => auth.handler(request);
157
- return { auth, handle, apiHandler, providers: { github: hasKeys(social.github), google: hasKeys(social.google) } };
158
+ return { auth: auth, handle, apiHandler, providers: { github: hasKeys(social.github), google: hasKeys(social.google) } };
158
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vonall/auth",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Accounts for a Web App Factory Product: better-auth with the hooks, the protected helpers, Einladungen, Konto, the auth pages and the settings; Admin as an option",
5
5
  "license": "MIT",
6
6
  "repository": {