@better-auth/core 1.5.0-beta.4 → 1.5.0-beta.5
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/.turbo/turbo-build.log +170 -37
- package/dist/api/index.d.mts +188 -1
- package/dist/api/index.mjs +2 -1
- package/dist/context/endpoint-context.d.mts +19 -0
- package/dist/context/endpoint-context.mjs +27 -0
- package/dist/context/index.d.mts +3 -52
- package/dist/context/index.mjs +22 -1
- package/dist/context/request-state.d.mts +27 -0
- package/dist/context/request-state.mjs +45 -0
- package/dist/context/transaction.d.mts +16 -0
- package/dist/context/transaction.mjs +48 -0
- package/dist/db/adapter/factory.d.mts +27 -0
- package/dist/db/adapter/factory.mjs +738 -0
- package/dist/db/adapter/get-default-field-name.d.mts +18 -0
- package/dist/db/adapter/get-default-field-name.mjs +38 -0
- package/dist/db/adapter/get-default-model-name.d.mts +12 -0
- package/dist/db/adapter/get-default-model-name.mjs +32 -0
- package/dist/db/adapter/get-field-attributes.d.mts +29 -0
- package/dist/db/adapter/get-field-attributes.mjs +39 -0
- package/dist/db/adapter/get-field-name.d.mts +18 -0
- package/dist/db/adapter/get-field-name.mjs +33 -0
- package/dist/db/adapter/get-id-field.d.mts +39 -0
- package/dist/db/adapter/get-id-field.mjs +67 -0
- package/dist/db/adapter/get-model-name.d.mts +12 -0
- package/dist/db/adapter/get-model-name.mjs +23 -0
- package/dist/db/adapter/index.d.mts +513 -1
- package/dist/db/adapter/index.mjs +8 -970
- package/dist/db/adapter/types.d.mts +139 -0
- package/dist/db/adapter/utils.d.mts +7 -0
- package/dist/db/adapter/utils.mjs +38 -0
- package/dist/db/get-tables.d.mts +8 -0
- package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
- package/dist/db/index.d.mts +10 -2
- package/dist/db/index.mjs +7 -60
- package/dist/db/plugin.d.mts +12 -0
- package/dist/db/schema/account.d.mts +26 -0
- package/dist/db/schema/account.mjs +19 -0
- package/dist/db/schema/rate-limit.d.mts +14 -0
- package/dist/db/schema/rate-limit.mjs +11 -0
- package/dist/db/schema/session.d.mts +21 -0
- package/dist/db/schema/session.mjs +14 -0
- package/dist/db/schema/shared.d.mts +10 -0
- package/dist/db/schema/shared.mjs +11 -0
- package/dist/db/schema/user.d.mts +20 -0
- package/dist/db/schema/user.mjs +13 -0
- package/dist/db/schema/verification.d.mts +19 -0
- package/dist/db/schema/verification.mjs +12 -0
- package/dist/db/type.d.mts +143 -0
- package/dist/env/color-depth.d.mts +4 -0
- package/dist/env/color-depth.mjs +88 -0
- package/dist/env/env-impl.d.mts +32 -0
- package/dist/env/env-impl.mjs +82 -0
- package/dist/env/index.d.mts +4 -2
- package/dist/env/index.mjs +3 -1
- package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
- package/dist/env/logger.mjs +81 -0
- package/dist/error/codes.d.mts +186 -0
- package/dist/{error-GNtLPYaS.mjs → error/codes.mjs} +2 -29
- package/dist/error/index.d.mts +1 -185
- package/dist/error/index.mjs +28 -3
- package/dist/index.d.mts +7 -1
- package/dist/oauth2/client-credentials-token.d.mts +36 -0
- package/dist/oauth2/client-credentials-token.mjs +54 -0
- package/dist/oauth2/create-authorization-url.d.mts +45 -0
- package/dist/oauth2/create-authorization-url.mjs +42 -0
- package/dist/oauth2/index.d.mts +8 -2
- package/dist/oauth2/index.mjs +6 -2
- package/dist/oauth2/oauth-provider.d.mts +194 -0
- package/dist/oauth2/refresh-access-token.d.mts +36 -0
- package/dist/oauth2/refresh-access-token.mjs +58 -0
- package/dist/oauth2/utils.d.mts +7 -0
- package/dist/oauth2/utils.mjs +27 -0
- package/dist/oauth2/validate-authorization-code.d.mts +55 -0
- package/dist/oauth2/validate-authorization-code.mjs +71 -0
- package/dist/oauth2/verify.d.mts +49 -0
- package/dist/oauth2/verify.mjs +95 -0
- package/dist/social-providers/apple.d.mts +119 -0
- package/dist/social-providers/apple.mjs +102 -0
- package/dist/social-providers/atlassian.d.mts +72 -0
- package/dist/social-providers/atlassian.mjs +83 -0
- package/dist/social-providers/cognito.d.mts +87 -0
- package/dist/social-providers/cognito.mjs +165 -0
- package/dist/social-providers/discord.d.mts +126 -0
- package/dist/social-providers/discord.mjs +64 -0
- package/dist/social-providers/dropbox.d.mts +71 -0
- package/dist/social-providers/dropbox.mjs +75 -0
- package/dist/social-providers/facebook.d.mts +81 -0
- package/dist/social-providers/facebook.mjs +120 -0
- package/dist/social-providers/figma.d.mts +63 -0
- package/dist/social-providers/figma.mjs +84 -0
- package/dist/social-providers/github.d.mts +104 -0
- package/dist/social-providers/github.mjs +80 -0
- package/dist/social-providers/gitlab.d.mts +125 -0
- package/dist/social-providers/gitlab.mjs +82 -0
- package/dist/social-providers/google.d.mts +99 -0
- package/dist/social-providers/google.mjs +108 -0
- package/dist/social-providers/huggingface.d.mts +85 -0
- package/dist/social-providers/huggingface.mjs +75 -0
- package/dist/social-providers/index.d.mts +1723 -1
- package/dist/social-providers/index.mjs +33 -2569
- package/dist/social-providers/kakao.d.mts +163 -0
- package/dist/social-providers/kakao.mjs +72 -0
- package/dist/social-providers/kick.d.mts +75 -0
- package/dist/social-providers/kick.mjs +71 -0
- package/dist/social-providers/line.d.mts +107 -0
- package/dist/social-providers/line.mjs +113 -0
- package/dist/social-providers/linear.d.mts +70 -0
- package/dist/social-providers/linear.mjs +88 -0
- package/dist/social-providers/linkedin.d.mts +69 -0
- package/dist/social-providers/linkedin.mjs +76 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
- package/dist/social-providers/microsoft-entra-id.mjs +106 -0
- package/dist/social-providers/naver.d.mts +104 -0
- package/dist/social-providers/naver.mjs +67 -0
- package/dist/social-providers/notion.d.mts +66 -0
- package/dist/social-providers/notion.mjs +75 -0
- package/dist/social-providers/paybin.d.mts +73 -0
- package/dist/social-providers/paybin.mjs +85 -0
- package/dist/social-providers/paypal.d.mts +131 -0
- package/dist/social-providers/paypal.mjs +144 -0
- package/dist/social-providers/polar.d.mts +76 -0
- package/dist/social-providers/polar.mjs +73 -0
- package/dist/social-providers/reddit.d.mts +64 -0
- package/dist/social-providers/reddit.mjs +83 -0
- package/dist/social-providers/roblox.d.mts +72 -0
- package/dist/social-providers/roblox.mjs +59 -0
- package/dist/social-providers/salesforce.d.mts +81 -0
- package/dist/social-providers/salesforce.mjs +91 -0
- package/dist/social-providers/slack.d.mts +85 -0
- package/dist/social-providers/slack.mjs +68 -0
- package/dist/social-providers/spotify.d.mts +65 -0
- package/dist/social-providers/spotify.mjs +71 -0
- package/dist/social-providers/tiktok.d.mts +171 -0
- package/dist/social-providers/tiktok.mjs +62 -0
- package/dist/social-providers/twitch.d.mts +81 -0
- package/dist/social-providers/twitch.mjs +78 -0
- package/dist/social-providers/twitter.d.mts +140 -0
- package/dist/social-providers/twitter.mjs +87 -0
- package/dist/social-providers/vercel.d.mts +64 -0
- package/dist/social-providers/vercel.mjs +61 -0
- package/dist/social-providers/vk.d.mts +72 -0
- package/dist/social-providers/vk.mjs +83 -0
- package/dist/social-providers/zoom.d.mts +173 -0
- package/dist/social-providers/zoom.mjs +72 -0
- package/dist/types/context.d.mts +246 -0
- package/dist/types/cookie.d.mts +23 -0
- package/dist/types/helper.d.mts +8 -0
- package/dist/types/index.d.mts +8 -0
- package/dist/types/init-options.d.mts +1266 -0
- package/dist/types/plugin-client.d.mts +110 -0
- package/dist/types/plugin.d.mts +124 -0
- package/dist/utils/deprecate.d.mts +10 -0
- package/dist/utils/deprecate.mjs +17 -0
- package/dist/utils/{index.d.mts → error-codes.d.mts} +1 -19
- package/dist/utils/error-codes.mjs +11 -0
- package/dist/utils/id.d.mts +4 -0
- package/dist/utils/id.mjs +9 -0
- package/dist/utils/json.d.mts +4 -0
- package/dist/utils/json.mjs +25 -0
- package/dist/utils/string.d.mts +4 -0
- package/dist/utils/string.mjs +7 -0
- package/package.json +9 -6
- package/src/db/adapter/get-id-field.ts +1 -1
- package/src/error/codes.ts +1 -1
- package/src/oauth2/create-authorization-url.ts +1 -1
- package/src/oauth2/oauth-provider.ts +6 -0
- package/tsdown.config.ts +3 -1
- package/dist/context-BBNwughv.mjs +0 -133
- package/dist/env-DbssmzoK.mjs +0 -245
- package/dist/index-B5x_W0dM.d.mts +0 -8054
- package/dist/oauth2-BjWM15hm.mjs +0 -326
- package/dist/utils/index.mjs +0 -4
- package/dist/utils-puAL36Bz.mjs +0 -63
- package/src/utils/index.ts +0 -5
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { LiteralString } from "./helper.mjs";
|
|
2
|
+
import { BetterAuthPlugin } from "./plugin.mjs";
|
|
3
|
+
import { BetterAuthOptions } from "./init-options.mjs";
|
|
4
|
+
import { BetterFetch, BetterFetchOption, BetterFetchPlugin } from "@better-fetch/fetch";
|
|
5
|
+
import { Atom, WritableAtom } from "nanostores";
|
|
6
|
+
|
|
7
|
+
//#region src/types/plugin-client.d.ts
|
|
8
|
+
interface ClientStore {
|
|
9
|
+
notify: (signal: string) => void;
|
|
10
|
+
listen: (signal: string, listener: () => void) => void;
|
|
11
|
+
atoms: Record<string, WritableAtom<any>>;
|
|
12
|
+
}
|
|
13
|
+
type ClientAtomListener = {
|
|
14
|
+
matcher: (path: string) => boolean;
|
|
15
|
+
signal: "$sessionSignal" | Omit<string, "$sessionSignal">;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Better-Fetch options but with additional options for the auth-client.
|
|
19
|
+
*/
|
|
20
|
+
type ClientFetchOption<Body = any, Query extends Record<string, any> = any, Params extends Record<string, any> | Array<string> | undefined = any, Res = any> = BetterFetchOption<Body, Query, Params, Res> & {
|
|
21
|
+
/**
|
|
22
|
+
* Certain endpoints, upon successful response, will trigger atom signals and thus rerendering all hooks related to that atom.
|
|
23
|
+
*
|
|
24
|
+
* This option is useful when you want to skip hook rerenders.
|
|
25
|
+
*/
|
|
26
|
+
disableSignal?: boolean | undefined;
|
|
27
|
+
};
|
|
28
|
+
interface RevalidateOptions {
|
|
29
|
+
/**
|
|
30
|
+
* A time interval (in seconds) after which the session will be re-fetched.
|
|
31
|
+
* If set to `0` (default), the session is not polled.
|
|
32
|
+
*
|
|
33
|
+
* This helps prevent session expiry during idle periods by periodically
|
|
34
|
+
* refreshing the session.
|
|
35
|
+
*
|
|
36
|
+
* @default 0
|
|
37
|
+
*/
|
|
38
|
+
refetchInterval?: number | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Automatically refetch the session when the user switches back to the window/tab.
|
|
41
|
+
* This option activates this behavior if set to `true` (default).
|
|
42
|
+
*
|
|
43
|
+
* Prevents expired sessions when users switch tabs and come back later.
|
|
44
|
+
*
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
refetchOnWindowFocus?: boolean | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Set to `false` to stop polling when the device has no internet access
|
|
50
|
+
* (determined by `navigator.onLine`).
|
|
51
|
+
*
|
|
52
|
+
* @default false
|
|
53
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
|
|
54
|
+
*/
|
|
55
|
+
refetchWhenOffline?: boolean | undefined;
|
|
56
|
+
}
|
|
57
|
+
interface BetterAuthClientOptions {
|
|
58
|
+
fetchOptions?: ClientFetchOption | undefined;
|
|
59
|
+
plugins?: BetterAuthClientPlugin[] | undefined;
|
|
60
|
+
baseURL?: string | undefined;
|
|
61
|
+
basePath?: string | undefined;
|
|
62
|
+
disableDefaultFetchPlugins?: boolean | undefined;
|
|
63
|
+
$InferAuth?: BetterAuthOptions | undefined;
|
|
64
|
+
sessionOptions?: RevalidateOptions | undefined;
|
|
65
|
+
}
|
|
66
|
+
interface BetterAuthClientPlugin {
|
|
67
|
+
id: LiteralString;
|
|
68
|
+
/**
|
|
69
|
+
* only used for type inference. don't pass the
|
|
70
|
+
* actual plugin
|
|
71
|
+
*/
|
|
72
|
+
$InferServerPlugin?: BetterAuthPlugin | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Custom actions
|
|
75
|
+
*/
|
|
76
|
+
getActions?: ($fetch: BetterFetch, $store: ClientStore,
|
|
77
|
+
/**
|
|
78
|
+
* better-auth client options
|
|
79
|
+
*/
|
|
80
|
+
options: BetterAuthClientOptions | undefined) => Record<string, any>;
|
|
81
|
+
/**
|
|
82
|
+
* State atoms that'll be resolved by each framework
|
|
83
|
+
* auth store.
|
|
84
|
+
*/
|
|
85
|
+
getAtoms?: (($fetch: BetterFetch) => Record<string, Atom<any>>) | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* specify path methods for server plugin inferred
|
|
88
|
+
* endpoints to force a specific method.
|
|
89
|
+
*/
|
|
90
|
+
pathMethods?: Record<string, "POST" | "GET"> | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* Better fetch plugins
|
|
93
|
+
*/
|
|
94
|
+
fetchPlugins?: BetterFetchPlugin[] | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* a list of recaller based on a matcher function.
|
|
97
|
+
* The signal name needs to match a signal in this
|
|
98
|
+
* plugin or any plugin the user might have added.
|
|
99
|
+
*/
|
|
100
|
+
atomListeners?: ClientAtomListener[] | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* The error codes returned by the plugin
|
|
103
|
+
*/
|
|
104
|
+
$ERROR_CODES?: Record<string, {
|
|
105
|
+
code: string;
|
|
106
|
+
message: string;
|
|
107
|
+
}>;
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
export { BetterAuthClientOptions, BetterAuthClientPlugin, ClientAtomListener, ClientFetchOption, ClientStore };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { BetterAuthPluginDBSchema } from "../db/plugin.mjs";
|
|
2
|
+
import "../db/index.mjs";
|
|
3
|
+
import { Awaitable, LiteralString } from "./helper.mjs";
|
|
4
|
+
import { BetterAuthOptions } from "./init-options.mjs";
|
|
5
|
+
import { AuthContext } from "./context.mjs";
|
|
6
|
+
import { AuthMiddleware } from "../api/index.mjs";
|
|
7
|
+
import { Endpoint, EndpointContext, InputContext, Middleware } from "better-call";
|
|
8
|
+
import { Migration } from "kysely";
|
|
9
|
+
|
|
10
|
+
//#region src/types/plugin.d.ts
|
|
11
|
+
type DeepPartial<T> = T extends Function ? T : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;
|
|
12
|
+
type HookEndpointContext = Partial<EndpointContext<string, any> & Omit<InputContext<string, any>, "method">> & {
|
|
13
|
+
path?: string;
|
|
14
|
+
context: AuthContext & {
|
|
15
|
+
returned?: unknown | undefined;
|
|
16
|
+
responseHeaders?: Headers | undefined;
|
|
17
|
+
};
|
|
18
|
+
headers?: Headers | undefined;
|
|
19
|
+
};
|
|
20
|
+
type BetterAuthPlugin = {
|
|
21
|
+
id: LiteralString;
|
|
22
|
+
/**
|
|
23
|
+
* The init function is called when the plugin is initialized.
|
|
24
|
+
* You can return a new context or modify the existing context.
|
|
25
|
+
*/
|
|
26
|
+
init?: ((ctx: AuthContext) => Awaitable<{
|
|
27
|
+
context?: DeepPartial<Omit<AuthContext, "options">>;
|
|
28
|
+
options?: Partial<BetterAuthOptions>;
|
|
29
|
+
}> | void | Promise<void>) | undefined;
|
|
30
|
+
endpoints?: {
|
|
31
|
+
[key: string]: Endpoint;
|
|
32
|
+
} | undefined;
|
|
33
|
+
middlewares?: {
|
|
34
|
+
path: string;
|
|
35
|
+
middleware: Middleware;
|
|
36
|
+
}[] | undefined;
|
|
37
|
+
onRequest?: ((request: Request, ctx: AuthContext) => Promise<{
|
|
38
|
+
response: Response;
|
|
39
|
+
} | {
|
|
40
|
+
request: Request;
|
|
41
|
+
} | void>) | undefined;
|
|
42
|
+
onResponse?: ((response: Response, ctx: AuthContext) => Promise<{
|
|
43
|
+
response: Response;
|
|
44
|
+
} | void>) | undefined;
|
|
45
|
+
hooks?: {
|
|
46
|
+
before?: {
|
|
47
|
+
matcher: (context: HookEndpointContext) => boolean;
|
|
48
|
+
handler: AuthMiddleware;
|
|
49
|
+
}[];
|
|
50
|
+
after?: {
|
|
51
|
+
matcher: (context: HookEndpointContext) => boolean;
|
|
52
|
+
handler: AuthMiddleware;
|
|
53
|
+
}[];
|
|
54
|
+
} | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Schema the plugin needs
|
|
57
|
+
*
|
|
58
|
+
* This will also be used to migrate the database. If the fields are dynamic from the plugins
|
|
59
|
+
* configuration each time the configuration is changed a new migration will be created.
|
|
60
|
+
*
|
|
61
|
+
* NOTE: If you want to create migrations manually using
|
|
62
|
+
* migrations option or any other way you
|
|
63
|
+
* can disable migration per table basis.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* schema: {
|
|
68
|
+
* user: {
|
|
69
|
+
* fields: {
|
|
70
|
+
* email: {
|
|
71
|
+
* type: "string",
|
|
72
|
+
* },
|
|
73
|
+
* emailVerified: {
|
|
74
|
+
* type: "boolean",
|
|
75
|
+
* defaultValue: false,
|
|
76
|
+
* },
|
|
77
|
+
* },
|
|
78
|
+
* }
|
|
79
|
+
* } as AuthPluginSchema
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
schema?: BetterAuthPluginDBSchema | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The migrations of the plugin. If you define schema that will automatically create
|
|
85
|
+
* migrations for you.
|
|
86
|
+
*
|
|
87
|
+
* ⚠️ Only uses this if you dont't want to use the schema option and you disabled migrations for
|
|
88
|
+
* the tables.
|
|
89
|
+
*/
|
|
90
|
+
migrations?: Record<string, Migration> | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* The options of the plugin
|
|
93
|
+
*/
|
|
94
|
+
options?: Record<string, any> | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* types to be inferred
|
|
97
|
+
*/
|
|
98
|
+
$Infer?: Record<string, any> | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* The rate limit rules to apply to specific paths.
|
|
101
|
+
*/
|
|
102
|
+
rateLimit?: {
|
|
103
|
+
window: number;
|
|
104
|
+
max: number;
|
|
105
|
+
pathMatcher: (path: string) => boolean;
|
|
106
|
+
}[] | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* The error codes returned by the plugin
|
|
109
|
+
*/
|
|
110
|
+
$ERROR_CODES?: Record<string, {
|
|
111
|
+
code: string;
|
|
112
|
+
message: string;
|
|
113
|
+
}> | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* All database operations that are performed by the plugin
|
|
116
|
+
*
|
|
117
|
+
* This will override the default database operations
|
|
118
|
+
*/
|
|
119
|
+
adapter?: {
|
|
120
|
+
[key: string]: (...args: any[]) => Awaitable<any>;
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
//#endregion
|
|
124
|
+
export { BetterAuthPlugin, HookEndpointContext };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { InternalLogger } from "../env/logger.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/deprecate.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wraps a function to log a deprecation warning at once.
|
|
7
|
+
*/
|
|
8
|
+
declare function deprecate<T extends (...args: any[]) => any>(fn: T, message: string, logger?: InternalLogger): T;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { deprecate };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/utils/deprecate.ts
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a function to log a deprecation warning at once.
|
|
4
|
+
*/
|
|
5
|
+
function deprecate(fn, message, logger) {
|
|
6
|
+
let warned = false;
|
|
7
|
+
return function(...args) {
|
|
8
|
+
if (!warned) {
|
|
9
|
+
(logger?.warn ?? console.warn)(`[Deprecation] ${message}`);
|
|
10
|
+
warned = true;
|
|
11
|
+
}
|
|
12
|
+
return fn.apply(this, args);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { deprecate };
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import { t as InternalLogger } from "../index-BRBu0-5h.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/utils/deprecate.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Wraps a function to log a deprecation warning at once.
|
|
7
|
-
*/
|
|
8
|
-
declare function deprecate<T extends (...args: any[]) => any>(fn: T, message: string, logger?: InternalLogger): T;
|
|
9
|
-
//#endregion
|
|
10
1
|
//#region src/utils/error-codes.d.ts
|
|
11
2
|
type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
|
|
12
3
|
type SpecialCharacter = "_";
|
|
@@ -18,13 +9,4 @@ declare function defineErrorCodes<const T extends Record<string, string>>(codes:
|
|
|
18
9
|
message: T[K];
|
|
19
10
|
} };
|
|
20
11
|
//#endregion
|
|
21
|
-
|
|
22
|
-
declare const generateId: (size?: number) => string;
|
|
23
|
-
//#endregion
|
|
24
|
-
//#region src/utils/json.d.ts
|
|
25
|
-
declare function safeJSONParse<T>(data: unknown): T | null;
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/utils/string.d.ts
|
|
28
|
-
declare function capitalizeFirstLetter(str: string): string;
|
|
29
|
-
//#endregion
|
|
30
|
-
export { capitalizeFirstLetter, defineErrorCodes, deprecate, generateId, safeJSONParse };
|
|
12
|
+
export { defineErrorCodes };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/utils/error-codes.ts
|
|
2
|
+
function defineErrorCodes(codes) {
|
|
3
|
+
return Object.fromEntries(Object.entries(codes).map(([key, value]) => [key, {
|
|
4
|
+
code: key,
|
|
5
|
+
message: value,
|
|
6
|
+
toString: () => value
|
|
7
|
+
}]));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { defineErrorCodes };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { logger } from "../env/logger.mjs";
|
|
2
|
+
import "../env/index.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/json.ts
|
|
5
|
+
function safeJSONParse(data) {
|
|
6
|
+
function reviver(_, value) {
|
|
7
|
+
if (typeof value === "string") {
|
|
8
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value)) {
|
|
9
|
+
const date = new Date(value);
|
|
10
|
+
if (!isNaN(date.getTime())) return date;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
if (typeof data !== "string") return data;
|
|
17
|
+
return JSON.parse(data, reviver);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
logger.error("Error parsing JSON", { error: e });
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { safeJSONParse };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/core",
|
|
3
|
-
"version": "1.5.0-beta.
|
|
3
|
+
"version": "1.5.0-beta.5",
|
|
4
4
|
"description": "The most comprehensive authentication framework for TypeScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"types": "./dist/error/index.d.mts",
|
|
49
49
|
"default": "./dist/error/index.mjs"
|
|
50
50
|
},
|
|
51
|
-
"./utils": {
|
|
52
|
-
"dev-source": "./src/utils
|
|
53
|
-
"types": "./dist/utils
|
|
54
|
-
"default": "./dist/utils
|
|
51
|
+
"./utils/*": {
|
|
52
|
+
"dev-source": "./src/utils/*.ts",
|
|
53
|
+
"types": "./dist/utils/*.d.mts",
|
|
54
|
+
"default": "./dist/utils/*.mjs"
|
|
55
55
|
},
|
|
56
56
|
"./social-providers": {
|
|
57
57
|
"dev-source": "./src/social-providers/index.ts",
|
|
@@ -97,6 +97,9 @@
|
|
|
97
97
|
"utils": [
|
|
98
98
|
"dist/utils/index.d.mts"
|
|
99
99
|
],
|
|
100
|
+
"utils/*": [
|
|
101
|
+
"dist/utils/*.d.mts"
|
|
102
|
+
],
|
|
100
103
|
"social-providers": [
|
|
101
104
|
"dist/social-providers/index.d.mts"
|
|
102
105
|
],
|
|
@@ -118,7 +121,7 @@
|
|
|
118
121
|
"jose": "^6.1.0",
|
|
119
122
|
"kysely": "^0.28.5",
|
|
120
123
|
"nanostores": "^1.0.1",
|
|
121
|
-
"tsdown": "^0.
|
|
124
|
+
"tsdown": "^0.19.0"
|
|
122
125
|
},
|
|
123
126
|
"dependencies": {
|
|
124
127
|
"@standard-schema/spec": "^1.0.0",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { logger } from "../../env";
|
|
2
2
|
import type { BetterAuthOptions } from "../../types";
|
|
3
|
-
import { generateId as defaultGenerateId } from "../../utils";
|
|
3
|
+
import { generateId as defaultGenerateId } from "../../utils/id";
|
|
4
4
|
import type { BetterAuthDBSchema, DBFieldAttribute } from "../type";
|
|
5
5
|
import { initGetDefaultModelName } from "./get-default-model-name";
|
|
6
6
|
|
package/src/error/codes.ts
CHANGED
|
@@ -40,7 +40,7 @@ export async function createAuthorizationURL({
|
|
|
40
40
|
additionalParams?: Record<string, string> | undefined;
|
|
41
41
|
scopeJoiner?: string | undefined;
|
|
42
42
|
}) {
|
|
43
|
-
const url = new URL(authorizationEndpoint);
|
|
43
|
+
const url = new URL(options.authorizationEndpoint || authorizationEndpoint);
|
|
44
44
|
url.searchParams.set("response_type", responseType || "code");
|
|
45
45
|
const primaryClientId = Array.isArray(options.clientId)
|
|
46
46
|
? options.clientId[0]
|
|
@@ -119,6 +119,12 @@ export type ProviderOptions<Profile extends Record<string, any> = any> = {
|
|
|
119
119
|
* whitelisted in the provider's dashboard.
|
|
120
120
|
*/
|
|
121
121
|
redirectURI?: string | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Custom authorization endpoint URL.
|
|
124
|
+
* Use this to override the default authorization endpoint of the provider.
|
|
125
|
+
* Useful for testing with local OAuth servers or using sandbox environments.
|
|
126
|
+
*/
|
|
127
|
+
authorizationEndpoint?: string | undefined;
|
|
122
128
|
/**
|
|
123
129
|
* The client key of your application
|
|
124
130
|
* Tiktok Social Provider uses this field instead of clientId
|
package/tsdown.config.ts
CHANGED
|
@@ -14,9 +14,11 @@ export default defineConfig({
|
|
|
14
14
|
"./src/oauth2/index.ts",
|
|
15
15
|
"./src/api/index.ts",
|
|
16
16
|
"./src/social-providers/index.ts",
|
|
17
|
-
"./src/utils
|
|
17
|
+
"./src/utils/*.ts",
|
|
18
|
+
"!./src/utils/*.test.ts",
|
|
18
19
|
"./src/error/index.ts",
|
|
19
20
|
],
|
|
20
21
|
external: ["@better-auth/core/async_hooks"],
|
|
22
|
+
unbundle: true,
|
|
21
23
|
clean: true,
|
|
22
24
|
});
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
|
|
2
|
-
|
|
3
|
-
//#region src/context/endpoint-context.ts
|
|
4
|
-
let currentContextAsyncStorage = null;
|
|
5
|
-
const ensureAsyncStorage$2 = async () => {
|
|
6
|
-
if (!currentContextAsyncStorage) currentContextAsyncStorage = new (await (getAsyncLocalStorage()))();
|
|
7
|
-
return currentContextAsyncStorage;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* This is for internal use only. Most users should use `getCurrentAuthContext` instead.
|
|
11
|
-
*
|
|
12
|
-
* It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
|
|
13
|
-
*/
|
|
14
|
-
async function getCurrentAuthContextAsyncLocalStorage() {
|
|
15
|
-
return ensureAsyncStorage$2();
|
|
16
|
-
}
|
|
17
|
-
async function getCurrentAuthContext() {
|
|
18
|
-
const context = (await ensureAsyncStorage$2()).getStore();
|
|
19
|
-
if (!context) throw new Error("No auth context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.");
|
|
20
|
-
return context;
|
|
21
|
-
}
|
|
22
|
-
async function runWithEndpointContext(context, fn) {
|
|
23
|
-
return (await ensureAsyncStorage$2()).run(context, fn);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/context/request-state.ts
|
|
28
|
-
let requestStateAsyncStorage = null;
|
|
29
|
-
const ensureAsyncStorage$1 = async () => {
|
|
30
|
-
if (!requestStateAsyncStorage) requestStateAsyncStorage = new (await (getAsyncLocalStorage()))();
|
|
31
|
-
return requestStateAsyncStorage;
|
|
32
|
-
};
|
|
33
|
-
async function getRequestStateAsyncLocalStorage() {
|
|
34
|
-
return ensureAsyncStorage$1();
|
|
35
|
-
}
|
|
36
|
-
async function hasRequestState() {
|
|
37
|
-
return (await ensureAsyncStorage$1()).getStore() !== void 0;
|
|
38
|
-
}
|
|
39
|
-
async function getCurrentRequestState() {
|
|
40
|
-
const store = (await ensureAsyncStorage$1()).getStore();
|
|
41
|
-
if (!store) throw new Error("No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.");
|
|
42
|
-
return store;
|
|
43
|
-
}
|
|
44
|
-
async function runWithRequestState(store, fn) {
|
|
45
|
-
return (await ensureAsyncStorage$1()).run(store, fn);
|
|
46
|
-
}
|
|
47
|
-
function defineRequestState(initFn) {
|
|
48
|
-
const ref = Object.freeze({});
|
|
49
|
-
return {
|
|
50
|
-
get ref() {
|
|
51
|
-
return ref;
|
|
52
|
-
},
|
|
53
|
-
async get() {
|
|
54
|
-
const store = await getCurrentRequestState();
|
|
55
|
-
if (!store.has(ref)) {
|
|
56
|
-
const initialValue = await initFn();
|
|
57
|
-
store.set(ref, initialValue);
|
|
58
|
-
return initialValue;
|
|
59
|
-
}
|
|
60
|
-
return store.get(ref);
|
|
61
|
-
},
|
|
62
|
-
async set(value) {
|
|
63
|
-
(await getCurrentRequestState()).set(ref, value);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
//#endregion
|
|
69
|
-
//#region src/context/transaction.ts
|
|
70
|
-
let currentAdapterAsyncStorage = null;
|
|
71
|
-
const ensureAsyncStorage = async () => {
|
|
72
|
-
if (!currentAdapterAsyncStorage) currentAdapterAsyncStorage = new (await (getAsyncLocalStorage()))();
|
|
73
|
-
return currentAdapterAsyncStorage;
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* This is for internal use only. Most users should use `getCurrentAdapter` instead.
|
|
77
|
-
*
|
|
78
|
-
* It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
|
|
79
|
-
*/
|
|
80
|
-
const getCurrentDBAdapterAsyncLocalStorage = async () => {
|
|
81
|
-
return ensureAsyncStorage();
|
|
82
|
-
};
|
|
83
|
-
const getCurrentAdapter = async (fallback) => {
|
|
84
|
-
return ensureAsyncStorage().then((als) => {
|
|
85
|
-
return als.getStore() || fallback;
|
|
86
|
-
}).catch(() => {
|
|
87
|
-
return fallback;
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
const runWithAdapter = async (adapter, fn) => {
|
|
91
|
-
let called = true;
|
|
92
|
-
return ensureAsyncStorage().then((als) => {
|
|
93
|
-
called = true;
|
|
94
|
-
return als.run(adapter, fn);
|
|
95
|
-
}).catch((err) => {
|
|
96
|
-
if (!called) return fn();
|
|
97
|
-
throw err;
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
const runWithTransaction = async (adapter, fn) => {
|
|
101
|
-
let called = true;
|
|
102
|
-
return ensureAsyncStorage().then((als) => {
|
|
103
|
-
called = true;
|
|
104
|
-
return adapter.transaction(async (trx) => {
|
|
105
|
-
return als.run(trx, fn);
|
|
106
|
-
});
|
|
107
|
-
}).catch((err) => {
|
|
108
|
-
if (!called) return fn();
|
|
109
|
-
throw err;
|
|
110
|
-
});
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
//#endregion
|
|
114
|
-
//#region src/context/index.ts
|
|
115
|
-
const glo = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
|
|
116
|
-
const importIdentifier = "__ $BETTER_AUTH$ __";
|
|
117
|
-
if (glo[importIdentifier] === true)
|
|
118
|
-
/**
|
|
119
|
-
* Dear reader of this message. Please take this seriously.
|
|
120
|
-
*
|
|
121
|
-
* If you see this message, make sure that you only import one version of Better Auth. In many cases,
|
|
122
|
-
* your package manager installs two versions of Better Auth that are used by different packages within your project.
|
|
123
|
-
*
|
|
124
|
-
* This often leads to issues that are hard to debug. We often need to ensure async local storage instance,
|
|
125
|
-
* If you imported different versions of Better Auth, it is impossible for us to
|
|
126
|
-
* do status synchronization per request anymore - which might break the states.
|
|
127
|
-
*
|
|
128
|
-
*/
|
|
129
|
-
console.error("Better Auth was already imported. This breaks async local storage instance and will lead to issues!");
|
|
130
|
-
glo[importIdentifier] = true;
|
|
131
|
-
|
|
132
|
-
//#endregion
|
|
133
|
-
export { defineRequestState as a, hasRequestState as c, getCurrentAuthContextAsyncLocalStorage as d, runWithEndpointContext as f, runWithTransaction as i, runWithRequestState as l, getCurrentDBAdapterAsyncLocalStorage as n, getCurrentRequestState as o, runWithAdapter as r, getRequestStateAsyncLocalStorage as s, getCurrentAdapter as t, getCurrentAuthContext as u };
|