@better-auth/electron 1.5.0-beta.12

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 ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2024 - present, Bereket Engida
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the “Software”), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ the Software, and to permit persons to whom the Software is furnished to do so,
9
+ subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
+ DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # Better Auth Electron Plugin
2
+
3
+ This plugin integrates Better Auth with Electron, allowing you to easily add
4
+ authentication to your Electron applications.
5
+
6
+ - **Main Better Auth Installation:** [Installation Guide][]
7
+ - **Electron Integration Guide:** [Electron Integration Guide][]
8
+
9
+ ## License
10
+
11
+ MIT
12
+
13
+ [electron integration guide]: https://www.better-auth.com/docs/integrations/electron
14
+ [installation guide]: https://www.better-auth.com/docs/installation
@@ -0,0 +1,129 @@
1
+ import * as z from "zod";
2
+ import { BetterFetch } from "@better-fetch/fetch";
3
+
4
+ //#region src/types/client.d.ts
5
+ interface Storage {
6
+ getItem: (name: string) => unknown | null;
7
+ setItem: (name: string, value: unknown) => void;
8
+ }
9
+ interface ElectronClientOptions {
10
+ /**
11
+ * The URL to redirect to for authentication.
12
+ *
13
+ * @example "http://localhost:3000/sign-in"
14
+ */
15
+ signInURL: string | URL;
16
+ /**
17
+ * The protocol scheme to use for deep linking in Electron.
18
+ *
19
+ * Should follow the reverse domain name notation to ensure uniqueness.
20
+ *
21
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc8252#section-7.1}
22
+ * @example "com.example.app"
23
+ */
24
+ protocol: string | {
25
+ scheme: string;
26
+ privileges?: Electron.Privileges | undefined;
27
+ };
28
+ /**
29
+ * The callback path to use for authentication redirects.
30
+ *
31
+ * @default "/auth/callback"
32
+ */
33
+ callbackPath?: string;
34
+ /**
35
+ * An instance of a storage solution (e.g., `electron-store`)
36
+ * to store session and cookie data.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * electronClient({
41
+ * storage: window.localStorage,
42
+ * });
43
+ * ```
44
+ */
45
+ storage: Storage;
46
+ /**
47
+ * Prefix for local storage keys (e.g., "my-app_cookie", "my-app_session_data")
48
+ * @default "better-auth"
49
+ */
50
+ storagePrefix?: string | undefined;
51
+ /**
52
+ * Prefix(es) for server cookie names to filter (e.g., "better-auth.session_token")
53
+ * This is used to identify which cookies belong to better-auth to prevent
54
+ * infinite refetching when third-party cookies are set.
55
+ *
56
+ * Can be a single string or an array of strings to match multiple prefixes.
57
+ *
58
+ * @default "better-auth"
59
+ * @example "better-auth"
60
+ * @example ["better-auth", "my-app"]
61
+ */
62
+ cookiePrefix?: string | string[] | undefined;
63
+ /**
64
+ * Channel prefix for IPC bridges (e.g., "better-auth:request-auth")
65
+ *
66
+ * @default "better-auth"
67
+ */
68
+ channelPrefix?: string | undefined;
69
+ /**
70
+ * Client ID to use for identifying the Electron client during authorization.
71
+ *
72
+ * @default "electron"
73
+ */
74
+ clientID?: string | undefined;
75
+ /**
76
+ * Whether to disable caching the session data locally.
77
+ *
78
+ * @default false
79
+ */
80
+ disableCache?: boolean | undefined;
81
+ }
82
+ interface ElectronProxyClientOptions {
83
+ /**
84
+ * The protocol scheme to use for deep linking in Electron.
85
+ *
86
+ * Should follow the reverse domain name notation to ensure uniqueness.
87
+ *
88
+ * Note that this must match the protocol scheme registered in the server plugin.
89
+ *
90
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc8252#section-7.1}
91
+ * @example "com.example.app"
92
+ */
93
+ protocol: string | {
94
+ scheme: string;
95
+ };
96
+ /**
97
+ * The callback path to use for authentication redirects.
98
+ *
99
+ * @default "/auth/callback"
100
+ */
101
+ callbackPath?: string;
102
+ /**
103
+ * Client ID to use for identifying the Electron client during authorization.
104
+ *
105
+ * @default "electron"
106
+ */
107
+ clientID?: string | undefined;
108
+ /**
109
+ * The prefix to use for cookies set by the plugin.
110
+ *
111
+ * @default "better-auth"
112
+ */
113
+ cookiePrefix?: string | undefined;
114
+ }
115
+ //#endregion
116
+ //#region src/authenticate.d.ts
117
+ declare const requestAuthOptionsSchema: z.ZodObject<{
118
+ provider: z.ZodOptional<z.ZodString>;
119
+ callbackURL: z.ZodOptional<z.ZodString>;
120
+ newUserCallbackURL: z.ZodOptional<z.ZodString>;
121
+ errorCallbackURL: z.ZodOptional<z.ZodString>;
122
+ disableRedirect: z.ZodOptional<z.ZodBoolean>;
123
+ scopes: z.ZodOptional<z.ZodArray<z.ZodString>>;
124
+ requestSignUp: z.ZodOptional<z.ZodBoolean>;
125
+ additionalData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
126
+ }, z.core.$strip>;
127
+ type ElectronRequestAuthOptions = z.infer<typeof requestAuthOptionsSchema>;
128
+ //#endregion
129
+ export { Storage as i, ElectronClientOptions as n, ElectronProxyClientOptions as r, ElectronRequestAuthOptions as t };
@@ -0,0 +1,224 @@
1
+ import { n as ElectronClientOptions, t as ElectronRequestAuthOptions } from "./authenticate-CWAVJ4W8.mjs";
2
+ import * as better_auth27 from "better-auth";
3
+ import { ClientStore, User } from "better-auth";
4
+ import * as _better_fetch_fetch0 from "@better-fetch/fetch";
5
+ import { BetterFetch, BetterFetchError } from "@better-fetch/fetch";
6
+ import electron from "electron";
7
+
8
+ //#region src/bridges.d.ts
9
+ /**
10
+ * Exposes IPC bridges to the renderer process.
11
+ */
12
+ declare function exposeBridges(opts: ElectronClientOptions): {
13
+ $InferBridges: {
14
+ getUser: () => Promise<{
15
+ id: string;
16
+ createdAt: Date;
17
+ updatedAt: Date;
18
+ email: string;
19
+ emailVerified: boolean;
20
+ name: string;
21
+ image?: string | null | undefined;
22
+ } & Record<string, any>>;
23
+ requestAuth: (options?: ElectronRequestAuthOptions) => Promise<void>;
24
+ signOut: () => Promise<void>;
25
+ onAuthenticated: (callback: (user: User & Record<string, any>) => unknown) => () => void;
26
+ onUserUpdated: (callback: (user: (User & Record<string, any>) | null) => unknown) => () => void;
27
+ onAuthError: (callback: (context: BetterFetchError & {
28
+ path: string;
29
+ }) => unknown) => () => void;
30
+ };
31
+ };
32
+ //#endregion
33
+ //#region src/setup.d.ts
34
+ type SetupMainConfig = {
35
+ getWindow?: () => electron.BrowserWindow | null | undefined;
36
+ csp?: boolean | undefined;
37
+ bridges?: boolean | undefined;
38
+ scheme?: boolean | undefined;
39
+ };
40
+ /**
41
+ * Handles the deep link URL for authentication.
42
+ */
43
+ declare function handleDeepLink({
44
+ $fetch,
45
+ options,
46
+ url,
47
+ getWindow
48
+ }: {
49
+ $fetch: BetterFetch;
50
+ options: ElectronClientOptions;
51
+ url: string;
52
+ getWindow?: SetupMainConfig["getWindow"] | undefined;
53
+ }): Promise<void>;
54
+ //#endregion
55
+ //#region src/client.d.ts
56
+ declare const electronClient: (options: ElectronClientOptions) => {
57
+ id: "electron";
58
+ fetchPlugins: {
59
+ id: string;
60
+ name: string;
61
+ init(url: string, options: ({
62
+ headers?: (HeadersInit & (HeadersInit | {
63
+ accept: "application/json" | "text/plain" | "application/octet-stream";
64
+ "content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
65
+ authorization: "Bearer" | "Basic";
66
+ })) | undefined;
67
+ method?: string | undefined;
68
+ redirect?: RequestRedirect | undefined;
69
+ cache?: RequestCache | undefined;
70
+ credentials?: RequestCredentials | undefined;
71
+ integrity?: string | undefined;
72
+ keepalive?: boolean | undefined;
73
+ mode?: RequestMode | undefined;
74
+ priority?: RequestPriority | undefined;
75
+ referrer?: string | undefined;
76
+ referrerPolicy?: ReferrerPolicy | undefined;
77
+ signal?: (AbortSignal | null) | undefined;
78
+ window?: null | undefined;
79
+ onRequest?: (<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>) => Promise<_better_fetch_fetch0.RequestContext | void> | _better_fetch_fetch0.RequestContext | void) | undefined;
80
+ onResponse?: ((context: _better_fetch_fetch0.ResponseContext) => Promise<Response | void | _better_fetch_fetch0.ResponseContext> | Response | _better_fetch_fetch0.ResponseContext | void) | undefined;
81
+ onSuccess?: ((context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void> | void) | undefined;
82
+ onError?: ((context: _better_fetch_fetch0.ErrorContext) => Promise<void> | void) | undefined;
83
+ onRetry?: ((response: _better_fetch_fetch0.ResponseContext) => Promise<void> | void) | undefined;
84
+ hookOptions?: {
85
+ cloneResponse?: boolean;
86
+ } | undefined;
87
+ timeout?: number | undefined;
88
+ customFetchImpl?: _better_fetch_fetch0.FetchEsque | undefined;
89
+ plugins?: _better_fetch_fetch0.BetterFetchPlugin[] | undefined;
90
+ baseURL?: string | undefined;
91
+ throw?: boolean | undefined;
92
+ auth?: ({
93
+ type: "Bearer";
94
+ token: string | Promise<string | undefined> | (() => string | Promise<string | undefined> | undefined) | undefined;
95
+ } | {
96
+ type: "Basic";
97
+ username: string | (() => string | undefined) | undefined;
98
+ password: string | (() => string | undefined) | undefined;
99
+ } | {
100
+ type: "Custom";
101
+ prefix: string | (() => string | undefined) | undefined;
102
+ value: string | (() => string | undefined) | undefined;
103
+ }) | undefined;
104
+ body?: any;
105
+ query?: any;
106
+ params?: any;
107
+ duplex?: "full" | "half" | undefined;
108
+ jsonParser?: ((text: string) => Promise<any> | any) | undefined;
109
+ retry?: _better_fetch_fetch0.RetryOptions | undefined;
110
+ retryAttempt?: number | undefined;
111
+ output?: (_better_fetch_fetch0.StandardSchemaV1 | typeof Blob | typeof File) | undefined;
112
+ errorSchema?: _better_fetch_fetch0.StandardSchemaV1 | undefined;
113
+ disableValidation?: boolean | undefined;
114
+ } & Record<string, any>) | undefined): Promise<{
115
+ url: string;
116
+ options: {
117
+ headers?: (HeadersInit & (HeadersInit | {
118
+ accept: "application/json" | "text/plain" | "application/octet-stream";
119
+ "content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
120
+ authorization: "Bearer" | "Basic";
121
+ })) | undefined;
122
+ method?: string | undefined;
123
+ redirect?: RequestRedirect | undefined;
124
+ cache?: RequestCache | undefined;
125
+ credentials?: RequestCredentials | undefined;
126
+ integrity?: string | undefined;
127
+ keepalive?: boolean | undefined;
128
+ mode?: RequestMode | undefined;
129
+ priority?: RequestPriority | undefined;
130
+ referrer?: string | undefined;
131
+ referrerPolicy?: ReferrerPolicy | undefined;
132
+ signal?: (AbortSignal | null) | undefined;
133
+ window?: null | undefined;
134
+ onRequest?: (<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>) => Promise<_better_fetch_fetch0.RequestContext | void> | _better_fetch_fetch0.RequestContext | void) | undefined;
135
+ onResponse?: ((context: _better_fetch_fetch0.ResponseContext) => Promise<Response | void | _better_fetch_fetch0.ResponseContext> | Response | _better_fetch_fetch0.ResponseContext | void) | undefined;
136
+ onSuccess?: ((context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void> | void) | undefined;
137
+ onError?: ((context: _better_fetch_fetch0.ErrorContext) => Promise<void> | void) | undefined;
138
+ onRetry?: ((response: _better_fetch_fetch0.ResponseContext) => Promise<void> | void) | undefined;
139
+ hookOptions?: {
140
+ cloneResponse?: boolean;
141
+ } | undefined;
142
+ timeout?: number | undefined;
143
+ customFetchImpl?: _better_fetch_fetch0.FetchEsque | undefined;
144
+ plugins?: _better_fetch_fetch0.BetterFetchPlugin[] | undefined;
145
+ baseURL?: string | undefined;
146
+ throw?: boolean | undefined;
147
+ auth?: ({
148
+ type: "Bearer";
149
+ token: string | Promise<string | undefined> | (() => string | Promise<string | undefined> | undefined) | undefined;
150
+ } | {
151
+ type: "Basic";
152
+ username: string | (() => string | undefined) | undefined;
153
+ password: string | (() => string | undefined) | undefined;
154
+ } | {
155
+ type: "Custom";
156
+ prefix: string | (() => string | undefined) | undefined;
157
+ value: string | (() => string | undefined) | undefined;
158
+ }) | undefined;
159
+ body?: any;
160
+ query?: any;
161
+ params?: any;
162
+ duplex?: "full" | "half" | undefined;
163
+ jsonParser?: ((text: string) => Promise<any> | any) | undefined;
164
+ retry?: _better_fetch_fetch0.RetryOptions | undefined;
165
+ retryAttempt?: number | undefined;
166
+ output?: (_better_fetch_fetch0.StandardSchemaV1 | typeof Blob | typeof File) | undefined;
167
+ errorSchema?: _better_fetch_fetch0.StandardSchemaV1 | undefined;
168
+ disableValidation?: boolean | undefined;
169
+ } & Record<string, any>;
170
+ }>;
171
+ hooks: {
172
+ onSuccess: (context: _better_fetch_fetch0.SuccessContext<any>) => Promise<void>;
173
+ onError: (context: _better_fetch_fetch0.ErrorContext) => Promise<void>;
174
+ };
175
+ }[];
176
+ getActions: ($fetch: _better_fetch_fetch0.BetterFetch, $store: ClientStore, clientOptions: better_auth27.BetterAuthClientOptions | undefined) => {
177
+ /**
178
+ * Gets the stored cookie.
179
+ *
180
+ * You can use this to get the cookie stored in
181
+ * the device and use it in your fetch requests.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const cookie = client.getCookie();
186
+ * await fetch("https://api.example.com", {
187
+ * headers: {
188
+ * cookie,
189
+ * },
190
+ * });
191
+ * ```
192
+ */
193
+ getCookie: () => string;
194
+ /**
195
+ * Initiates the authentication process.
196
+ * Opens the system's default browser for user authentication.
197
+ */
198
+ requestAuth: (options?: ElectronRequestAuthOptions | undefined) => Promise<void>;
199
+ /**
200
+ * Sets up the renderer process.
201
+ *
202
+ * - Exposes IPC bridges to the renderer process.
203
+ */
204
+ setupRenderer: () => void;
205
+ /**
206
+ * Sets up the main process.
207
+ *
208
+ * - Registers custom protocol scheme.
209
+ * - Registers IPC bridge handlers.
210
+ * - Handles content security policy if needed.
211
+ */
212
+ setupMain: (cfg?: {
213
+ csp?: boolean | undefined;
214
+ bridges?: boolean | undefined;
215
+ scheme?: boolean | undefined;
216
+ getWindow?: () => electron.BrowserWindow | null | undefined;
217
+ }) => void;
218
+ $Infer: {
219
+ Bridges: ReturnType<typeof exposeBridges>["$InferBridges"];
220
+ };
221
+ };
222
+ };
223
+ //#endregion
224
+ export { handleDeepLink as n, electronClient as t };
@@ -0,0 +1,3 @@
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 };