abckit 0.0.62 → 0.0.63
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.
|
@@ -46,6 +46,6 @@ export declare function hasBetterAuthCookies(setCookieHeader: string, cookiePref
|
|
|
46
46
|
* Provides offline-first authentication with persistent storage
|
|
47
47
|
*/
|
|
48
48
|
export declare function capacitorClient(opts?: CapacitorClientOptions): BetterAuthClientPlugin;
|
|
49
|
-
export
|
|
50
|
-
export
|
|
49
|
+
export { setupCapacitorFocusManager } from './focus-manager.js';
|
|
50
|
+
export { setupCapacitorOnlineManager } from './online-manager.js';
|
|
51
51
|
export { parseSetCookieHeader } from 'better-auth/cookies';
|
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
import { safeJSONParse } from "@better-auth/core/utils/json";
|
|
2
|
-
import { App } from "@capacitor/app";
|
|
3
|
-
import { Browser } from "@capacitor/browser";
|
|
4
|
-
import { Capacitor } from "@capacitor/core";
|
|
5
|
-
import { Preferences } from "@capacitor/preferences";
|
|
6
2
|
import {
|
|
7
3
|
parseSetCookieHeader,
|
|
8
4
|
SECURE_COOKIE_PREFIX,
|
|
9
5
|
stripSecureCookiePrefix
|
|
10
6
|
} from "better-auth/cookies";
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let _Preferences = null;
|
|
8
|
+
let _Browser = null;
|
|
9
|
+
let _App = null;
|
|
10
|
+
async function getPreferences() {
|
|
11
|
+
if (!_Preferences) {
|
|
12
|
+
const mod = await import("@capacitor/preferences");
|
|
13
|
+
_Preferences = mod.Preferences;
|
|
14
|
+
}
|
|
15
|
+
return _Preferences;
|
|
16
|
+
}
|
|
17
|
+
async function getBrowser() {
|
|
18
|
+
if (!_Browser) {
|
|
19
|
+
const mod = await import("@capacitor/browser");
|
|
20
|
+
_Browser = mod.Browser;
|
|
21
|
+
}
|
|
22
|
+
return _Browser;
|
|
23
|
+
}
|
|
24
|
+
async function getApp() {
|
|
25
|
+
if (!_App) {
|
|
26
|
+
const mod = await import("@capacitor/app");
|
|
27
|
+
_App = mod.App;
|
|
28
|
+
}
|
|
29
|
+
return _App;
|
|
30
|
+
}
|
|
31
|
+
function isNativePlatform() {
|
|
32
|
+
if (typeof window === "undefined")
|
|
33
|
+
return false;
|
|
34
|
+
const win = window;
|
|
35
|
+
return win.Capacitor?.isNativePlatform?.() ?? false;
|
|
36
|
+
}
|
|
13
37
|
let managersInitialized = false;
|
|
14
|
-
function initializeManagers() {
|
|
15
|
-
if (managersInitialized || !
|
|
38
|
+
async function initializeManagers() {
|
|
39
|
+
if (managersInitialized || !isNativePlatform())
|
|
16
40
|
return;
|
|
17
41
|
managersInitialized = true;
|
|
18
|
-
setupCapacitorFocusManager(
|
|
19
|
-
|
|
42
|
+
const [{ setupCapacitorFocusManager: setupCapacitorFocusManager2 }, { setupCapacitorOnlineManager: setupCapacitorOnlineManager2 }] = await Promise.all([
|
|
43
|
+
import("./focus-manager.js"),
|
|
44
|
+
import("./online-manager.js")
|
|
45
|
+
]);
|
|
46
|
+
setupCapacitorFocusManager2();
|
|
47
|
+
setupCapacitorOnlineManager2();
|
|
20
48
|
}
|
|
21
49
|
export function normalizeCookieName(name) {
|
|
22
50
|
return name.replace(/:/g, "_");
|
|
@@ -146,6 +174,7 @@ export function capacitorClient(opts) {
|
|
|
146
174
|
* Get stored cookie string for manual fetch requests
|
|
147
175
|
*/
|
|
148
176
|
getCookie: async () => {
|
|
177
|
+
const Preferences = await getPreferences();
|
|
149
178
|
const result = await Preferences.get({ key: normalizeCookieName(cookieName) });
|
|
150
179
|
return getCookie(result?.value || "{}");
|
|
151
180
|
},
|
|
@@ -153,6 +182,7 @@ export function capacitorClient(opts) {
|
|
|
153
182
|
* Get cached session data for offline use
|
|
154
183
|
*/
|
|
155
184
|
getCachedSession: async () => {
|
|
185
|
+
const Preferences = await getPreferences();
|
|
156
186
|
const result = await Preferences.get({ key: normalizeCookieName(localCacheName) });
|
|
157
187
|
if (!result?.value)
|
|
158
188
|
return null;
|
|
@@ -166,6 +196,7 @@ export function capacitorClient(opts) {
|
|
|
166
196
|
* Clear all stored auth data
|
|
167
197
|
*/
|
|
168
198
|
clearStorage: async () => {
|
|
199
|
+
const Preferences = await getPreferences();
|
|
169
200
|
await Preferences.remove({ key: normalizeCookieName(cookieName) });
|
|
170
201
|
await Preferences.remove({ key: normalizeCookieName(localCacheName) });
|
|
171
202
|
}
|
|
@@ -177,8 +208,9 @@ export function capacitorClient(opts) {
|
|
|
177
208
|
name: "Capacitor Auth",
|
|
178
209
|
hooks: {
|
|
179
210
|
async onSuccess(context) {
|
|
180
|
-
if (!
|
|
211
|
+
if (!isNativePlatform())
|
|
181
212
|
return;
|
|
213
|
+
const Preferences = await getPreferences();
|
|
182
214
|
const normalizedCookieName = normalizeCookieName(cookieName);
|
|
183
215
|
const authToken = context.response.headers.get("set-auth-token");
|
|
184
216
|
if (authToken) {
|
|
@@ -214,6 +246,7 @@ export function capacitorClient(opts) {
|
|
|
214
246
|
});
|
|
215
247
|
}
|
|
216
248
|
if (context.data?.redirect && (context.request.url.toString().includes("/sign-in") || context.request.url.toString().includes("/link-social")) && !context.request?.body?.includes?.("idToken") && scheme) {
|
|
249
|
+
const [Browser, App] = await Promise.all([getBrowser(), getApp()]);
|
|
217
250
|
const callbackURL = JSON.parse(context.request.body)?.callbackURL;
|
|
218
251
|
const signInURL = context.data?.url;
|
|
219
252
|
const storedCookieJson = (await Preferences.get({ key: normalizedCookieName }))?.value;
|
|
@@ -250,10 +283,11 @@ export function capacitorClient(opts) {
|
|
|
250
283
|
}
|
|
251
284
|
},
|
|
252
285
|
async init(url, options) {
|
|
253
|
-
if (!
|
|
286
|
+
if (!isNativePlatform()) {
|
|
254
287
|
return { url, options };
|
|
255
288
|
}
|
|
256
|
-
initializeManagers();
|
|
289
|
+
await initializeManagers();
|
|
290
|
+
const Preferences = await getPreferences();
|
|
257
291
|
const normalizedCookieName = normalizeCookieName(cookieName);
|
|
258
292
|
const storedCookie = (await Preferences.get({ key: normalizedCookieName }))?.value;
|
|
259
293
|
const cookie = getCookie(storedCookie || "{}");
|
|
@@ -301,6 +335,6 @@ export function capacitorClient(opts) {
|
|
|
301
335
|
]
|
|
302
336
|
};
|
|
303
337
|
}
|
|
304
|
-
export
|
|
305
|
-
export
|
|
338
|
+
export { setupCapacitorFocusManager } from "./focus-manager.js";
|
|
339
|
+
export { setupCapacitorOnlineManager } from "./online-manager.js";
|
|
306
340
|
export { parseSetCookieHeader } from "better-auth/cookies";
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { App } from "@capacitor/app";
|
|
2
1
|
import { kFocusManager } from "better-auth/client";
|
|
2
|
+
let _App = null;
|
|
3
|
+
async function getApp() {
|
|
4
|
+
if (!_App) {
|
|
5
|
+
const mod = await import("@capacitor/app");
|
|
6
|
+
_App = mod.App;
|
|
7
|
+
}
|
|
8
|
+
return _App;
|
|
9
|
+
}
|
|
3
10
|
class CapacitorFocusManager {
|
|
4
11
|
listeners = /* @__PURE__ */ new Set();
|
|
5
12
|
isFocused;
|
|
@@ -17,10 +24,13 @@ class CapacitorFocusManager {
|
|
|
17
24
|
this.listeners.forEach((listener) => listener(focused));
|
|
18
25
|
}
|
|
19
26
|
setup() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
getApp().then((App) => {
|
|
28
|
+
App.addListener("appStateChange", (state) => {
|
|
29
|
+
this.setFocused(state.isActive);
|
|
30
|
+
}).then((handle) => {
|
|
31
|
+
this.unsubscribe = () => handle.remove();
|
|
32
|
+
}).catch(() => {
|
|
33
|
+
});
|
|
24
34
|
}).catch(() => {
|
|
25
35
|
});
|
|
26
36
|
return () => {
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { Network } from "@capacitor/network";
|
|
2
1
|
import { kOnlineManager } from "better-auth/client";
|
|
2
|
+
let _Network = null;
|
|
3
|
+
async function getNetwork() {
|
|
4
|
+
if (!_Network) {
|
|
5
|
+
const mod = await import("@capacitor/network");
|
|
6
|
+
_Network = mod.Network;
|
|
7
|
+
}
|
|
8
|
+
return _Network;
|
|
9
|
+
}
|
|
3
10
|
class CapacitorOnlineManager {
|
|
4
11
|
listeners = /* @__PURE__ */ new Set();
|
|
5
12
|
isOnline = true;
|
|
@@ -17,10 +24,14 @@ class CapacitorOnlineManager {
|
|
|
17
24
|
this.listeners.forEach((listener) => listener(online));
|
|
18
25
|
}
|
|
19
26
|
setup() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
getNetwork().then((Network) => {
|
|
28
|
+
Network.addListener("networkStatusChange", (status) => {
|
|
29
|
+
this.setOnline(status.connected);
|
|
30
|
+
}).then((handle) => {
|
|
31
|
+
this.unsubscribe = () => handle.remove();
|
|
32
|
+
}).catch(() => {
|
|
33
|
+
this.setOnline(true);
|
|
34
|
+
});
|
|
24
35
|
}).catch(() => {
|
|
25
36
|
this.setOnline(true);
|
|
26
37
|
});
|