@delmaredigital/payload-better-auth 0.4.3 → 0.5.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/README.md +19 -1824
- package/dist/adapter/index.d.ts +3 -15
- package/dist/adapter/index.js +49 -180
- package/dist/components/management/ApiKeysManagementClient.js +2 -1
- package/dist/exports/client.d.ts +1124 -1929
- package/dist/exports/client.js +4 -2
- package/dist/generated-types.d.ts +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin/index.js +1 -1
- package/dist/scripts/generate-types.js +3 -2
- package/dist/types/betterAuth.d.ts +5 -70
- package/dist/utils/apiKeyAccess.d.ts +5 -3
- package/dist/utils/apiKeyAccess.js +16 -7
- package/dist/utils/betterAuthDefaults.d.ts +0 -1056
- package/dist/utils/betterAuthDefaults.js +11 -38
- package/package.json +10 -5
package/dist/exports/client.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
* Client-side auth utilities
|
|
3
3
|
* Re-exports createAuthClient from better-auth/react and common plugins
|
|
4
4
|
*/ import { createAuthClient } from 'better-auth/react';
|
|
5
|
-
import { twoFactorClient
|
|
5
|
+
import { twoFactorClient } from 'better-auth/client/plugins';
|
|
6
|
+
import { apiKeyClient } from '@better-auth/api-key/client';
|
|
6
7
|
import { passkeyClient } from '@better-auth/passkey/client';
|
|
7
8
|
// Re-export createAuthClient and common plugins
|
|
8
9
|
export { createAuthClient } from 'better-auth/react';
|
|
9
|
-
export { twoFactorClient
|
|
10
|
+
export { twoFactorClient } from 'better-auth/client/plugins';
|
|
11
|
+
export { apiKeyClient } from '@better-auth/api-key/client';
|
|
10
12
|
export { passkeyClient } from '@better-auth/passkey/client';
|
|
11
13
|
/**
|
|
12
14
|
* Default plugins included with Payload Better Auth.
|
|
@@ -79,11 +79,12 @@ export type BaseVerificationFields = {
|
|
|
79
79
|
};
|
|
80
80
|
export type Verification = BaseVerificationFields;
|
|
81
81
|
export type ApikeyFields = {
|
|
82
|
+
configId: string;
|
|
82
83
|
name?: string;
|
|
83
84
|
start?: string;
|
|
85
|
+
referenceId: string;
|
|
84
86
|
prefix?: string;
|
|
85
87
|
key: string;
|
|
86
|
-
userId: string;
|
|
87
88
|
refillInterval?: number;
|
|
88
89
|
refillAmount?: number;
|
|
89
90
|
lastRefillAt?: Date;
|
|
@@ -204,7 +205,7 @@ export type TwoFactor = TwoFactorFields;
|
|
|
204
205
|
/**
|
|
205
206
|
* Union of all supported plugin identifiers.
|
|
206
207
|
*/
|
|
207
|
-
export type PluginId = "username" | "admin" | "api-key" | "passkey" | "bearer" | "email-otp" | "magic-link" | "phone-number" | "one-tap" | "anonymous" | "multi-session" | "one-time-token" | "oidc" | "generic-oauth" | "open-api" | "organization" | "jwt" | "two-factor";
|
|
208
|
+
export type PluginId = "username" | "admin" | "api-key" | "passkey" | "bearer" | "email-otp" | "magic-link" | "phone-number" | "one-tap" | "anonymous" | "multi-session" | "one-time-token" | "oidc-provider" | "generic-oauth" | "open-api" | "organization" | "jwt" | "two-factor";
|
|
208
209
|
/**
|
|
209
210
|
* Complete schema mapping of all models to their types.
|
|
210
211
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -26,4 +26,4 @@ export { getServerSession, getServerUser, createSessionHelpers } from './utils/s
|
|
|
26
26
|
export type { Session, SessionHelperOptions } from './utils/session.js';
|
|
27
27
|
export { firstUserAdminHooks } from './utils/firstUserAdmin.js';
|
|
28
28
|
export type { FirstUserAdminOptions } from './utils/firstUserAdmin.js';
|
|
29
|
-
export { withBetterAuthDefaults
|
|
29
|
+
export { withBetterAuthDefaults } from './utils/betterAuthDefaults.js';
|
package/dist/index.js
CHANGED
|
@@ -24,4 +24,4 @@ export { getServerSession, getServerUser, createSessionHelpers } from './utils/s
|
|
|
24
24
|
// First user admin hook utility
|
|
25
25
|
export { firstUserAdminHooks } from './utils/firstUserAdmin.js';
|
|
26
26
|
// Better Auth defaults utility
|
|
27
|
-
export { withBetterAuthDefaults
|
|
27
|
+
export { withBetterAuthDefaults } from './utils/betterAuthDefaults.js';
|
package/dist/plugin/index.js
CHANGED
|
@@ -83,7 +83,7 @@ let apiKeyScopesConfig = undefined;
|
|
|
83
83
|
const isMetadataDisabled = errorMessage.toLowerCase().includes('metadata') && errorMessage.toLowerCase().includes('disabled');
|
|
84
84
|
if (isMetadataDisabled && createOptions.body.metadata) {
|
|
85
85
|
// Retry without metadata - key will still work, just won't show scopes in UI
|
|
86
|
-
console.warn('[better-auth] Metadata disabled, creating API key without scope metadata. Enable metadata with
|
|
86
|
+
console.warn('[better-auth] Metadata disabled, creating API key without scope metadata. Enable metadata with apiKey({ enableMetadata: true }) for better UX.');
|
|
87
87
|
const optionsWithoutMetadata = {
|
|
88
88
|
body: {
|
|
89
89
|
...createOptions.body,
|
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
* and diffing plugin additions against the base schema.
|
|
6
6
|
*
|
|
7
7
|
* Run with: pnpm generate:types
|
|
8
|
-
*/ import {
|
|
8
|
+
*/ import { apiKey } from '@better-auth/api-key';
|
|
9
|
+
import { passkey } from '@better-auth/passkey';
|
|
9
10
|
import { getSchema } from 'better-auth/db';
|
|
10
|
-
import { admin, anonymous,
|
|
11
|
+
import { admin, anonymous, bearer, emailOTP, genericOAuth, jwt, magicLink, multiSession, oidcProvider, oneTap, oneTimeToken, openAPI, organization, phoneNumber, twoFactor, username } from 'better-auth/plugins';
|
|
11
12
|
import fs from 'node:fs/promises';
|
|
12
13
|
import path from 'node:path';
|
|
13
14
|
import { fileURLToPath } from 'node:url';
|
|
@@ -4,50 +4,8 @@
|
|
|
4
4
|
* Provides improved type inference for the Better Auth instance,
|
|
5
5
|
* including session/user types, API methods, and error codes.
|
|
6
6
|
*/
|
|
7
|
-
import type {
|
|
8
|
-
import { router } from 'better-auth/api';
|
|
9
|
-
import type { BetterAuthOptions, BetterAuthPlugin, InferAPI, InferPluginTypes, InferSession, InferUser } from 'better-auth/types';
|
|
7
|
+
import type { Auth, BetterAuthOptions } from 'better-auth/types';
|
|
10
8
|
import type { BasePayload, Endpoint, PayloadRequest } from 'payload';
|
|
11
|
-
/**
|
|
12
|
-
* Base error codes from Better Auth core.
|
|
13
|
-
*/
|
|
14
|
-
type BaseErrorCodes = {
|
|
15
|
-
FAILED_TO_GET_USER_INFO: string;
|
|
16
|
-
USER_ALREADY_EXISTS: string;
|
|
17
|
-
INVALID_PASSWORD: string;
|
|
18
|
-
FAILED_TO_CREATE_USER: string;
|
|
19
|
-
FAILED_TO_CREATE_SESSION: string;
|
|
20
|
-
FAILED_TO_UPDATE_USER: string;
|
|
21
|
-
FAILED_TO_GET_SESSION: string;
|
|
22
|
-
INVALID_EMAIL_OR_PASSWORD: string;
|
|
23
|
-
SOCIAL_ACCOUNT_ALREADY_LINKED: string;
|
|
24
|
-
PROVIDER_NOT_FOUND: string;
|
|
25
|
-
INVALID_TOKEN: string;
|
|
26
|
-
ID_TOKEN_NOT_SUPPORTED: string;
|
|
27
|
-
FAILED_TO_GET_USER_INFO_OPENID: string;
|
|
28
|
-
UNEXPECTED_PROVIDER_RESPONSE: string;
|
|
29
|
-
TOKEN_REFRESH_FAILED: string;
|
|
30
|
-
FAILED_TO_UNLINK: string;
|
|
31
|
-
ACCOUNT_NOT_FOUND: string;
|
|
32
|
-
SESSION_EXPIRED: string;
|
|
33
|
-
INTERNAL_SERVER_ERROR: string;
|
|
34
|
-
VALIDATION_ERROR: string;
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Union to intersection utility type.
|
|
38
|
-
*/
|
|
39
|
-
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
40
|
-
/**
|
|
41
|
-
* Deeply prettify a type for better IDE display.
|
|
42
|
-
* Flattens intersections and preserves functions/arrays/dates.
|
|
43
|
-
*/
|
|
44
|
-
type PrettifyDeep<T> = {
|
|
45
|
-
[K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? T[K] : T[K] extends object ? T[K] extends Array<unknown> ? T[K] : T[K] extends Date ? T[K] : PrettifyDeep<T[K]> : T[K];
|
|
46
|
-
} & {};
|
|
47
|
-
/**
|
|
48
|
-
* Infer error codes from enabled plugins.
|
|
49
|
-
*/
|
|
50
|
-
type InferPluginErrorCodes<O extends BetterAuthOptions> = O['plugins'] extends Array<infer P> ? UnionToIntersection<P extends BetterAuthPlugin ? P['$ERROR_CODES'] extends Record<string, unknown> ? P['$ERROR_CODES'] : never : never> extends infer R ? [R] extends [never] ? object : R : object : object;
|
|
51
9
|
/**
|
|
52
10
|
* Role array type with configurable roles.
|
|
53
11
|
*/
|
|
@@ -55,11 +13,9 @@ export type RoleArray<O extends readonly string[] = readonly ['user']> = O[numbe
|
|
|
55
13
|
/**
|
|
56
14
|
* The return type of a Better Auth instance.
|
|
57
15
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* - Error codes from all enabled plugins
|
|
62
|
-
* - Auth context for advanced use cases
|
|
16
|
+
* Uses the official `Auth<O>` type from Better Auth 1.5, which provides
|
|
17
|
+
* full type inference for API endpoints, session/user types, error codes,
|
|
18
|
+
* and auth context.
|
|
63
19
|
*
|
|
64
20
|
* @template O - Better Auth options type for inference
|
|
65
21
|
*
|
|
@@ -72,27 +28,7 @@ export type RoleArray<O extends readonly string[] = readonly ['user']> = O[numbe
|
|
|
72
28
|
* const result = await payload.betterAuth.api.getSession({ headers })
|
|
73
29
|
* ```
|
|
74
30
|
*/
|
|
75
|
-
export type BetterAuthReturn<O extends BetterAuthOptions = BetterAuthOptions> =
|
|
76
|
-
/** The request handler for auth endpoints */
|
|
77
|
-
handler: (request: Request) => Promise<Response>;
|
|
78
|
-
/** Type-safe API methods */
|
|
79
|
-
api: InferAPI<ReturnType<typeof router<O>>>['endpoints'];
|
|
80
|
-
/** The resolved options */
|
|
81
|
-
options: O;
|
|
82
|
-
/** All error codes from enabled plugins */
|
|
83
|
-
$ERROR_CODES: InferPluginErrorCodes<O> & BaseErrorCodes;
|
|
84
|
-
/** Auth context (async) for advanced use cases */
|
|
85
|
-
$context: Promise<AuthContext>;
|
|
86
|
-
/** Inferred types for Session and User */
|
|
87
|
-
$Infer: InferPluginTypes<O> extends {
|
|
88
|
-
Session: unknown;
|
|
89
|
-
} ? InferPluginTypes<O> : {
|
|
90
|
-
Session: {
|
|
91
|
-
session: PrettifyDeep<InferSession<O>>;
|
|
92
|
-
user: PrettifyDeep<InferUser<O>>;
|
|
93
|
-
};
|
|
94
|
-
} & InferPluginTypes<O>;
|
|
95
|
-
};
|
|
31
|
+
export type BetterAuthReturn<O extends BetterAuthOptions = BetterAuthOptions> = Auth<O>;
|
|
96
32
|
/**
|
|
97
33
|
* Payload instance with Better Auth attached.
|
|
98
34
|
*
|
|
@@ -188,4 +124,3 @@ export type CollectionHookWithBetterAuth<O extends BetterAuthOptions, T extends
|
|
|
188
124
|
export type EndpointWithBetterAuth<O extends BetterAuthOptions> = Omit<Endpoint, 'handler'> & {
|
|
189
125
|
handler: (req: PayloadRequestWithBetterAuth<O>) => Promise<Response> | Response;
|
|
190
126
|
};
|
|
191
|
-
export {};
|
|
@@ -24,8 +24,10 @@ import type { Access, PayloadRequest } from 'payload';
|
|
|
24
24
|
export type ApiKeyInfo = {
|
|
25
25
|
/** The API key ID */
|
|
26
26
|
id: string;
|
|
27
|
-
/**
|
|
28
|
-
|
|
27
|
+
/** Reference ID (user or organization) who owns this key */
|
|
28
|
+
referenceId: string;
|
|
29
|
+
/** Reference type - whether key is owned by a user or organization */
|
|
30
|
+
referenceType: 'user' | 'organization';
|
|
29
31
|
/** Array of granted scope strings */
|
|
30
32
|
scopes: string[];
|
|
31
33
|
/** The raw key (only first/last chars visible) */
|
|
@@ -163,7 +165,7 @@ export declare function allowSessionOrAnyScope(scopes: string[], config?: Omit<A
|
|
|
163
165
|
* ```ts
|
|
164
166
|
* const keyInfo = await validateApiKey(req)
|
|
165
167
|
* if (keyInfo) {
|
|
166
|
-
* console.log('Valid API key for
|
|
168
|
+
* console.log('Valid API key for:', keyInfo.referenceId, keyInfo.referenceType)
|
|
167
169
|
* console.log('Scopes:', keyInfo.scopes)
|
|
168
170
|
* }
|
|
169
171
|
* ```
|
|
@@ -140,12 +140,20 @@
|
|
|
140
140
|
} else if (Array.isArray(doc.scopes)) {
|
|
141
141
|
scopes = doc.scopes;
|
|
142
142
|
}
|
|
143
|
-
// Get
|
|
144
|
-
let
|
|
145
|
-
|
|
146
|
-
|
|
143
|
+
// Get reference ID and type (BA 1.5 uses referenceId/referenceType instead of userId)
|
|
144
|
+
let referenceId;
|
|
145
|
+
let referenceType = 'user';
|
|
146
|
+
if (doc.referenceId) {
|
|
147
|
+
referenceId = String(doc.referenceId);
|
|
148
|
+
if (doc.referenceType === 'organization') {
|
|
149
|
+
referenceType = 'organization';
|
|
150
|
+
}
|
|
151
|
+
} else if (doc.userId) {
|
|
152
|
+
// Fallback for pre-1.5 schema
|
|
153
|
+
referenceId = String(doc.userId);
|
|
147
154
|
} else if (doc.user) {
|
|
148
|
-
|
|
155
|
+
// Fallback for relationship field
|
|
156
|
+
referenceId = typeof doc.user === 'object' ? String(doc.user.id) : String(doc.user);
|
|
149
157
|
} else {
|
|
150
158
|
return null;
|
|
151
159
|
}
|
|
@@ -171,7 +179,8 @@
|
|
|
171
179
|
}
|
|
172
180
|
return {
|
|
173
181
|
id: String(doc.id),
|
|
174
|
-
|
|
182
|
+
referenceId,
|
|
183
|
+
referenceType,
|
|
175
184
|
scopes,
|
|
176
185
|
keyPrefix: doc.start,
|
|
177
186
|
metadata
|
|
@@ -372,7 +381,7 @@
|
|
|
372
381
|
* ```ts
|
|
373
382
|
* const keyInfo = await validateApiKey(req)
|
|
374
383
|
* if (keyInfo) {
|
|
375
|
-
* console.log('Valid API key for
|
|
384
|
+
* console.log('Valid API key for:', keyInfo.referenceId, keyInfo.referenceType)
|
|
376
385
|
* console.log('Scopes:', keyInfo.scopes)
|
|
377
386
|
* }
|
|
378
387
|
* ```
|