@depup/better-auth__core 1.6.23-depup.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +32 -0
- package/changes.json +14 -0
- package/dist/api/index.d.mts +281 -0
- package/dist/api/index.mjs +92 -0
- package/dist/async_hooks/index.d.mts +6 -0
- package/dist/async_hooks/index.mjs +20 -0
- package/dist/async_hooks/pure.index.d.mts +6 -0
- package/dist/async_hooks/pure.index.mjs +33 -0
- package/dist/context/endpoint-context.d.mts +18 -0
- package/dist/context/endpoint-context.mjs +29 -0
- package/dist/context/global.d.mts +6 -0
- package/dist/context/global.mjs +36 -0
- package/dist/context/index.d.mts +5 -0
- package/dist/context/index.mjs +5 -0
- package/dist/context/request-state.d.mts +27 -0
- package/dist/context/request-state.mjs +47 -0
- package/dist/context/transaction.d.mts +27 -0
- package/dist/context/transaction.mjs +96 -0
- package/dist/db/adapter/factory.d.mts +17 -0
- package/dist/db/adapter/factory.mjs +905 -0
- package/dist/db/adapter/get-default-field-name.d.mts +18 -0
- package/dist/db/adapter/get-default-field-name.mjs +36 -0
- package/dist/db/adapter/get-default-model-name.d.mts +12 -0
- package/dist/db/adapter/get-default-model-name.mjs +30 -0
- package/dist/db/adapter/get-field-attributes.d.mts +26 -0
- package/dist/db/adapter/get-field-attributes.mjs +37 -0
- package/dist/db/adapter/get-field-name.d.mts +18 -0
- package/dist/db/adapter/get-field-name.mjs +31 -0
- package/dist/db/adapter/get-id-field.d.mts +36 -0
- package/dist/db/adapter/get-id-field.mjs +64 -0
- package/dist/db/adapter/get-model-name.d.mts +12 -0
- package/dist/db/adapter/get-model-name.mjs +21 -0
- package/dist/db/adapter/index.d.mts +615 -0
- package/dist/db/adapter/index.mjs +24 -0
- package/dist/db/adapter/types.d.mts +105 -0
- package/dist/db/adapter/utils.d.mts +7 -0
- package/dist/db/adapter/utils.mjs +37 -0
- package/dist/db/get-tables.d.mts +6 -0
- package/dist/db/get-tables.mjs +266 -0
- package/dist/db/index.d.mts +10 -0
- package/dist/db/index.mjs +8 -0
- package/dist/db/plugin.d.mts +12 -0
- package/dist/db/schema/account.d.mts +28 -0
- package/dist/db/schema/account.mjs +17 -0
- package/dist/db/schema/rate-limit.d.mts +18 -0
- package/dist/db/schema/rate-limit.mjs +9 -0
- package/dist/db/schema/session.d.mts +23 -0
- package/dist/db/schema/session.mjs +12 -0
- package/dist/db/schema/shared.d.mts +10 -0
- package/dist/db/schema/shared.mjs +9 -0
- package/dist/db/schema/user.d.mts +22 -0
- package/dist/db/schema/user.mjs +11 -0
- package/dist/db/schema/verification.d.mts +21 -0
- package/dist/db/schema/verification.mjs +10 -0
- package/dist/db/type.d.mts +194 -0
- package/dist/env/color-depth.d.mts +4 -0
- package/dist/env/color-depth.mjs +86 -0
- package/dist/env/env-impl.d.mts +32 -0
- package/dist/env/env-impl.mjs +81 -0
- package/dist/env/index.d.mts +4 -0
- package/dist/env/index.mjs +4 -0
- package/dist/env/logger.d.mts +48 -0
- package/dist/env/logger.mjs +79 -0
- package/dist/error/codes.d.mts +69 -0
- package/dist/error/codes.mjs +55 -0
- package/dist/error/index.d.mts +26 -0
- package/dist/error/index.mjs +27 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.mjs +1 -0
- package/dist/instrumentation/api.mjs +12 -0
- package/dist/instrumentation/attributes.d.mts +11 -0
- package/dist/instrumentation/attributes.mjs +10 -0
- package/dist/instrumentation/index.d.mts +3 -0
- package/dist/instrumentation/index.mjs +3 -0
- package/dist/instrumentation/noop.mjs +42 -0
- package/dist/instrumentation/pure.index.d.mts +7 -0
- package/dist/instrumentation/pure.index.mjs +7 -0
- package/dist/instrumentation/tracer.d.mts +13 -0
- package/dist/instrumentation/tracer.mjs +53 -0
- package/dist/oauth2/client-credentials-token.d.mts +56 -0
- package/dist/oauth2/client-credentials-token.mjs +64 -0
- package/dist/oauth2/create-authorization-url.d.mts +44 -0
- package/dist/oauth2/create-authorization-url.mjs +41 -0
- package/dist/oauth2/index.d.mts +8 -0
- package/dist/oauth2/index.mjs +7 -0
- package/dist/oauth2/oauth-provider.d.mts +192 -0
- package/dist/oauth2/refresh-access-token.d.mts +54 -0
- package/dist/oauth2/refresh-access-token.mjs +73 -0
- package/dist/oauth2/reject-redirects.mjs +60 -0
- package/dist/oauth2/utils.d.mts +24 -0
- package/dist/oauth2/utils.mjs +48 -0
- package/dist/oauth2/validate-authorization-code.d.mts +85 -0
- package/dist/oauth2/validate-authorization-code.mjs +86 -0
- package/dist/oauth2/verify.d.mts +65 -0
- package/dist/oauth2/verify.mjs +202 -0
- package/dist/social-providers/apple.d.mts +133 -0
- package/dist/social-providers/apple.mjs +117 -0
- package/dist/social-providers/atlassian.d.mts +70 -0
- package/dist/social-providers/atlassian.mjs +80 -0
- package/dist/social-providers/cognito.d.mts +85 -0
- package/dist/social-providers/cognito.mjs +162 -0
- package/dist/social-providers/discord.d.mts +124 -0
- package/dist/social-providers/discord.mjs +62 -0
- package/dist/social-providers/dropbox.d.mts +69 -0
- package/dist/social-providers/dropbox.mjs +72 -0
- package/dist/social-providers/facebook.d.mts +79 -0
- package/dist/social-providers/facebook.mjs +157 -0
- package/dist/social-providers/figma.d.mts +61 -0
- package/dist/social-providers/figma.mjs +83 -0
- package/dist/social-providers/github.d.mts +102 -0
- package/dist/social-providers/github.mjs +92 -0
- package/dist/social-providers/gitlab.d.mts +123 -0
- package/dist/social-providers/gitlab.mjs +79 -0
- package/dist/social-providers/google.d.mts +123 -0
- package/dist/social-providers/google.mjs +137 -0
- package/dist/social-providers/huggingface.d.mts +83 -0
- package/dist/social-providers/huggingface.mjs +73 -0
- package/dist/social-providers/index.d.mts +1834 -0
- package/dist/social-providers/index.mjs +78 -0
- package/dist/social-providers/kakao.d.mts +161 -0
- package/dist/social-providers/kakao.mjs +70 -0
- package/dist/social-providers/kick.d.mts +73 -0
- package/dist/social-providers/kick.mjs +68 -0
- package/dist/social-providers/line.d.mts +105 -0
- package/dist/social-providers/line.mjs +110 -0
- package/dist/social-providers/linear.d.mts +68 -0
- package/dist/social-providers/linear.mjs +85 -0
- package/dist/social-providers/linkedin.d.mts +67 -0
- package/dist/social-providers/linkedin.mjs +73 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
- package/dist/social-providers/microsoft-entra-id.mjs +152 -0
- package/dist/social-providers/naver.d.mts +92 -0
- package/dist/social-providers/naver.mjs +65 -0
- package/dist/social-providers/notion.d.mts +64 -0
- package/dist/social-providers/notion.mjs +72 -0
- package/dist/social-providers/paybin.d.mts +71 -0
- package/dist/social-providers/paybin.mjs +81 -0
- package/dist/social-providers/paypal.d.mts +131 -0
- package/dist/social-providers/paypal.mjs +188 -0
- package/dist/social-providers/polar.d.mts +74 -0
- package/dist/social-providers/polar.mjs +71 -0
- package/dist/social-providers/railway.d.mts +65 -0
- package/dist/social-providers/railway.mjs +74 -0
- package/dist/social-providers/reddit.d.mts +62 -0
- package/dist/social-providers/reddit.mjs +81 -0
- package/dist/social-providers/roblox.d.mts +70 -0
- package/dist/social-providers/roblox.mjs +57 -0
- package/dist/social-providers/salesforce.d.mts +79 -0
- package/dist/social-providers/salesforce.mjs +87 -0
- package/dist/social-providers/slack.d.mts +83 -0
- package/dist/social-providers/slack.mjs +66 -0
- package/dist/social-providers/spotify.d.mts +63 -0
- package/dist/social-providers/spotify.mjs +69 -0
- package/dist/social-providers/tiktok.d.mts +168 -0
- package/dist/social-providers/tiktok.mjs +60 -0
- package/dist/social-providers/twitch.d.mts +79 -0
- package/dist/social-providers/twitch.mjs +75 -0
- package/dist/social-providers/twitter.d.mts +126 -0
- package/dist/social-providers/twitter.mjs +85 -0
- package/dist/social-providers/vercel.d.mts +62 -0
- package/dist/social-providers/vercel.mjs +58 -0
- package/dist/social-providers/vk.d.mts +70 -0
- package/dist/social-providers/vk.mjs +81 -0
- package/dist/social-providers/wechat.d.mts +113 -0
- package/dist/social-providers/wechat.mjs +81 -0
- package/dist/social-providers/zoom.d.mts +163 -0
- package/dist/social-providers/zoom.mjs +69 -0
- package/dist/types/context.d.mts +319 -0
- package/dist/types/cookie.d.mts +15 -0
- package/dist/types/helper.d.mts +10 -0
- package/dist/types/index.d.mts +9 -0
- package/dist/types/init-options.d.mts +1430 -0
- package/dist/types/plugin-client.d.mts +123 -0
- package/dist/types/plugin.d.mts +124 -0
- package/dist/types/secret.d.mts +11 -0
- package/dist/utils/async.d.mts +22 -0
- package/dist/utils/async.mjs +32 -0
- package/dist/utils/db.d.mts +9 -0
- package/dist/utils/db.mjs +15 -0
- package/dist/utils/deprecate.d.mts +9 -0
- package/dist/utils/deprecate.mjs +16 -0
- package/dist/utils/error-codes.d.mts +13 -0
- package/dist/utils/error-codes.mjs +10 -0
- package/dist/utils/fetch-metadata.d.mts +4 -0
- package/dist/utils/fetch-metadata.mjs +6 -0
- package/dist/utils/host.d.mts +147 -0
- package/dist/utils/host.mjs +295 -0
- package/dist/utils/id.d.mts +4 -0
- package/dist/utils/id.mjs +7 -0
- package/dist/utils/ip.d.mts +77 -0
- package/dist/utils/ip.mjs +230 -0
- package/dist/utils/is-api-error.d.mts +6 -0
- package/dist/utils/is-api-error.mjs +8 -0
- package/dist/utils/json.d.mts +4 -0
- package/dist/utils/json.mjs +41 -0
- package/dist/utils/redirect-uri.d.mts +20 -0
- package/dist/utils/redirect-uri.mjs +48 -0
- package/dist/utils/string.d.mts +8 -0
- package/dist/utils/string.mjs +25 -0
- package/dist/utils/url.d.mts +37 -0
- package/dist/utils/url.mjs +61 -0
- package/package.json +216 -0
- package/src/api/index.ts +185 -0
- package/src/async_hooks/index.ts +40 -0
- package/src/async_hooks/pure.index.ts +46 -0
- package/src/context/endpoint-context.ts +50 -0
- package/src/context/global.ts +57 -0
- package/src/context/index.ts +23 -0
- package/src/context/request-state.ts +91 -0
- package/src/context/transaction.ts +169 -0
- package/src/db/adapter/factory.ts +1740 -0
- package/src/db/adapter/get-default-field-name.ts +59 -0
- package/src/db/adapter/get-default-model-name.ts +51 -0
- package/src/db/adapter/get-field-attributes.ts +62 -0
- package/src/db/adapter/get-field-name.ts +43 -0
- package/src/db/adapter/get-id-field.ts +150 -0
- package/src/db/adapter/get-model-name.ts +36 -0
- package/src/db/adapter/index.ts +656 -0
- package/src/db/adapter/types.ts +134 -0
- package/src/db/adapter/utils.ts +61 -0
- package/src/db/get-tables.ts +304 -0
- package/src/db/index.ts +43 -0
- package/src/db/plugin.ts +11 -0
- package/src/db/schema/account.ts +47 -0
- package/src/db/schema/rate-limit.ts +36 -0
- package/src/db/schema/session.ts +29 -0
- package/src/db/schema/shared.ts +7 -0
- package/src/db/schema/user.ts +31 -0
- package/src/db/schema/verification.ts +28 -0
- package/src/db/type.ts +360 -0
- package/src/env/color-depth.ts +172 -0
- package/src/env/env-impl.ts +123 -0
- package/src/env/index.ts +23 -0
- package/src/env/logger.ts +145 -0
- package/src/error/codes.ts +72 -0
- package/src/error/index.ts +44 -0
- package/src/index.ts +1 -0
- package/src/instrumentation/api.ts +17 -0
- package/src/instrumentation/attributes.ts +22 -0
- package/src/instrumentation/index.ts +2 -0
- package/src/instrumentation/noop.ts +74 -0
- package/src/instrumentation/pure.index.ts +31 -0
- package/src/instrumentation/tracer.ts +95 -0
- package/src/oauth2/client-credentials-token.ts +126 -0
- package/src/oauth2/create-authorization-url.ts +89 -0
- package/src/oauth2/index.ts +34 -0
- package/src/oauth2/oauth-provider.ts +222 -0
- package/src/oauth2/refresh-access-token.ts +157 -0
- package/src/oauth2/reject-redirects.ts +70 -0
- package/src/oauth2/utils.ts +70 -0
- package/src/oauth2/validate-authorization-code.ts +190 -0
- package/src/oauth2/verify.ts +408 -0
- package/src/social-providers/apple.ts +249 -0
- package/src/social-providers/atlassian.ts +133 -0
- package/src/social-providers/cognito.ts +281 -0
- package/src/social-providers/discord.ts +170 -0
- package/src/social-providers/dropbox.ts +112 -0
- package/src/social-providers/facebook.ts +288 -0
- package/src/social-providers/figma.ts +118 -0
- package/src/social-providers/github.ts +184 -0
- package/src/social-providers/gitlab.ts +155 -0
- package/src/social-providers/google.ts +263 -0
- package/src/social-providers/huggingface.ts +119 -0
- package/src/social-providers/index.ts +132 -0
- package/src/social-providers/kakao.ts +179 -0
- package/src/social-providers/kick.ts +109 -0
- package/src/social-providers/line.ts +169 -0
- package/src/social-providers/linear.ts +121 -0
- package/src/social-providers/linkedin.ts +110 -0
- package/src/social-providers/microsoft-entra-id.ts +391 -0
- package/src/social-providers/naver.ts +113 -0
- package/src/social-providers/notion.ts +108 -0
- package/src/social-providers/paybin.ts +118 -0
- package/src/social-providers/paypal.ts +371 -0
- package/src/social-providers/polar.ts +111 -0
- package/src/social-providers/railway.ts +100 -0
- package/src/social-providers/reddit.ts +126 -0
- package/src/social-providers/roblox.ts +112 -0
- package/src/social-providers/salesforce.ts +159 -0
- package/src/social-providers/slack.ts +112 -0
- package/src/social-providers/spotify.ts +94 -0
- package/src/social-providers/tiktok.ts +211 -0
- package/src/social-providers/twitch.ts +112 -0
- package/src/social-providers/twitter.ts +199 -0
- package/src/social-providers/vercel.ts +87 -0
- package/src/social-providers/vk.ts +125 -0
- package/src/social-providers/wechat.ts +220 -0
- package/src/social-providers/zoom.ts +230 -0
- package/src/types/context.ts +460 -0
- package/src/types/cookie.ts +10 -0
- package/src/types/helper.ts +27 -0
- package/src/types/index.ts +40 -0
- package/src/types/init-options.ts +1679 -0
- package/src/types/plugin-client.ts +143 -0
- package/src/types/plugin.ts +163 -0
- package/src/types/secret.ts +8 -0
- package/src/utils/async.ts +53 -0
- package/src/utils/db.ts +20 -0
- package/src/utils/deprecate.ts +21 -0
- package/src/utils/error-codes.ts +68 -0
- package/src/utils/fetch-metadata.ts +3 -0
- package/src/utils/host.ts +416 -0
- package/src/utils/id.ts +5 -0
- package/src/utils/ip.ts +395 -0
- package/src/utils/is-api-error.ts +10 -0
- package/src/utils/json.ts +56 -0
- package/src/utils/redirect-uri.ts +54 -0
- package/src/utils/string.ts +40 -0
- package/src/utils/url.ts +77 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BetterFetch,
|
|
3
|
+
BetterFetchOption,
|
|
4
|
+
BetterFetchPlugin,
|
|
5
|
+
} from "@better-fetch/fetch";
|
|
6
|
+
import type { Atom, WritableAtom } from "nanostores";
|
|
7
|
+
import type { LiteralString } from "./helper";
|
|
8
|
+
import type { BetterAuthOptions } from "./init-options";
|
|
9
|
+
|
|
10
|
+
type InferableServerPlugin = {
|
|
11
|
+
id?: LiteralString | undefined;
|
|
12
|
+
endpoints?: Record<string, unknown> | undefined;
|
|
13
|
+
schema?: Record<string, { fields: Record<string, unknown> }> | undefined;
|
|
14
|
+
$ERROR_CODES?:
|
|
15
|
+
| Record<
|
|
16
|
+
string,
|
|
17
|
+
{
|
|
18
|
+
readonly code: string;
|
|
19
|
+
message: string;
|
|
20
|
+
}
|
|
21
|
+
>
|
|
22
|
+
| undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface ClientStore {
|
|
26
|
+
notify: (signal: string) => void;
|
|
27
|
+
listen: (signal: string, listener: () => void) => void;
|
|
28
|
+
atoms: Record<string, WritableAtom<any>>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ClientAtomListener = {
|
|
32
|
+
matcher: (path: string) => boolean;
|
|
33
|
+
signal: "$sessionSignal" | Omit<string, "$sessionSignal">;
|
|
34
|
+
callback?: (path: string) => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Better-Fetch options but with additional options for the auth-client.
|
|
39
|
+
*/
|
|
40
|
+
export type ClientFetchOption<
|
|
41
|
+
Body = any,
|
|
42
|
+
Query extends Record<string, any> = any,
|
|
43
|
+
Params extends Record<string, any> | Array<string> | undefined = any,
|
|
44
|
+
Res = any,
|
|
45
|
+
> = BetterFetchOption<Body, Query, Params, Res> & {
|
|
46
|
+
/**
|
|
47
|
+
* Certain endpoints, upon successful response, will trigger atom signals and thus rerendering all hooks related to that atom.
|
|
48
|
+
*
|
|
49
|
+
* This option is useful when you want to skip hook rerenders.
|
|
50
|
+
*/
|
|
51
|
+
disableSignal?: boolean | undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export interface RevalidateOptions {
|
|
55
|
+
/**
|
|
56
|
+
* A time interval (in seconds) after which the session will be re-fetched.
|
|
57
|
+
* If set to `0` (default), the session is not polled.
|
|
58
|
+
*
|
|
59
|
+
* This helps prevent session expiry during idle periods by periodically
|
|
60
|
+
* refreshing the session.
|
|
61
|
+
*
|
|
62
|
+
* @default 0
|
|
63
|
+
*/
|
|
64
|
+
refetchInterval?: number | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Automatically refetch the session when the user switches back to the window/tab.
|
|
67
|
+
* This option activates this behavior if set to `true` (default).
|
|
68
|
+
*
|
|
69
|
+
* Prevents expired sessions when users switch tabs and come back later.
|
|
70
|
+
*
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
refetchOnWindowFocus?: boolean | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Set to `false` to stop polling when the device has no internet access
|
|
76
|
+
* (determined by `navigator.onLine`).
|
|
77
|
+
*
|
|
78
|
+
* @default false
|
|
79
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
|
|
80
|
+
*/
|
|
81
|
+
refetchWhenOffline?: boolean | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface BetterAuthClientOptions {
|
|
85
|
+
fetchOptions?: ClientFetchOption | undefined;
|
|
86
|
+
plugins?: BetterAuthClientPlugin[] | undefined;
|
|
87
|
+
baseURL?: string | undefined;
|
|
88
|
+
basePath?: string | undefined;
|
|
89
|
+
disableDefaultFetchPlugins?: boolean | undefined;
|
|
90
|
+
$InferAuth?: BetterAuthOptions | undefined;
|
|
91
|
+
sessionOptions?: RevalidateOptions | undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface BetterAuthClientPlugin {
|
|
95
|
+
id: LiteralString;
|
|
96
|
+
version?: string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* only used for type inference. don't pass the
|
|
99
|
+
* actual plugin
|
|
100
|
+
*/
|
|
101
|
+
$InferServerPlugin?: InferableServerPlugin | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Custom actions
|
|
104
|
+
*/
|
|
105
|
+
getActions?: (
|
|
106
|
+
$fetch: BetterFetch,
|
|
107
|
+
$store: ClientStore,
|
|
108
|
+
/**
|
|
109
|
+
* better-auth client options
|
|
110
|
+
*/
|
|
111
|
+
options: BetterAuthClientOptions | undefined,
|
|
112
|
+
) => Record<string, any>;
|
|
113
|
+
/**
|
|
114
|
+
* State atoms that'll be resolved by each framework
|
|
115
|
+
* auth store.
|
|
116
|
+
*/
|
|
117
|
+
getAtoms?: (($fetch: BetterFetch) => Record<string, Atom<any>>) | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* specify path methods for server plugin inferred
|
|
120
|
+
* endpoints to force a specific method.
|
|
121
|
+
*/
|
|
122
|
+
pathMethods?: Record<string, "POST" | "GET"> | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Better fetch plugins
|
|
125
|
+
*/
|
|
126
|
+
fetchPlugins?: BetterFetchPlugin[] | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* a list of recaller based on a matcher function.
|
|
129
|
+
* The signal name needs to match a signal in this
|
|
130
|
+
* plugin or any plugin the user might have added.
|
|
131
|
+
*/
|
|
132
|
+
atomListeners?: ClientAtomListener[] | undefined;
|
|
133
|
+
/**
|
|
134
|
+
* The error codes returned by the plugin
|
|
135
|
+
*/
|
|
136
|
+
$ERROR_CODES?: Record<
|
|
137
|
+
string,
|
|
138
|
+
{
|
|
139
|
+
code: string;
|
|
140
|
+
message: string;
|
|
141
|
+
}
|
|
142
|
+
>;
|
|
143
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Endpoint,
|
|
3
|
+
EndpointContext,
|
|
4
|
+
InputContext,
|
|
5
|
+
Middleware,
|
|
6
|
+
} from "better-call";
|
|
7
|
+
import type { Migration } from "kysely";
|
|
8
|
+
import type { AuthMiddleware } from "../api";
|
|
9
|
+
import type { BetterAuthPluginDBSchema } from "../db";
|
|
10
|
+
import type { RawError } from "../utils/error-codes";
|
|
11
|
+
import type { AuthContext } from "./context";
|
|
12
|
+
import type { Awaitable, LiteralString } from "./helper";
|
|
13
|
+
import type { BetterAuthOptions } from "./init-options";
|
|
14
|
+
|
|
15
|
+
type DeepPartial<T> = T extends Function
|
|
16
|
+
? T
|
|
17
|
+
: T extends object
|
|
18
|
+
? { [K in keyof T]?: DeepPartial<T[K]> }
|
|
19
|
+
: T;
|
|
20
|
+
|
|
21
|
+
export type HookEndpointContext = Partial<
|
|
22
|
+
EndpointContext<string, any> & Omit<InputContext<string, any>, "method">
|
|
23
|
+
> & {
|
|
24
|
+
path?: string;
|
|
25
|
+
context: AuthContext & {
|
|
26
|
+
returned?: unknown | undefined;
|
|
27
|
+
responseHeaders?: Headers | undefined;
|
|
28
|
+
};
|
|
29
|
+
headers?: Headers | undefined;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type BetterAuthPluginErrorCodePart = {
|
|
33
|
+
/**
|
|
34
|
+
* The error codes returned by the plugin
|
|
35
|
+
*/
|
|
36
|
+
$ERROR_CODES?: Record<string, RawError>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type BetterAuthPlugin = BetterAuthPluginErrorCodePart & {
|
|
40
|
+
id: LiteralString;
|
|
41
|
+
version?: string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The init function is called when the plugin is initialized.
|
|
44
|
+
* You can return a new context or modify the existing context.
|
|
45
|
+
*/
|
|
46
|
+
init?:
|
|
47
|
+
| ((ctx: AuthContext) =>
|
|
48
|
+
| Awaitable<{
|
|
49
|
+
context?: DeepPartial<Omit<AuthContext, "options">> &
|
|
50
|
+
Record<string, unknown>;
|
|
51
|
+
options?: Partial<BetterAuthOptions>;
|
|
52
|
+
}>
|
|
53
|
+
| void
|
|
54
|
+
| Promise<void>)
|
|
55
|
+
| undefined;
|
|
56
|
+
endpoints?:
|
|
57
|
+
| {
|
|
58
|
+
[key: string]: Endpoint;
|
|
59
|
+
}
|
|
60
|
+
| undefined;
|
|
61
|
+
middlewares?:
|
|
62
|
+
| {
|
|
63
|
+
path: string;
|
|
64
|
+
middleware: Middleware;
|
|
65
|
+
}[]
|
|
66
|
+
| undefined;
|
|
67
|
+
onRequest?:
|
|
68
|
+
| ((
|
|
69
|
+
request: Request,
|
|
70
|
+
ctx: AuthContext,
|
|
71
|
+
) => Promise<
|
|
72
|
+
| {
|
|
73
|
+
response: Response;
|
|
74
|
+
}
|
|
75
|
+
| {
|
|
76
|
+
request: Request;
|
|
77
|
+
}
|
|
78
|
+
| void
|
|
79
|
+
>)
|
|
80
|
+
| undefined;
|
|
81
|
+
onResponse?:
|
|
82
|
+
| ((
|
|
83
|
+
response: Response,
|
|
84
|
+
ctx: AuthContext,
|
|
85
|
+
) => Promise<{
|
|
86
|
+
response: Response;
|
|
87
|
+
} | void>)
|
|
88
|
+
| undefined;
|
|
89
|
+
hooks?:
|
|
90
|
+
| {
|
|
91
|
+
before?: {
|
|
92
|
+
matcher: (context: HookEndpointContext) => boolean;
|
|
93
|
+
handler: AuthMiddleware;
|
|
94
|
+
}[];
|
|
95
|
+
after?: {
|
|
96
|
+
matcher: (context: HookEndpointContext) => boolean;
|
|
97
|
+
handler: AuthMiddleware;
|
|
98
|
+
}[];
|
|
99
|
+
}
|
|
100
|
+
| undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Schema the plugin needs
|
|
103
|
+
*
|
|
104
|
+
* This will also be used to migrate the database. If the fields are dynamic from the plugins
|
|
105
|
+
* configuration each time the configuration is changed a new migration will be created.
|
|
106
|
+
*
|
|
107
|
+
* NOTE: If you want to create migrations manually using
|
|
108
|
+
* migrations option or any other way you
|
|
109
|
+
* can disable migration per table basis.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* schema: {
|
|
114
|
+
* user: {
|
|
115
|
+
* fields: {
|
|
116
|
+
* email: {
|
|
117
|
+
* type: "string",
|
|
118
|
+
* },
|
|
119
|
+
* emailVerified: {
|
|
120
|
+
* type: "boolean",
|
|
121
|
+
* defaultValue: false,
|
|
122
|
+
* },
|
|
123
|
+
* },
|
|
124
|
+
* }
|
|
125
|
+
* } as AuthPluginSchema
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
schema?: BetterAuthPluginDBSchema | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* The migrations of the plugin. If you define schema that will automatically create
|
|
131
|
+
* migrations for you.
|
|
132
|
+
*
|
|
133
|
+
* ⚠️ Only uses this if you dont't want to use the schema option and you disabled migrations for
|
|
134
|
+
* the tables.
|
|
135
|
+
*/
|
|
136
|
+
migrations?: Record<string, Migration> | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* The options of the plugin
|
|
139
|
+
*/
|
|
140
|
+
options?: Record<string, any> | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* types to be inferred
|
|
143
|
+
*/
|
|
144
|
+
$Infer?: Record<string, any> | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* The rate limit rules to apply to specific paths.
|
|
147
|
+
*/
|
|
148
|
+
rateLimit?:
|
|
149
|
+
| {
|
|
150
|
+
window: number;
|
|
151
|
+
max: number;
|
|
152
|
+
pathMatcher: (path: string) => boolean;
|
|
153
|
+
}[]
|
|
154
|
+
| undefined;
|
|
155
|
+
/**
|
|
156
|
+
* All database operations that are performed by the plugin
|
|
157
|
+
*
|
|
158
|
+
* This will override the default database operations
|
|
159
|
+
*/
|
|
160
|
+
adapter?: {
|
|
161
|
+
[key: string]: (...args: any[]) => Awaitable<any>;
|
|
162
|
+
};
|
|
163
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface SecretConfig {
|
|
2
|
+
/** Map of version number → secret value */
|
|
3
|
+
keys: Map<number, string>;
|
|
4
|
+
/** Version to use for new encryption (first entry in secrets array) */
|
|
5
|
+
currentVersion: number;
|
|
6
|
+
/** Legacy secret for bare-hex fallback (from BETTER_AUTH_SECRET) */
|
|
7
|
+
legacySecret?: string;
|
|
8
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Awaitable } from "../types/helper";
|
|
2
|
+
|
|
3
|
+
export interface MapConcurrentOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Max in-flight mappers. Non-integer values are floored, then clamped
|
|
6
|
+
* to the range `[1, items.length]`. `NaN` falls back to 1.
|
|
7
|
+
*/
|
|
8
|
+
concurrency: number;
|
|
9
|
+
/**
|
|
10
|
+
* Rejects with `signal.reason` when aborted. In-flight mappers keep
|
|
11
|
+
* running but their results are not returned.
|
|
12
|
+
*/
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Run an async mapper over items with bounded concurrency.
|
|
18
|
+
* Preserves input order in the result. Fails fast on the first rejection.
|
|
19
|
+
*/
|
|
20
|
+
export async function mapConcurrent<T, R>(
|
|
21
|
+
items: readonly T[],
|
|
22
|
+
fn: (item: T, index: number) => Awaitable<R>,
|
|
23
|
+
options: MapConcurrentOptions,
|
|
24
|
+
): Promise<R[]> {
|
|
25
|
+
const n = items.length;
|
|
26
|
+
if (n === 0) return [];
|
|
27
|
+
|
|
28
|
+
const { signal } = options;
|
|
29
|
+
if (signal?.aborted) throw signal.reason;
|
|
30
|
+
|
|
31
|
+
const raw = Math.floor(options.concurrency);
|
|
32
|
+
const width = Math.min(n, raw >= 1 ? raw : 1);
|
|
33
|
+
|
|
34
|
+
const results = new Array<R>(n);
|
|
35
|
+
let idx = 0;
|
|
36
|
+
let failed = false;
|
|
37
|
+
|
|
38
|
+
const worker = async (): Promise<void> => {
|
|
39
|
+
while (!failed && idx < n) {
|
|
40
|
+
if (signal?.aborted) throw signal.reason;
|
|
41
|
+
const i = idx++;
|
|
42
|
+
try {
|
|
43
|
+
results[i] = await fn(items[i] as T, i);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
failed = true;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
await Promise.all(Array.from({ length: width }, worker));
|
|
52
|
+
return results;
|
|
53
|
+
}
|
package/src/utils/db.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DBFieldAttribute } from "../db";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Filters output data by removing fields with the `returned: false` attribute.
|
|
5
|
+
* This ensures sensitive fields are not exposed in API responses.
|
|
6
|
+
*/
|
|
7
|
+
export function filterOutputFields<T extends Record<string, unknown> | null>(
|
|
8
|
+
data: T,
|
|
9
|
+
additionalFields: Record<string, DBFieldAttribute> | undefined,
|
|
10
|
+
): T {
|
|
11
|
+
if (!data || !additionalFields) {
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
const returnFiltered = Object.entries(additionalFields)
|
|
15
|
+
.filter(([, { returned }]) => returned === false)
|
|
16
|
+
.map(([key]) => key);
|
|
17
|
+
return Object.entries(structuredClone(data))
|
|
18
|
+
.filter(([key]) => !returnFiltered.includes(key))
|
|
19
|
+
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {} as T);
|
|
20
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { InternalLogger } from "../env";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a function to log a deprecation warning at once.
|
|
5
|
+
*/
|
|
6
|
+
export function deprecate<T extends (...args: any[]) => any>(
|
|
7
|
+
fn: T,
|
|
8
|
+
message: string,
|
|
9
|
+
logger?: InternalLogger,
|
|
10
|
+
): T {
|
|
11
|
+
let warned = false;
|
|
12
|
+
|
|
13
|
+
return function (this: any, ...args: Parameters<T>): ReturnType<T> {
|
|
14
|
+
if (!warned) {
|
|
15
|
+
const warn = logger?.warn ?? console.warn;
|
|
16
|
+
warn(`[Deprecation] ${message}`);
|
|
17
|
+
warned = true;
|
|
18
|
+
}
|
|
19
|
+
return fn.apply(this, args);
|
|
20
|
+
} as T;
|
|
21
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
type UpperLetter =
|
|
2
|
+
| "A"
|
|
3
|
+
| "B"
|
|
4
|
+
| "C"
|
|
5
|
+
| "D"
|
|
6
|
+
| "E"
|
|
7
|
+
| "F"
|
|
8
|
+
| "G"
|
|
9
|
+
| "H"
|
|
10
|
+
| "I"
|
|
11
|
+
| "J"
|
|
12
|
+
| "K"
|
|
13
|
+
| "L"
|
|
14
|
+
| "M"
|
|
15
|
+
| "N"
|
|
16
|
+
| "O"
|
|
17
|
+
| "P"
|
|
18
|
+
| "Q"
|
|
19
|
+
| "R"
|
|
20
|
+
| "S"
|
|
21
|
+
| "T"
|
|
22
|
+
| "U"
|
|
23
|
+
| "V"
|
|
24
|
+
| "W"
|
|
25
|
+
| "X"
|
|
26
|
+
| "Y"
|
|
27
|
+
| "Z";
|
|
28
|
+
type SpecialCharacter = "_";
|
|
29
|
+
|
|
30
|
+
type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}`
|
|
31
|
+
? F extends UpperLetter | SpecialCharacter
|
|
32
|
+
? IsValidUpperSnakeCase<R>
|
|
33
|
+
: false
|
|
34
|
+
: true;
|
|
35
|
+
|
|
36
|
+
type InvalidKeyError<K extends string> =
|
|
37
|
+
`Invalid error code key: "${K}" - must only contain uppercase letters (A-Z) and underscores (_)`;
|
|
38
|
+
|
|
39
|
+
type ValidateErrorCodes<T> = {
|
|
40
|
+
[K in keyof T]: K extends string
|
|
41
|
+
? IsValidUpperSnakeCase<K> extends false
|
|
42
|
+
? InvalidKeyError<K>
|
|
43
|
+
: T[K]
|
|
44
|
+
: T[K];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type RawError<K extends string = string> = {
|
|
48
|
+
readonly code: K;
|
|
49
|
+
message: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function defineErrorCodes<
|
|
53
|
+
const T extends Record<string, string>,
|
|
54
|
+
R extends {
|
|
55
|
+
[K in keyof T & string]: RawError<K>;
|
|
56
|
+
},
|
|
57
|
+
>(codes: ValidateErrorCodes<T>): R {
|
|
58
|
+
return Object.fromEntries(
|
|
59
|
+
Object.entries(codes).map(([key, value]) => [
|
|
60
|
+
key,
|
|
61
|
+
{
|
|
62
|
+
code: key,
|
|
63
|
+
message: value,
|
|
64
|
+
toString: () => key,
|
|
65
|
+
},
|
|
66
|
+
]),
|
|
67
|
+
) as any;
|
|
68
|
+
}
|