@better-auth/electron 1.5.0-beta.13 → 1.5.0-beta.16

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.
@@ -0,0 +1,311 @@
1
+ import * as z from "zod";
2
+ import { BetterFetch, BetterFetchError, CreateFetchOption } from "@better-fetch/fetch";
3
+ import electron from "electron";
4
+ import { Awaitable, BetterAuthClientOptions } from "@better-auth/core";
5
+ import { User } from "@better-auth/core/db";
6
+
7
+ //#region src/types/options.d.ts
8
+ interface ElectronSharedOptions {
9
+ /**
10
+ * Client ID to use for identifying the Electron client during authorization.
11
+ *
12
+ * @default "electron"
13
+ */
14
+ clientID?: string | undefined;
15
+ }
16
+ interface ElectronOptions extends ElectronSharedOptions {
17
+ /**
18
+ * The duration (in seconds) for which the authorization code remains valid.
19
+ *
20
+ * @default 300 (5 minutes)
21
+ */
22
+ codeExpiresIn?: number | undefined;
23
+ /**
24
+ * The duration (in seconds) for which the redirect cookie remains valid.
25
+ *
26
+ * @default 120 (2 minutes)
27
+ */
28
+ redirectCookieExpiresIn?: number | undefined;
29
+ /**
30
+ * The prefix to use for cookies set by the plugin.
31
+ *
32
+ * @default "better-auth"
33
+ */
34
+ cookiePrefix?: string | undefined;
35
+ /**
36
+ * Override the origin for Electron API routes.
37
+ * Enable this if you're facing cors origin issues with Electron API routes.
38
+ *
39
+ * @default false
40
+ */
41
+ disableOriginOverride?: boolean | undefined;
42
+ }
43
+ //#endregion
44
+ //#region src/preload.d.ts
45
+ type ExposedBridges<O extends ElectronClientOptions> = ReturnType<typeof exposeBridges<O>>["$InferBridges"];
46
+ /**
47
+ * Exposes IPC bridges to the renderer process.
48
+ */
49
+ declare function exposeBridges<O extends ElectronClientOptions>(opts: SetupRendererConfig<O>): {
50
+ $InferBridges: {
51
+ getUser: () => Promise<(O["sanitizeUser"] extends ((user: User & Record<string, any>) => Awaitable<User & Record<string, any>>) ? Awaited<ReturnType<O["sanitizeUser"]>> : {
52
+ id: string;
53
+ createdAt: Date;
54
+ updatedAt: Date;
55
+ email: string;
56
+ emailVerified: boolean;
57
+ name: string;
58
+ image?: string | null | undefined;
59
+ } & Record<string, any>) | null>;
60
+ requestAuth: (options?: ElectronRequestAuthOptions) => Promise<void>;
61
+ signOut: () => Promise<void>;
62
+ authenticate: (data: {
63
+ token: string;
64
+ }) => Promise<void>;
65
+ onAuthenticated: (callback: (user: O["sanitizeUser"] extends ((user: User & Record<string, any>) => Awaitable<User & Record<string, any>>) ? Awaited<ReturnType<O["sanitizeUser"]>> : {
66
+ id: string;
67
+ createdAt: Date;
68
+ updatedAt: Date;
69
+ email: string;
70
+ emailVerified: boolean;
71
+ name: string;
72
+ image?: string | null | undefined;
73
+ } & Record<string, any>) => unknown) => () => void;
74
+ onUserUpdated: (callback: (user: (O["sanitizeUser"] extends ((user: User & Record<string, any>) => Awaitable<User & Record<string, any>>) ? Awaited<ReturnType<O["sanitizeUser"]>> : {
75
+ id: string;
76
+ createdAt: Date;
77
+ updatedAt: Date;
78
+ email: string;
79
+ emailVerified: boolean;
80
+ name: string;
81
+ image?: string | null | undefined;
82
+ } & Record<string, any>) | null) => unknown) => () => void;
83
+ onAuthError: (callback: (context: BetterFetchError & {
84
+ path: string;
85
+ }) => unknown) => () => void;
86
+ };
87
+ };
88
+ interface SetupRendererConfig<O extends ElectronClientOptions = ElectronClientOptions> {
89
+ channelPrefix?: O["channelPrefix"] | undefined;
90
+ }
91
+ /**
92
+ * Sets up the renderer process.
93
+ *
94
+ * - Exposes IPC bridges to the renderer process.
95
+ */
96
+ declare function setupRenderer(options?: SetupRendererConfig): void;
97
+ //#endregion
98
+ //#region src/user.d.ts
99
+ type FetchUserImageResult = {
100
+ stream: ReadableStream<Uint8Array>;
101
+ mimeType: string;
102
+ };
103
+ declare function fetchUserImage(baseURL: string | undefined, url: string, options?: Pick<ElectronClientOptions, "userImageProxy"> | undefined): Promise<FetchUserImageResult | null>;
104
+ declare function normalizeUserOutput<U extends User & Record<string, any>>(user: U, options?: ElectronClientOptions | undefined): U;
105
+ //#endregion
106
+ //#region src/types/client.d.ts
107
+ interface Storage {
108
+ getItem: (name: string) => unknown | null;
109
+ setItem: (name: string, value: unknown) => void;
110
+ }
111
+ interface ElectronSharedClientOptions extends ElectronSharedOptions {
112
+ /**
113
+ * The protocol scheme to use for deep linking in Electron.
114
+ *
115
+ * Should follow the reverse domain name notation to ensure uniqueness.
116
+ *
117
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc8252#section-7.1}
118
+ * @example "com.example.app"
119
+ */
120
+ protocol: string | {
121
+ scheme: string;
122
+ };
123
+ /**
124
+ * The callback path to use for authentication redirects.
125
+ *
126
+ * @default "/auth/callback"
127
+ */
128
+ callbackPath?: string;
129
+ }
130
+ interface ElectronClientOptions extends ElectronSharedClientOptions {
131
+ /**
132
+ * The URL to redirect to for authentication.
133
+ *
134
+ * @example "http://localhost:3000/sign-in"
135
+ */
136
+ signInURL: string | URL;
137
+ protocol: string | {
138
+ scheme: string;
139
+ privileges?: Electron.Privileges | undefined;
140
+ };
141
+ /**
142
+ * An instance of a storage solution (e.g., `conf`)
143
+ * to store session and cookie data.
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * import { storage } from "@better-auth/electron/storage";
148
+ * electronClient({
149
+ * storage: storage(),
150
+ * });
151
+ * ```
152
+ */
153
+ storage: Storage;
154
+ /**
155
+ * Prefix for local storage keys (e.g., "my-app_cookie", "my-app_session_data")
156
+ * @default "better-auth"
157
+ */
158
+ storagePrefix?: string | undefined;
159
+ /**
160
+ * A function to sanitize the user object before it is sent to the renderer process.
161
+ *
162
+ * @default undefined
163
+ */
164
+ sanitizeUser?: ((user: User & Record<string, any>) => Awaitable<User & Record<string, any>>) | undefined;
165
+ /**
166
+ * User image proxy configuration.
167
+ */
168
+ userImageProxy?: {
169
+ /**
170
+ * Whether to enable user image proxy.
171
+ *
172
+ * @default true
173
+ */
174
+ enabled?: boolean | undefined;
175
+ /**
176
+ * The protocol scheme to use for user image proxy.
177
+ *
178
+ * @default "user-image"
179
+ */
180
+ scheme?: string | undefined;
181
+ /**
182
+ * The maximum size of user images in bytes.
183
+ *
184
+ * @default 1024 * 1024 * 5 (5MB)
185
+ */
186
+ maxSize?: number | undefined;
187
+ /**
188
+ * Allowed image mime types that should be passed to Accept header.
189
+ *
190
+ * @default "image/*"
191
+ */
192
+ accept?: string | undefined;
193
+ /**
194
+ * Custom user image validator.
195
+ *
196
+ * Note: This will override the default validator, and can have security implications if not handled properly.
197
+ * Only use this if you know what you are doing.
198
+ *
199
+ * @param bytes The user image bytes.
200
+ * @returns The image mime type or null if the image is not valid.
201
+ */
202
+ customValidator?: (bytes: Uint8Array) => Awaitable<string | null>;
203
+ } | undefined;
204
+ /**
205
+ * Prefix(es) for server cookie names to filter (e.g., "better-auth.session_token")
206
+ * This is used to identify which cookies belong to better-auth to prevent
207
+ * infinite refetching when third-party cookies are set.
208
+ *
209
+ * Can be a single string or an array of strings to match multiple prefixes.
210
+ *
211
+ * @default "better-auth"
212
+ * @example "better-auth"
213
+ * @example ["better-auth", "my-app"]
214
+ */
215
+ cookiePrefix?: string | string[] | undefined;
216
+ /**
217
+ * Channel prefix for IPC bridges (e.g., "better-auth:request-auth")
218
+ *
219
+ * @default "better-auth"
220
+ */
221
+ channelPrefix?: string | undefined;
222
+ /**
223
+ * Whether to disable caching the session data locally.
224
+ *
225
+ * @default false
226
+ */
227
+ disableCache?: boolean | undefined;
228
+ }
229
+ interface ElectronProxyClientOptions extends ElectronSharedClientOptions {
230
+ /**
231
+ * The prefix to use for cookies set by the plugin.
232
+ *
233
+ * @default "better-auth"
234
+ */
235
+ cookiePrefix?: string | undefined;
236
+ }
237
+ //#endregion
238
+ //#region src/authenticate.d.ts
239
+ declare const kElectron: unique symbol;
240
+ declare const requestAuthOptionsSchema: z.ZodObject<{
241
+ provider: z.ZodOptional<z.ZodString>;
242
+ callbackURL: z.ZodOptional<z.ZodString>;
243
+ newUserCallbackURL: z.ZodOptional<z.ZodString>;
244
+ errorCallbackURL: z.ZodOptional<z.ZodString>;
245
+ disableRedirect: z.ZodOptional<z.ZodBoolean>;
246
+ scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
247
+ requestSignUp: z.ZodOptional<z.ZodBoolean>;
248
+ additionalData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
249
+ }, z.core.$strip>;
250
+ type ElectronRequestAuthOptions = z.infer<typeof requestAuthOptionsSchema>;
251
+ /**
252
+ * Opens the system browser to request user authentication.
253
+ */
254
+ declare function requestAuth(clientOptions: BetterAuthClientOptions | undefined, options: ElectronClientOptions, cfg?: ElectronRequestAuthOptions | undefined): Promise<void>;
255
+ interface ElectronAuthenticateOptions {
256
+ fetchOptions?: Omit<CreateFetchOption, "method"> | undefined;
257
+ token: string;
258
+ }
259
+ /**
260
+ * Exchanges the authorization code for a session.
261
+ */
262
+ declare function authenticate({
263
+ $fetch,
264
+ options,
265
+ token,
266
+ getWindow,
267
+ fetchOptions
268
+ }: ElectronAuthenticateOptions & {
269
+ $fetch: BetterFetch;
270
+ options: ElectronClientOptions;
271
+ getWindow: () => Electron.BrowserWindow | null | undefined;
272
+ }): Promise<{
273
+ data: null;
274
+ error: {
275
+ message?: string | undefined;
276
+ status: number;
277
+ statusText: string;
278
+ };
279
+ } | {
280
+ data: {
281
+ token: string;
282
+ user: User & Record<string, any>;
283
+ };
284
+ error: null;
285
+ }>;
286
+ //#endregion
287
+ //#region src/browser.d.ts
288
+ type SetupMainConfig = {
289
+ getWindow?: () => electron.BrowserWindow | null | undefined;
290
+ csp?: boolean | undefined;
291
+ bridges?: boolean | undefined;
292
+ scheme?: boolean | undefined;
293
+ };
294
+ /**
295
+ * Handles the deep link URL for authentication.
296
+ */
297
+ declare function handleDeepLink({
298
+ $fetch,
299
+ options,
300
+ url,
301
+ getWindow,
302
+ clientOptions
303
+ }: {
304
+ $fetch: BetterFetch;
305
+ options: ElectronClientOptions;
306
+ url: string;
307
+ getWindow?: SetupMainConfig["getWindow"] | undefined;
308
+ clientOptions?: BetterAuthClientOptions | undefined;
309
+ }): Promise<void>;
310
+ //#endregion
311
+ export { ElectronOptions as _, kElectron as a, ElectronProxyClientOptions as c, FetchUserImageResult as d, fetchUserImage as f, setupRenderer as g, SetupRendererConfig as h, authenticate as i, ElectronSharedClientOptions as l, ExposedBridges as m, ElectronAuthenticateOptions as n, requestAuth as o, normalizeUserOutput as p, ElectronRequestAuthOptions as r, ElectronClientOptions as s, handleDeepLink as t, Storage as u, ElectronSharedOptions as v };
package/dist/client.d.mts CHANGED
@@ -1,3 +1,194 @@
1
- import { i as Storage, n as ElectronClientOptions, r as ElectronProxyClientOptions, t as ElectronRequestAuthOptions } from "./authenticate-CWAVJ4W8.mjs";
2
- import { n as handleDeepLink, t as electronClient } from "./client-BBp9yCmE.mjs";
3
- export { ElectronClientOptions, ElectronProxyClientOptions, ElectronRequestAuthOptions, Storage, electronClient, handleDeepLink };
1
+ import { a as kElectron, c as ElectronProxyClientOptions, d as FetchUserImageResult, f as fetchUserImage, g as setupRenderer, h as SetupRendererConfig, i as authenticate, l as ElectronSharedClientOptions, m as ExposedBridges, n as ElectronAuthenticateOptions, o as requestAuth, p as normalizeUserOutput, r as ElectronRequestAuthOptions, s as ElectronClientOptions, t as handleDeepLink, u as Storage, v as ElectronSharedOptions } from "./browser-CCZQ2aoV.mjs";
2
+ import * as better_auth0 from "better-auth";
3
+ import { ClientStore } from "better-auth";
4
+ import * as _better_fetch_fetch0 from "@better-fetch/fetch";
5
+ import electron from "electron";
6
+
7
+ //#region src/client.d.ts
8
+ declare const electronClient: <O extends ElectronClientOptions>(options: O) => {
9
+ id: "electron";
10
+ fetchPlugins: {
11
+ id: string;
12
+ name: string;
13
+ init(url: string, options: ({
14
+ cache?: RequestCache | undefined;
15
+ credentials?: RequestCredentials | undefined;
16
+ headers?: (HeadersInit & (HeadersInit | {
17
+ accept: "application/json" | "text/plain" | "application/octet-stream";
18
+ "content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
19
+ authorization: "Bearer" | "Basic";
20
+ })) | undefined;
21
+ integrity?: string | undefined;
22
+ keepalive?: boolean | undefined;
23
+ method?: string | undefined;
24
+ mode?: RequestMode | undefined;
25
+ priority?: RequestPriority | undefined;
26
+ redirect?: RequestRedirect | undefined;
27
+ referrer?: string | undefined;
28
+ referrerPolicy?: ReferrerPolicy | undefined;
29
+ signal?: (AbortSignal | null) | undefined;
30
+ window?: null | undefined;
31
+ onRequest?: (<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>) => Promise<_better_fetch_fetch0.RequestContext | void> | _better_fetch_fetch0.RequestContext | void) | undefined;
32
+ onResponse?: ((context: _better_fetch_fetch0.ResponseContext) => Promise<Response | void | _better_fetch_fetch0.ResponseContext> | Response | _better_fetch_fetch0.ResponseContext | void) | undefined;
33
+ onSuccess?: ((context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void> | void) | undefined;
34
+ onError?: ((context: _better_fetch_fetch0.ErrorContext) => Promise<void> | void) | undefined;
35
+ onRetry?: ((response: _better_fetch_fetch0.ResponseContext) => Promise<void> | void) | undefined;
36
+ hookOptions?: {
37
+ cloneResponse?: boolean;
38
+ } | undefined;
39
+ timeout?: number | undefined;
40
+ customFetchImpl?: _better_fetch_fetch0.FetchEsque | undefined;
41
+ plugins?: _better_fetch_fetch0.BetterFetchPlugin[] | undefined;
42
+ baseURL?: string | undefined;
43
+ throw?: boolean | undefined;
44
+ auth?: ({
45
+ type: "Bearer";
46
+ token: string | Promise<string | undefined> | (() => string | Promise<string | undefined> | undefined) | undefined;
47
+ } | {
48
+ type: "Basic";
49
+ username: string | (() => string | undefined) | undefined;
50
+ password: string | (() => string | undefined) | undefined;
51
+ } | {
52
+ type: "Custom";
53
+ prefix: string | (() => string | undefined) | undefined;
54
+ value: string | (() => string | undefined) | undefined;
55
+ }) | undefined;
56
+ body?: any;
57
+ query?: any;
58
+ params?: any;
59
+ duplex?: "full" | "half" | undefined;
60
+ jsonParser?: ((text: string) => Promise<any> | any) | undefined;
61
+ retry?: _better_fetch_fetch0.RetryOptions | undefined;
62
+ retryAttempt?: number | undefined;
63
+ output?: (_better_fetch_fetch0.StandardSchemaV1 | typeof Blob | typeof File) | undefined;
64
+ errorSchema?: _better_fetch_fetch0.StandardSchemaV1 | undefined;
65
+ disableValidation?: boolean | undefined;
66
+ } & Record<string, any>) | undefined): Promise<{
67
+ url: string;
68
+ options: {
69
+ cache?: RequestCache | undefined;
70
+ credentials?: RequestCredentials | undefined;
71
+ headers?: (HeadersInit & (HeadersInit | {
72
+ accept: "application/json" | "text/plain" | "application/octet-stream";
73
+ "content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
74
+ authorization: "Bearer" | "Basic";
75
+ })) | undefined;
76
+ integrity?: string | undefined;
77
+ keepalive?: boolean | undefined;
78
+ method?: string | undefined;
79
+ mode?: RequestMode | undefined;
80
+ priority?: RequestPriority | undefined;
81
+ redirect?: RequestRedirect | undefined;
82
+ referrer?: string | undefined;
83
+ referrerPolicy?: ReferrerPolicy | undefined;
84
+ signal?: (AbortSignal | null) | undefined;
85
+ window?: null | undefined;
86
+ onRequest?: (<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>) => Promise<_better_fetch_fetch0.RequestContext | void> | _better_fetch_fetch0.RequestContext | void) | undefined;
87
+ onResponse?: ((context: _better_fetch_fetch0.ResponseContext) => Promise<Response | void | _better_fetch_fetch0.ResponseContext> | Response | _better_fetch_fetch0.ResponseContext | void) | undefined;
88
+ onSuccess?: ((context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void> | void) | undefined;
89
+ onError?: ((context: _better_fetch_fetch0.ErrorContext) => Promise<void> | void) | undefined;
90
+ onRetry?: ((response: _better_fetch_fetch0.ResponseContext) => Promise<void> | void) | undefined;
91
+ hookOptions?: {
92
+ cloneResponse?: boolean;
93
+ } | undefined;
94
+ timeout?: number | undefined;
95
+ customFetchImpl?: _better_fetch_fetch0.FetchEsque | undefined;
96
+ plugins?: _better_fetch_fetch0.BetterFetchPlugin[] | undefined;
97
+ baseURL?: string | undefined;
98
+ throw?: boolean | undefined;
99
+ auth?: ({
100
+ type: "Bearer";
101
+ token: string | Promise<string | undefined> | (() => string | Promise<string | undefined> | undefined) | undefined;
102
+ } | {
103
+ type: "Basic";
104
+ username: string | (() => string | undefined) | undefined;
105
+ password: string | (() => string | undefined) | undefined;
106
+ } | {
107
+ type: "Custom";
108
+ prefix: string | (() => string | undefined) | undefined;
109
+ value: string | (() => string | undefined) | undefined;
110
+ }) | undefined;
111
+ body?: any;
112
+ query?: any;
113
+ params?: any;
114
+ duplex?: "full" | "half" | undefined;
115
+ jsonParser?: ((text: string) => Promise<any> | any) | undefined;
116
+ retry?: _better_fetch_fetch0.RetryOptions | undefined;
117
+ retryAttempt?: number | undefined;
118
+ output?: (_better_fetch_fetch0.StandardSchemaV1 | typeof Blob | typeof File) | undefined;
119
+ errorSchema?: _better_fetch_fetch0.StandardSchemaV1 | undefined;
120
+ disableValidation?: boolean | undefined;
121
+ } & Record<string, any>;
122
+ }>;
123
+ hooks: {
124
+ onSuccess: (context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void>;
125
+ onError: (context: _better_fetch_fetch0.ErrorContext) => Promise<void>;
126
+ };
127
+ }[];
128
+ getActions: ($fetch: _better_fetch_fetch0.BetterFetch, $store: ClientStore, clientOptions: better_auth0.BetterAuthClientOptions | undefined) => {
129
+ /**
130
+ * Gets the stored cookie.
131
+ *
132
+ * You can use this to get the cookie stored in
133
+ * the device and use it in your fetch requests.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * const cookie = client.getCookie();
138
+ * await fetch("https://api.example.com", {
139
+ * headers: {
140
+ * cookie,
141
+ * },
142
+ * });
143
+ * ```
144
+ */
145
+ getCookie: () => string;
146
+ /**
147
+ * Exchanges the authorization code for a session.
148
+ *
149
+ * Use this when you need to manually complete the exchange
150
+ * (e.g., when another app registered the scheme or deep linking fails).
151
+ *
152
+ * The authorization code is returned when the user is authorized in the browser. (`electron_authorization_code`)
153
+ *
154
+ * Note: Must be called after `requestAuth`, since the code verifier and state are stored when the auth flow is initiated.
155
+ */
156
+ authenticate: (data: ElectronAuthenticateOptions) => Promise<{
157
+ data: null;
158
+ error: {
159
+ message?: string | undefined;
160
+ status: number;
161
+ statusText: string;
162
+ };
163
+ } | {
164
+ data: {
165
+ token: string;
166
+ user: better_auth0.User & Record<string, any>;
167
+ };
168
+ error: null;
169
+ }>;
170
+ /**
171
+ * Initiates the authentication process.
172
+ * Opens the system's default browser for user authentication.
173
+ */
174
+ requestAuth: (options?: ElectronRequestAuthOptions | undefined) => Promise<void>;
175
+ /**
176
+ * Sets up the main process.
177
+ *
178
+ * - Registers custom protocol scheme.
179
+ * - Registers IPC bridge handlers.
180
+ * - Handles content security policy if needed.
181
+ */
182
+ setupMain: (cfg?: {
183
+ csp?: boolean | undefined;
184
+ bridges?: boolean | undefined;
185
+ scheme?: boolean | undefined;
186
+ getWindow?: () => electron.BrowserWindow | null | undefined;
187
+ }) => void;
188
+ $Infer: {
189
+ Bridges: ExposedBridges<O>;
190
+ };
191
+ };
192
+ };
193
+ //#endregion
194
+ export { ElectronAuthenticateOptions, ElectronClientOptions, ElectronProxyClientOptions, ElectronRequestAuthOptions, ElectronSharedClientOptions, ElectronSharedOptions, ExposedBridges, FetchUserImageResult, SetupRendererConfig, Storage, authenticate, electronClient, fetchUserImage, handleDeepLink, kElectron, normalizeUserOutput, requestAuth, setupRenderer };