@dashadmin/dash-auth 1.3.24 → 1.3.26

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/dist/index.cjs ADDED
@@ -0,0 +1,473 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
37
+ var __async = (__this, __arguments, generator) => {
38
+ return new Promise((resolve, reject) => {
39
+ var fulfilled = (value) => {
40
+ try {
41
+ step(generator.next(value));
42
+ } catch (e) {
43
+ reject(e);
44
+ }
45
+ };
46
+ var rejected = (value) => {
47
+ try {
48
+ step(generator.throw(value));
49
+ } catch (e) {
50
+ reject(e);
51
+ }
52
+ };
53
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
54
+ step((generator = generator.apply(__this, __arguments)).next());
55
+ });
56
+ };
57
+
58
+ // src/index.ts
59
+ var index_exports = {};
60
+ __export(index_exports, {
61
+ AuthPersistenceService: () => AuthPersistenceService,
62
+ clearDeviceStoreAuth: () => clearDeviceStoreAuth,
63
+ syncDeviceStoreToLocalStorage: () => syncDeviceStoreToLocalStorage,
64
+ syncLocalStorageToDeviceStore: () => syncLocalStorageToDeviceStore
65
+ });
66
+ module.exports = __toCommonJS(index_exports);
67
+
68
+ // src/AuthPersistanceService.tsx
69
+ var import_dash_utils = require("dash-utils");
70
+ var AuthPersistenceService = class {
71
+ static saveAuth(authData) {
72
+ var _a, _b;
73
+ try {
74
+ const authToPersist = {
75
+ auth: authData.auth
76
+ // Only save auth.auth, not auth.user
77
+ };
78
+ if ((_a = authData.auth) == null ? void 0 : _a.tenantImages) {
79
+ import_dash_utils.dashStorage.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(authData.auth.tenantImages));
80
+ } else {
81
+ import_dash_utils.dashStorage.removeItem(this.TENANT_IMAGES_KEY);
82
+ }
83
+ if ((_b = authData.auth) == null ? void 0 : _b.tenantSettings) {
84
+ import_dash_utils.dashStorage.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(authData.auth.tenantSettings));
85
+ } else {
86
+ import_dash_utils.dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
87
+ }
88
+ if (authData.systemValues) {
89
+ import_dash_utils.dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(authData.systemValues));
90
+ }
91
+ const cleanAuthData = __spreadValues({}, authToPersist);
92
+ delete cleanAuthData._loggedOut;
93
+ delete cleanAuthData._loggedOutAt;
94
+ import_dash_utils.dashStorage.setItem(this.AUTH_KEY, JSON.stringify(cleanAuthData));
95
+ import_dash_utils.dashStorage.setItem(this.TIMESTAMP_KEY, Date.now().toString());
96
+ } catch (error) {
97
+ console.error("Failed to save auth data:", error);
98
+ }
99
+ }
100
+ static setAuth(authData) {
101
+ try {
102
+ if (authData.token) {
103
+ import_dash_utils.dashStorage.setItem("token", authData.token);
104
+ }
105
+ if (authData.user) {
106
+ import_dash_utils.dashStorage.setItem("user", JSON.stringify(authData.user));
107
+ }
108
+ if (authData.systemValues) {
109
+ import_dash_utils.dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(authData.systemValues));
110
+ }
111
+ import_dash_utils.dashStorage.setItem("authenticated", "true");
112
+ this.saveAuth({
113
+ auth: {
114
+ user: authData.user,
115
+ token: authData.token,
116
+ refreshToken: authData.refreshToken
117
+ // Include any other auth-related data
118
+ },
119
+ systemValues: authData.systemValues
120
+ });
121
+ console.log("Auth data set successfully");
122
+ } catch (error) {
123
+ console.error("Failed to set auth data:", error);
124
+ }
125
+ }
126
+ static getToken() {
127
+ try {
128
+ return import_dash_utils.dashStorage.getItem("token");
129
+ } catch (error) {
130
+ console.error("Failed to get token:", error);
131
+ return null;
132
+ }
133
+ }
134
+ static getUser() {
135
+ try {
136
+ const userData = import_dash_utils.dashStorage.getItem("user");
137
+ return userData ? JSON.parse(userData) : null;
138
+ } catch (error) {
139
+ console.error("Failed to get user data:", error);
140
+ return null;
141
+ }
142
+ }
143
+ static getAuth() {
144
+ try {
145
+ const authData = import_dash_utils.dashStorage.getItem(this.AUTH_KEY);
146
+ const timestamp = import_dash_utils.dashStorage.getItem(this.TIMESTAMP_KEY);
147
+ if (!authData || !timestamp) {
148
+ return null;
149
+ }
150
+ const savedTime = parseInt(timestamp);
151
+ const currentTime = Date.now();
152
+ const hoursDiff = (currentTime - savedTime) / (1e3 * 60 * 60);
153
+ if (hoursDiff > this.EXPIRY_HOURS) {
154
+ this.clearAuth();
155
+ return null;
156
+ }
157
+ const parsedData = JSON.parse(authData);
158
+ if (parsedData._loggedOut) {
159
+ console.log("Auth data exists but user is marked as logged out");
160
+ return null;
161
+ }
162
+ return parsedData;
163
+ } catch (error) {
164
+ console.error("Failed to retrieve auth data:", error);
165
+ this.clearAuth();
166
+ return null;
167
+ }
168
+ }
169
+ static markAsLoggedOut() {
170
+ try {
171
+ const authData = import_dash_utils.dashStorage.getItem(this.AUTH_KEY);
172
+ if (authData) {
173
+ const parsedData = JSON.parse(authData);
174
+ const loggedOutAuth = __spreadProps(__spreadValues({}, parsedData), {
175
+ _loggedOut: true,
176
+ _loggedOutAt: Date.now()
177
+ });
178
+ import_dash_utils.dashStorage.setItem(this.AUTH_KEY, JSON.stringify(loggedOutAuth));
179
+ console.log("Auth data marked as logged out but preserved in localStorage");
180
+ }
181
+ import_dash_utils.dashStorage.removeItem("token");
182
+ import_dash_utils.dashStorage.setItem("authenticated", "false");
183
+ import_dash_utils.dashStorage.removeItem("user");
184
+ } catch (error) {
185
+ console.error("Failed to mark auth as logged out:", error);
186
+ }
187
+ }
188
+ static getTenantImages() {
189
+ try {
190
+ const tenantImages = import_dash_utils.dashStorage.getItem(this.TENANT_IMAGES_KEY);
191
+ return tenantImages ? JSON.parse(tenantImages) : null;
192
+ } catch (error) {
193
+ console.error("Failed to get tenant images:", error);
194
+ return null;
195
+ }
196
+ }
197
+ static setTenantImages(images) {
198
+ try {
199
+ import_dash_utils.dashStorage.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(images));
200
+ } catch (error) {
201
+ console.error("Failed to set tenant images:", error);
202
+ }
203
+ }
204
+ static getTenantSettings() {
205
+ try {
206
+ const tenantSettings = import_dash_utils.dashStorage.getItem(this.TENANT_SETTINGS_KEY);
207
+ return tenantSettings ? JSON.parse(tenantSettings) : null;
208
+ } catch (error) {
209
+ console.error("Failed to get tenant settings:", error);
210
+ return null;
211
+ }
212
+ }
213
+ static setTenantSettings(settings) {
214
+ try {
215
+ import_dash_utils.dashStorage.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(settings));
216
+ } catch (error) {
217
+ console.error("Failed to set tenant settings:", error);
218
+ }
219
+ }
220
+ static clearTenantSettings() {
221
+ try {
222
+ import_dash_utils.dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
223
+ } catch (error) {
224
+ console.error("Failed to clear tenant settings:", error);
225
+ }
226
+ }
227
+ static clearTenantImages() {
228
+ try {
229
+ import_dash_utils.dashStorage.removeItem(this.TENANT_IMAGES_KEY);
230
+ } catch (error) {
231
+ console.error("Failed to clear tenant images:", error);
232
+ }
233
+ }
234
+ static getSystemValues() {
235
+ try {
236
+ const systemValues = import_dash_utils.dashStorage.getItem(this.SYSTEM_VALUES_KEY);
237
+ return systemValues ? JSON.parse(systemValues) : null;
238
+ } catch (error) {
239
+ console.error("Failed to get system values:", error);
240
+ return null;
241
+ }
242
+ }
243
+ static setSystemValues(values) {
244
+ try {
245
+ import_dash_utils.dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(values));
246
+ } catch (error) {
247
+ console.error("Failed to set system values:", error);
248
+ }
249
+ }
250
+ static getSystemValue(key) {
251
+ try {
252
+ const systemValues = this.getSystemValues();
253
+ return systemValues ? systemValues[key] : null;
254
+ } catch (error) {
255
+ console.error(`Failed to get system value for key '${key}':`, error);
256
+ return null;
257
+ }
258
+ }
259
+ static getPointOfSales() {
260
+ return this.getSystemValue("point_of_sales");
261
+ }
262
+ static clearAuth() {
263
+ import_dash_utils.dashStorage.removeItem(this.AUTH_KEY);
264
+ import_dash_utils.dashStorage.removeItem(this.TIMESTAMP_KEY);
265
+ import_dash_utils.dashStorage.removeItem("token");
266
+ import_dash_utils.dashStorage.removeItem("user");
267
+ import_dash_utils.dashStorage.setItem("authenticated", "false");
268
+ }
269
+ static clearAllAuthData() {
270
+ import_dash_utils.dashStorage.removeItem(this.AUTH_KEY);
271
+ import_dash_utils.dashStorage.removeItem(this.TIMESTAMP_KEY);
272
+ import_dash_utils.dashStorage.removeItem(this.TENANT_IMAGES_KEY);
273
+ import_dash_utils.dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
274
+ import_dash_utils.dashStorage.removeItem(this.SYSTEM_VALUES_KEY);
275
+ import_dash_utils.dashStorage.removeItem("token");
276
+ import_dash_utils.dashStorage.removeItem("user");
277
+ import_dash_utils.dashStorage.setItem("authenticated", "false");
278
+ }
279
+ static getStoredAuthData() {
280
+ try {
281
+ const token = import_dash_utils.dashStorage.getItem("token");
282
+ const userData = import_dash_utils.dashStorage.getItem("user");
283
+ const systemValuesData = import_dash_utils.dashStorage.getItem(this.SYSTEM_VALUES_KEY);
284
+ const authData = import_dash_utils.dashStorage.getItem(this.AUTH_KEY);
285
+ const tenantImagesData = import_dash_utils.dashStorage.getItem(this.TENANT_IMAGES_KEY);
286
+ const tenantSettingsData = import_dash_utils.dashStorage.getItem(this.TENANT_SETTINGS_KEY);
287
+ const user = userData ? JSON.parse(userData) : null;
288
+ const systemValues = systemValuesData ? JSON.parse(systemValuesData) : null;
289
+ const auth = authData ? JSON.parse(authData) : null;
290
+ const tenantImages = tenantImagesData ? JSON.parse(tenantImagesData) : null;
291
+ const tenantSettings = tenantSettingsData ? JSON.parse(tenantSettingsData) : null;
292
+ return {
293
+ token,
294
+ user,
295
+ systemValues,
296
+ auth: (auth == null ? void 0 : auth.auth) || null,
297
+ tenantImages,
298
+ tenantSettings
299
+ };
300
+ } catch (error) {
301
+ console.error("Failed to get stored auth data:", error);
302
+ return null;
303
+ }
304
+ }
305
+ static isAuthValid() {
306
+ return this.getAuth() !== null;
307
+ }
308
+ static getPermissions() {
309
+ var _a, _b;
310
+ try {
311
+ const storedRoles = import_dash_utils.dashStorage.getItem("roles");
312
+ if (storedRoles === "guest") {
313
+ return Promise.resolve("guest");
314
+ }
315
+ const authData = this.getAuth();
316
+ if ((_b = (_a = authData == null ? void 0 : authData.auth) == null ? void 0 : _a.user) == null ? void 0 : _b.roles) {
317
+ const processedPermissions = {
318
+ roles: authData.auth.user.roles.map(
319
+ (item) => typeof item === "string" ? item : item.name
320
+ )
321
+ };
322
+ return Promise.resolve(processedPermissions);
323
+ }
324
+ if (storedRoles) {
325
+ try {
326
+ const parsedRoles = JSON.parse(storedRoles);
327
+ const processedPermissions = {
328
+ roles: Array.isArray(parsedRoles) ? parsedRoles.map((item) => typeof item === "string" ? item : item.name) : [parsedRoles]
329
+ };
330
+ return Promise.resolve(processedPermissions);
331
+ } catch (parseError) {
332
+ console.error("Failed to parse roles from localStorage:", parseError);
333
+ return Promise.resolve("null");
334
+ }
335
+ }
336
+ return Promise.resolve("null");
337
+ } catch (error) {
338
+ console.error("Failed to get permissions:", error);
339
+ return Promise.resolve("null");
340
+ }
341
+ }
342
+ };
343
+ __publicField(AuthPersistenceService, "AUTH_KEY", "dashAuth");
344
+ __publicField(AuthPersistenceService, "TIMESTAMP_KEY", "dashAuthTimestamp");
345
+ __publicField(AuthPersistenceService, "TENANT_IMAGES_KEY", "dashTenantImages");
346
+ __publicField(AuthPersistenceService, "TENANT_SETTINGS_KEY", "dashTenantSettings");
347
+ __publicField(AuthPersistenceService, "SYSTEM_VALUES_KEY", "dashSystemValues");
348
+ __publicField(AuthPersistenceService, "EXPIRY_HOURS", 24);
349
+ function syncDeviceStoreToLocalStorage() {
350
+ return __async(this, null, function* () {
351
+ var _a, _b, _c;
352
+ const electronStore = window.electronStore;
353
+ if (electronStore && electronStore.getAll) {
354
+ try {
355
+ const allData = yield electronStore.getAll();
356
+ if (allData && typeof allData === "object") {
357
+ for (const [key, value] of Object.entries(allData)) {
358
+ window.localStorage.setItem(key, JSON.stringify(value));
359
+ }
360
+ console.log("[ElectronStorageSync] Synced electron-store to localStorage");
361
+ }
362
+ return;
363
+ } catch (err) {
364
+ console.error("[ElectronStorageSync] Failed to sync:", err);
365
+ }
366
+ }
367
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
368
+ if (Preferences && Preferences.keys && Preferences.get) {
369
+ try {
370
+ const { keys } = yield Preferences.keys();
371
+ for (const key of keys) {
372
+ const { value } = yield Preferences.get({ key });
373
+ if (value !== null) {
374
+ window.localStorage.setItem(key, value);
375
+ }
376
+ }
377
+ console.log("[CapacitorStorageSync] Synced Preferences to localStorage");
378
+ } catch (err) {
379
+ console.error("[CapacitorStorageSync] Failed to sync:", err);
380
+ }
381
+ }
382
+ });
383
+ }
384
+ function syncLocalStorageToDeviceStore() {
385
+ return __async(this, null, function* () {
386
+ var _a, _b, _c;
387
+ const electronStore = window.electronStore;
388
+ if (electronStore && electronStore.set) {
389
+ try {
390
+ for (let i = 0; i < window.localStorage.length; i++) {
391
+ const key = window.localStorage.key(i);
392
+ if (!key) continue;
393
+ const value = window.localStorage.getItem(key);
394
+ try {
395
+ yield electronStore.set(key, JSON.parse(value));
396
+ } catch (e) {
397
+ yield electronStore.set(key, value);
398
+ }
399
+ }
400
+ console.log("[ElectronStorageSync] Synced localStorage to electron-store");
401
+ return;
402
+ } catch (err) {
403
+ console.error("[ElectronStorageSync] Failed to sync:", err);
404
+ }
405
+ }
406
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
407
+ if (Preferences && Preferences.set) {
408
+ try {
409
+ for (let i = 0; i < window.localStorage.length; i++) {
410
+ const key = window.localStorage.key(i);
411
+ if (!key) continue;
412
+ const value = window.localStorage.getItem(key);
413
+ if (value !== null) {
414
+ yield Preferences.set({ key, value });
415
+ }
416
+ }
417
+ console.log("[CapacitorStorageSync] Synced localStorage to Preferences");
418
+ } catch (err) {
419
+ console.error("[CapacitorStorageSync] Failed to sync:", err);
420
+ }
421
+ }
422
+ });
423
+ }
424
+ function clearDeviceStoreAuth() {
425
+ return __async(this, null, function* () {
426
+ var _a, _b, _c;
427
+ const authKeys = [
428
+ "token",
429
+ "user",
430
+ "authenticated",
431
+ "roles",
432
+ "dashAuth",
433
+ "dashAuthTimestamp",
434
+ "dashSystemValues",
435
+ "socketConnectionState",
436
+ "SerializedAuthContext",
437
+ "tenant_id",
438
+ "user_id"
439
+ ];
440
+ const electronStore = window.electronStore;
441
+ if (electronStore && electronStore.delete && electronStore.getAll) {
442
+ try {
443
+ for (const key of authKeys) {
444
+ yield electronStore.delete(key);
445
+ }
446
+ yield electronStore.set("authenticated", false);
447
+ console.log("[ElectronStorageSync] Cleared auth data from electron-store");
448
+ return;
449
+ } catch (err) {
450
+ console.error("[ElectronStorageSync] Failed to clear auth:", err);
451
+ }
452
+ }
453
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
454
+ if (Preferences && Preferences.remove) {
455
+ try {
456
+ for (const key of authKeys) {
457
+ yield Preferences.remove({ key });
458
+ }
459
+ yield Preferences.set({ key: "authenticated", value: "false" });
460
+ console.log("[CapacitorStorageSync] Cleared auth data from Preferences");
461
+ } catch (err) {
462
+ console.error("[CapacitorStorageSync] Failed to clear auth:", err);
463
+ }
464
+ }
465
+ });
466
+ }
467
+ // Annotate the CommonJS export names for ESM import in node:
468
+ 0 && (module.exports = {
469
+ AuthPersistenceService,
470
+ clearDeviceStoreAuth,
471
+ syncDeviceStoreToLocalStorage,
472
+ syncLocalStorageToDeviceStore
473
+ });
@@ -0,0 +1,41 @@
1
+ declare class AuthPersistenceService {
2
+ private static readonly AUTH_KEY;
3
+ private static readonly TIMESTAMP_KEY;
4
+ private static readonly TENANT_IMAGES_KEY;
5
+ private static readonly TENANT_SETTINGS_KEY;
6
+ private static readonly SYSTEM_VALUES_KEY;
7
+ private static readonly EXPIRY_HOURS;
8
+ static saveAuth(authData: any): void;
9
+ static setAuth(authData: any): void;
10
+ static getToken(): string | null;
11
+ static getUser(): any | null;
12
+ static getAuth(): any | null;
13
+ static markAsLoggedOut(): void;
14
+ static getTenantImages(): any | null;
15
+ static setTenantImages(images: any): void;
16
+ static getTenantSettings(): any | null;
17
+ static setTenantSettings(settings: any): void;
18
+ static clearTenantSettings(): void;
19
+ static clearTenantImages(): void;
20
+ static getSystemValues(): any | null;
21
+ static setSystemValues(values: any): void;
22
+ static getSystemValue(key: string): any | null;
23
+ static getPointOfSales(): any | null;
24
+ static clearAuth(): void;
25
+ static clearAllAuthData(): void;
26
+ static getStoredAuthData(): {
27
+ token: string | null;
28
+ user: any | null;
29
+ systemValues: any | null;
30
+ auth: any | null;
31
+ tenantImages: any | null;
32
+ tenantSettings: any | null;
33
+ } | null;
34
+ static isAuthValid(): boolean;
35
+ static getPermissions(): Promise<any>;
36
+ }
37
+ declare function syncDeviceStoreToLocalStorage(): Promise<void>;
38
+ declare function syncLocalStorageToDeviceStore(): Promise<void>;
39
+ declare function clearDeviceStoreAuth(): Promise<void>;
40
+
41
+ export { AuthPersistenceService, clearDeviceStoreAuth, syncDeviceStoreToLocalStorage, syncLocalStorageToDeviceStore };
@@ -0,0 +1,41 @@
1
+ declare class AuthPersistenceService {
2
+ private static readonly AUTH_KEY;
3
+ private static readonly TIMESTAMP_KEY;
4
+ private static readonly TENANT_IMAGES_KEY;
5
+ private static readonly TENANT_SETTINGS_KEY;
6
+ private static readonly SYSTEM_VALUES_KEY;
7
+ private static readonly EXPIRY_HOURS;
8
+ static saveAuth(authData: any): void;
9
+ static setAuth(authData: any): void;
10
+ static getToken(): string | null;
11
+ static getUser(): any | null;
12
+ static getAuth(): any | null;
13
+ static markAsLoggedOut(): void;
14
+ static getTenantImages(): any | null;
15
+ static setTenantImages(images: any): void;
16
+ static getTenantSettings(): any | null;
17
+ static setTenantSettings(settings: any): void;
18
+ static clearTenantSettings(): void;
19
+ static clearTenantImages(): void;
20
+ static getSystemValues(): any | null;
21
+ static setSystemValues(values: any): void;
22
+ static getSystemValue(key: string): any | null;
23
+ static getPointOfSales(): any | null;
24
+ static clearAuth(): void;
25
+ static clearAllAuthData(): void;
26
+ static getStoredAuthData(): {
27
+ token: string | null;
28
+ user: any | null;
29
+ systemValues: any | null;
30
+ auth: any | null;
31
+ tenantImages: any | null;
32
+ tenantSettings: any | null;
33
+ } | null;
34
+ static isAuthValid(): boolean;
35
+ static getPermissions(): Promise<any>;
36
+ }
37
+ declare function syncDeviceStoreToLocalStorage(): Promise<void>;
38
+ declare function syncLocalStorageToDeviceStore(): Promise<void>;
39
+ declare function clearDeviceStoreAuth(): Promise<void>;
40
+
41
+ export { AuthPersistenceService, clearDeviceStoreAuth, syncDeviceStoreToLocalStorage, syncLocalStorageToDeviceStore };
package/dist/index.js CHANGED
@@ -1,303 +1,446 @@
1
- import { dashStorage as r } from "@dashadmin/dash-utils";
2
- class h {
3
- static AUTH_KEY = "dashAuth";
4
- static TIMESTAMP_KEY = "dashAuthTimestamp";
5
- static TENANT_IMAGES_KEY = "dashTenantImages";
6
- static TENANT_SETTINGS_KEY = "dashTenantSettings";
7
- static SYSTEM_VALUES_KEY = "dashSystemValues";
8
- static EXPIRY_HOURS = 24;
9
- static saveAuth(e) {
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+ var __async = (__this, __arguments, generator) => {
22
+ return new Promise((resolve, reject) => {
23
+ var fulfilled = (value) => {
24
+ try {
25
+ step(generator.next(value));
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ };
30
+ var rejected = (value) => {
31
+ try {
32
+ step(generator.throw(value));
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ };
37
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
38
+ step((generator = generator.apply(__this, __arguments)).next());
39
+ });
40
+ };
41
+
42
+ // src/AuthPersistanceService.tsx
43
+ import { dashStorage } from "dash-utils";
44
+ var AuthPersistenceService = class {
45
+ static saveAuth(authData) {
46
+ var _a, _b;
10
47
  try {
11
- const t = {
12
- auth: e.auth
48
+ const authToPersist = {
49
+ auth: authData.auth
13
50
  // Only save auth.auth, not auth.user
14
51
  };
15
- e.auth?.tenantImages ? r.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(e.auth.tenantImages)) : r.removeItem(this.TENANT_IMAGES_KEY), e.auth?.tenantSettings ? r.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(e.auth.tenantSettings)) : r.removeItem(this.TENANT_SETTINGS_KEY), e.systemValues && r.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(e.systemValues));
16
- const s = { ...t };
17
- delete s._loggedOut, delete s._loggedOutAt, r.setItem(this.AUTH_KEY, JSON.stringify(s)), r.setItem(this.TIMESTAMP_KEY, Date.now().toString());
18
- } catch (t) {
19
- console.error("Failed to save auth data:", t);
52
+ if ((_a = authData.auth) == null ? void 0 : _a.tenantImages) {
53
+ dashStorage.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(authData.auth.tenantImages));
54
+ } else {
55
+ dashStorage.removeItem(this.TENANT_IMAGES_KEY);
56
+ }
57
+ if ((_b = authData.auth) == null ? void 0 : _b.tenantSettings) {
58
+ dashStorage.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(authData.auth.tenantSettings));
59
+ } else {
60
+ dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
61
+ }
62
+ if (authData.systemValues) {
63
+ dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(authData.systemValues));
64
+ }
65
+ const cleanAuthData = __spreadValues({}, authToPersist);
66
+ delete cleanAuthData._loggedOut;
67
+ delete cleanAuthData._loggedOutAt;
68
+ dashStorage.setItem(this.AUTH_KEY, JSON.stringify(cleanAuthData));
69
+ dashStorage.setItem(this.TIMESTAMP_KEY, Date.now().toString());
70
+ } catch (error) {
71
+ console.error("Failed to save auth data:", error);
20
72
  }
21
73
  }
22
- static setAuth(e) {
74
+ static setAuth(authData) {
23
75
  try {
24
- e.token && r.setItem("token", e.token), e.user && r.setItem("user", JSON.stringify(e.user)), e.systemValues && r.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(e.systemValues)), r.setItem("authenticated", "true"), this.saveAuth({
76
+ if (authData.token) {
77
+ dashStorage.setItem("token", authData.token);
78
+ }
79
+ if (authData.user) {
80
+ dashStorage.setItem("user", JSON.stringify(authData.user));
81
+ }
82
+ if (authData.systemValues) {
83
+ dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(authData.systemValues));
84
+ }
85
+ dashStorage.setItem("authenticated", "true");
86
+ this.saveAuth({
25
87
  auth: {
26
- user: e.user,
27
- token: e.token,
28
- refreshToken: e.refreshToken
88
+ user: authData.user,
89
+ token: authData.token,
90
+ refreshToken: authData.refreshToken
29
91
  // Include any other auth-related data
30
92
  },
31
- systemValues: e.systemValues
32
- }), console.log("Auth data set successfully");
33
- } catch (t) {
34
- console.error("Failed to set auth data:", t);
93
+ systemValues: authData.systemValues
94
+ });
95
+ console.log("Auth data set successfully");
96
+ } catch (error) {
97
+ console.error("Failed to set auth data:", error);
35
98
  }
36
99
  }
37
100
  static getToken() {
38
101
  try {
39
- return r.getItem("token");
40
- } catch (e) {
41
- return console.error("Failed to get token:", e), null;
102
+ return dashStorage.getItem("token");
103
+ } catch (error) {
104
+ console.error("Failed to get token:", error);
105
+ return null;
42
106
  }
43
107
  }
44
108
  static getUser() {
45
109
  try {
46
- const e = r.getItem("user");
47
- return e ? JSON.parse(e) : null;
48
- } catch (e) {
49
- return console.error("Failed to get user data:", e), null;
110
+ const userData = dashStorage.getItem("user");
111
+ return userData ? JSON.parse(userData) : null;
112
+ } catch (error) {
113
+ console.error("Failed to get user data:", error);
114
+ return null;
50
115
  }
51
116
  }
52
117
  static getAuth() {
53
118
  try {
54
- const e = r.getItem(this.AUTH_KEY), t = r.getItem(this.TIMESTAMP_KEY);
55
- if (!e || !t)
119
+ const authData = dashStorage.getItem(this.AUTH_KEY);
120
+ const timestamp = dashStorage.getItem(this.TIMESTAMP_KEY);
121
+ if (!authData || !timestamp) {
56
122
  return null;
57
- const s = parseInt(t);
58
- if ((Date.now() - s) / (1e3 * 60 * 60) > this.EXPIRY_HOURS)
59
- return this.clearAuth(), null;
60
- const c = JSON.parse(e);
61
- return c._loggedOut ? (console.log("Auth data exists but user is marked as logged out"), null) : c;
62
- } catch (e) {
63
- return console.error("Failed to retrieve auth data:", e), this.clearAuth(), null;
123
+ }
124
+ const savedTime = parseInt(timestamp);
125
+ const currentTime = Date.now();
126
+ const hoursDiff = (currentTime - savedTime) / (1e3 * 60 * 60);
127
+ if (hoursDiff > this.EXPIRY_HOURS) {
128
+ this.clearAuth();
129
+ return null;
130
+ }
131
+ const parsedData = JSON.parse(authData);
132
+ if (parsedData._loggedOut) {
133
+ console.log("Auth data exists but user is marked as logged out");
134
+ return null;
135
+ }
136
+ return parsedData;
137
+ } catch (error) {
138
+ console.error("Failed to retrieve auth data:", error);
139
+ this.clearAuth();
140
+ return null;
64
141
  }
65
142
  }
66
143
  static markAsLoggedOut() {
67
144
  try {
68
- const e = r.getItem(this.AUTH_KEY);
69
- if (e) {
70
- const s = {
71
- ...JSON.parse(e),
72
- _loggedOut: !0,
145
+ const authData = dashStorage.getItem(this.AUTH_KEY);
146
+ if (authData) {
147
+ const parsedData = JSON.parse(authData);
148
+ const loggedOutAuth = __spreadProps(__spreadValues({}, parsedData), {
149
+ _loggedOut: true,
73
150
  _loggedOutAt: Date.now()
74
- };
75
- r.setItem(this.AUTH_KEY, JSON.stringify(s)), console.log("Auth data marked as logged out but preserved in localStorage");
151
+ });
152
+ dashStorage.setItem(this.AUTH_KEY, JSON.stringify(loggedOutAuth));
153
+ console.log("Auth data marked as logged out but preserved in localStorage");
76
154
  }
77
- r.removeItem("token"), r.setItem("authenticated", "false"), r.removeItem("user");
78
- } catch (e) {
79
- console.error("Failed to mark auth as logged out:", e);
155
+ dashStorage.removeItem("token");
156
+ dashStorage.setItem("authenticated", "false");
157
+ dashStorage.removeItem("user");
158
+ } catch (error) {
159
+ console.error("Failed to mark auth as logged out:", error);
80
160
  }
81
161
  }
82
162
  static getTenantImages() {
83
163
  try {
84
- const e = r.getItem(this.TENANT_IMAGES_KEY);
85
- return e ? JSON.parse(e) : null;
86
- } catch (e) {
87
- return console.error("Failed to get tenant images:", e), null;
164
+ const tenantImages = dashStorage.getItem(this.TENANT_IMAGES_KEY);
165
+ return tenantImages ? JSON.parse(tenantImages) : null;
166
+ } catch (error) {
167
+ console.error("Failed to get tenant images:", error);
168
+ return null;
88
169
  }
89
170
  }
90
- static setTenantImages(e) {
171
+ static setTenantImages(images) {
91
172
  try {
92
- r.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(e));
93
- } catch (t) {
94
- console.error("Failed to set tenant images:", t);
173
+ dashStorage.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(images));
174
+ } catch (error) {
175
+ console.error("Failed to set tenant images:", error);
95
176
  }
96
177
  }
97
178
  static getTenantSettings() {
98
179
  try {
99
- const e = r.getItem(this.TENANT_SETTINGS_KEY);
100
- return e ? JSON.parse(e) : null;
101
- } catch (e) {
102
- return console.error("Failed to get tenant settings:", e), null;
180
+ const tenantSettings = dashStorage.getItem(this.TENANT_SETTINGS_KEY);
181
+ return tenantSettings ? JSON.parse(tenantSettings) : null;
182
+ } catch (error) {
183
+ console.error("Failed to get tenant settings:", error);
184
+ return null;
103
185
  }
104
186
  }
105
- static setTenantSettings(e) {
187
+ static setTenantSettings(settings) {
106
188
  try {
107
- r.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(e));
108
- } catch (t) {
109
- console.error("Failed to set tenant settings:", t);
189
+ dashStorage.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(settings));
190
+ } catch (error) {
191
+ console.error("Failed to set tenant settings:", error);
110
192
  }
111
193
  }
112
194
  static clearTenantSettings() {
113
195
  try {
114
- r.removeItem(this.TENANT_SETTINGS_KEY);
115
- } catch (e) {
116
- console.error("Failed to clear tenant settings:", e);
196
+ dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
197
+ } catch (error) {
198
+ console.error("Failed to clear tenant settings:", error);
117
199
  }
118
200
  }
119
201
  static clearTenantImages() {
120
202
  try {
121
- r.removeItem(this.TENANT_IMAGES_KEY);
122
- } catch (e) {
123
- console.error("Failed to clear tenant images:", e);
203
+ dashStorage.removeItem(this.TENANT_IMAGES_KEY);
204
+ } catch (error) {
205
+ console.error("Failed to clear tenant images:", error);
124
206
  }
125
207
  }
126
208
  static getSystemValues() {
127
209
  try {
128
- const e = r.getItem(this.SYSTEM_VALUES_KEY);
129
- return e ? JSON.parse(e) : null;
130
- } catch (e) {
131
- return console.error("Failed to get system values:", e), null;
210
+ const systemValues = dashStorage.getItem(this.SYSTEM_VALUES_KEY);
211
+ return systemValues ? JSON.parse(systemValues) : null;
212
+ } catch (error) {
213
+ console.error("Failed to get system values:", error);
214
+ return null;
132
215
  }
133
216
  }
134
- static setSystemValues(e) {
217
+ static setSystemValues(values) {
135
218
  try {
136
- r.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(e));
137
- } catch (t) {
138
- console.error("Failed to set system values:", t);
219
+ dashStorage.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(values));
220
+ } catch (error) {
221
+ console.error("Failed to set system values:", error);
139
222
  }
140
223
  }
141
- static getSystemValue(e) {
224
+ static getSystemValue(key) {
142
225
  try {
143
- const t = this.getSystemValues();
144
- return t ? t[e] : null;
145
- } catch (t) {
146
- return console.error(`Failed to get system value for key '${e}':`, t), null;
226
+ const systemValues = this.getSystemValues();
227
+ return systemValues ? systemValues[key] : null;
228
+ } catch (error) {
229
+ console.error(`Failed to get system value for key '${key}':`, error);
230
+ return null;
147
231
  }
148
232
  }
149
233
  static getPointOfSales() {
150
234
  return this.getSystemValue("point_of_sales");
151
235
  }
152
236
  static clearAuth() {
153
- r.removeItem(this.AUTH_KEY), r.removeItem(this.TIMESTAMP_KEY), r.removeItem("token"), r.removeItem("user"), r.setItem("authenticated", "false");
237
+ dashStorage.removeItem(this.AUTH_KEY);
238
+ dashStorage.removeItem(this.TIMESTAMP_KEY);
239
+ dashStorage.removeItem("token");
240
+ dashStorage.removeItem("user");
241
+ dashStorage.setItem("authenticated", "false");
154
242
  }
155
243
  static clearAllAuthData() {
156
- r.removeItem(this.AUTH_KEY), r.removeItem(this.TIMESTAMP_KEY), r.removeItem(this.TENANT_IMAGES_KEY), r.removeItem(this.TENANT_SETTINGS_KEY), r.removeItem(this.SYSTEM_VALUES_KEY), r.removeItem("token"), r.removeItem("user"), r.setItem("authenticated", "false");
244
+ dashStorage.removeItem(this.AUTH_KEY);
245
+ dashStorage.removeItem(this.TIMESTAMP_KEY);
246
+ dashStorage.removeItem(this.TENANT_IMAGES_KEY);
247
+ dashStorage.removeItem(this.TENANT_SETTINGS_KEY);
248
+ dashStorage.removeItem(this.SYSTEM_VALUES_KEY);
249
+ dashStorage.removeItem("token");
250
+ dashStorage.removeItem("user");
251
+ dashStorage.setItem("authenticated", "false");
157
252
  }
158
253
  static getStoredAuthData() {
159
254
  try {
160
- const e = r.getItem("token"), t = r.getItem("user"), s = r.getItem(this.SYSTEM_VALUES_KEY), o = r.getItem(this.AUTH_KEY), n = r.getItem(this.TENANT_IMAGES_KEY), c = r.getItem(this.TENANT_SETTINGS_KEY), l = t ? JSON.parse(t) : null, i = s ? JSON.parse(s) : null, u = o ? JSON.parse(o) : null, S = n ? JSON.parse(n) : null, g = c ? JSON.parse(c) : null;
255
+ const token = dashStorage.getItem("token");
256
+ const userData = dashStorage.getItem("user");
257
+ const systemValuesData = dashStorage.getItem(this.SYSTEM_VALUES_KEY);
258
+ const authData = dashStorage.getItem(this.AUTH_KEY);
259
+ const tenantImagesData = dashStorage.getItem(this.TENANT_IMAGES_KEY);
260
+ const tenantSettingsData = dashStorage.getItem(this.TENANT_SETTINGS_KEY);
261
+ const user = userData ? JSON.parse(userData) : null;
262
+ const systemValues = systemValuesData ? JSON.parse(systemValuesData) : null;
263
+ const auth = authData ? JSON.parse(authData) : null;
264
+ const tenantImages = tenantImagesData ? JSON.parse(tenantImagesData) : null;
265
+ const tenantSettings = tenantSettingsData ? JSON.parse(tenantSettingsData) : null;
161
266
  return {
162
- token: e,
163
- user: l,
164
- systemValues: i,
165
- auth: u?.auth || null,
166
- tenantImages: S,
167
- tenantSettings: g
267
+ token,
268
+ user,
269
+ systemValues,
270
+ auth: (auth == null ? void 0 : auth.auth) || null,
271
+ tenantImages,
272
+ tenantSettings
168
273
  };
169
- } catch (e) {
170
- return console.error("Failed to get stored auth data:", e), null;
274
+ } catch (error) {
275
+ console.error("Failed to get stored auth data:", error);
276
+ return null;
171
277
  }
172
278
  }
173
279
  static isAuthValid() {
174
280
  return this.getAuth() !== null;
175
281
  }
176
282
  static getPermissions() {
283
+ var _a, _b;
177
284
  try {
178
- const e = r.getItem("roles");
179
- if (e === "guest")
285
+ const storedRoles = dashStorage.getItem("roles");
286
+ if (storedRoles === "guest") {
180
287
  return Promise.resolve("guest");
181
- const t = this.getAuth();
182
- if (t?.auth?.user?.roles) {
183
- const s = {
184
- roles: t.auth.user.roles.map(
185
- (o) => typeof o == "string" ? o : o.name
288
+ }
289
+ const authData = this.getAuth();
290
+ if ((_b = (_a = authData == null ? void 0 : authData.auth) == null ? void 0 : _a.user) == null ? void 0 : _b.roles) {
291
+ const processedPermissions = {
292
+ roles: authData.auth.user.roles.map(
293
+ (item) => typeof item === "string" ? item : item.name
186
294
  )
187
295
  };
188
- return Promise.resolve(s);
296
+ return Promise.resolve(processedPermissions);
189
297
  }
190
- if (e)
298
+ if (storedRoles) {
191
299
  try {
192
- const s = JSON.parse(e), o = {
193
- roles: Array.isArray(s) ? s.map((n) => typeof n == "string" ? n : n.name) : [s]
300
+ const parsedRoles = JSON.parse(storedRoles);
301
+ const processedPermissions = {
302
+ roles: Array.isArray(parsedRoles) ? parsedRoles.map((item) => typeof item === "string" ? item : item.name) : [parsedRoles]
194
303
  };
195
- return Promise.resolve(o);
196
- } catch (s) {
197
- return console.error("Failed to parse roles from localStorage:", s), Promise.resolve("null");
304
+ return Promise.resolve(processedPermissions);
305
+ } catch (parseError) {
306
+ console.error("Failed to parse roles from localStorage:", parseError);
307
+ return Promise.resolve("null");
198
308
  }
309
+ }
310
+ return Promise.resolve("null");
311
+ } catch (error) {
312
+ console.error("Failed to get permissions:", error);
199
313
  return Promise.resolve("null");
200
- } catch (e) {
201
- return console.error("Failed to get permissions:", e), Promise.resolve("null");
202
314
  }
203
315
  }
204
- }
205
- async function d() {
206
- const a = window.electronStore;
207
- if (a && a.getAll)
208
- try {
209
- const t = await a.getAll();
210
- if (t && typeof t == "object") {
211
- for (const [s, o] of Object.entries(t))
212
- window.localStorage.setItem(s, JSON.stringify(o));
213
- console.log("[ElectronStorageSync] Synced electron-store to localStorage");
316
+ };
317
+ __publicField(AuthPersistenceService, "AUTH_KEY", "dashAuth");
318
+ __publicField(AuthPersistenceService, "TIMESTAMP_KEY", "dashAuthTimestamp");
319
+ __publicField(AuthPersistenceService, "TENANT_IMAGES_KEY", "dashTenantImages");
320
+ __publicField(AuthPersistenceService, "TENANT_SETTINGS_KEY", "dashTenantSettings");
321
+ __publicField(AuthPersistenceService, "SYSTEM_VALUES_KEY", "dashSystemValues");
322
+ __publicField(AuthPersistenceService, "EXPIRY_HOURS", 24);
323
+ function syncDeviceStoreToLocalStorage() {
324
+ return __async(this, null, function* () {
325
+ var _a, _b, _c;
326
+ const electronStore = window.electronStore;
327
+ if (electronStore && electronStore.getAll) {
328
+ try {
329
+ const allData = yield electronStore.getAll();
330
+ if (allData && typeof allData === "object") {
331
+ for (const [key, value] of Object.entries(allData)) {
332
+ window.localStorage.setItem(key, JSON.stringify(value));
333
+ }
334
+ console.log("[ElectronStorageSync] Synced electron-store to localStorage");
335
+ }
336
+ return;
337
+ } catch (err) {
338
+ console.error("[ElectronStorageSync] Failed to sync:", err);
214
339
  }
215
- return;
216
- } catch (t) {
217
- console.error("[ElectronStorageSync] Failed to sync:", t);
218
340
  }
219
- const e = window?.Capacitor?.Plugins?.Preferences || window?.Capacitor?.Preferences;
220
- if (e && e.keys && e.get)
221
- try {
222
- const { keys: t } = await e.keys();
223
- for (const s of t) {
224
- const { value: o } = await e.get({ key: s });
225
- o !== null && window.localStorage.setItem(s, o);
341
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
342
+ if (Preferences && Preferences.keys && Preferences.get) {
343
+ try {
344
+ const { keys } = yield Preferences.keys();
345
+ for (const key of keys) {
346
+ const { value } = yield Preferences.get({ key });
347
+ if (value !== null) {
348
+ window.localStorage.setItem(key, value);
349
+ }
350
+ }
351
+ console.log("[CapacitorStorageSync] Synced Preferences to localStorage");
352
+ } catch (err) {
353
+ console.error("[CapacitorStorageSync] Failed to sync:", err);
226
354
  }
227
- console.log("[CapacitorStorageSync] Synced Preferences to localStorage");
228
- } catch (t) {
229
- console.error("[CapacitorStorageSync] Failed to sync:", t);
230
355
  }
356
+ });
231
357
  }
232
- async function y() {
233
- const a = window.electronStore;
234
- if (a && a.set)
235
- try {
236
- for (let t = 0; t < window.localStorage.length; t++) {
237
- const s = window.localStorage.key(t);
238
- if (!s) continue;
239
- const o = window.localStorage.getItem(s);
240
- try {
241
- await a.set(s, JSON.parse(o));
242
- } catch {
243
- await a.set(s, o);
358
+ function syncLocalStorageToDeviceStore() {
359
+ return __async(this, null, function* () {
360
+ var _a, _b, _c;
361
+ const electronStore = window.electronStore;
362
+ if (electronStore && electronStore.set) {
363
+ try {
364
+ for (let i = 0; i < window.localStorage.length; i++) {
365
+ const key = window.localStorage.key(i);
366
+ if (!key) continue;
367
+ const value = window.localStorage.getItem(key);
368
+ try {
369
+ yield electronStore.set(key, JSON.parse(value));
370
+ } catch (e) {
371
+ yield electronStore.set(key, value);
372
+ }
244
373
  }
374
+ console.log("[ElectronStorageSync] Synced localStorage to electron-store");
375
+ return;
376
+ } catch (err) {
377
+ console.error("[ElectronStorageSync] Failed to sync:", err);
245
378
  }
246
- console.log("[ElectronStorageSync] Synced localStorage to electron-store");
247
- return;
248
- } catch (t) {
249
- console.error("[ElectronStorageSync] Failed to sync:", t);
250
379
  }
251
- const e = window?.Capacitor?.Plugins?.Preferences || window?.Capacitor?.Preferences;
252
- if (e && e.set)
253
- try {
254
- for (let t = 0; t < window.localStorage.length; t++) {
255
- const s = window.localStorage.key(t);
256
- if (!s) continue;
257
- const o = window.localStorage.getItem(s);
258
- o !== null && await e.set({ key: s, value: o });
380
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
381
+ if (Preferences && Preferences.set) {
382
+ try {
383
+ for (let i = 0; i < window.localStorage.length; i++) {
384
+ const key = window.localStorage.key(i);
385
+ if (!key) continue;
386
+ const value = window.localStorage.getItem(key);
387
+ if (value !== null) {
388
+ yield Preferences.set({ key, value });
389
+ }
390
+ }
391
+ console.log("[CapacitorStorageSync] Synced localStorage to Preferences");
392
+ } catch (err) {
393
+ console.error("[CapacitorStorageSync] Failed to sync:", err);
259
394
  }
260
- console.log("[CapacitorStorageSync] Synced localStorage to Preferences");
261
- } catch (t) {
262
- console.error("[CapacitorStorageSync] Failed to sync:", t);
263
395
  }
396
+ });
264
397
  }
265
- async function E() {
266
- const a = [
267
- "token",
268
- "user",
269
- "authenticated",
270
- "roles",
271
- "dashAuth",
272
- "dashAuthTimestamp",
273
- "dashSystemValues",
274
- "socketConnectionState",
275
- "SerializedAuthContext",
276
- "tenant_id",
277
- "user_id"
278
- ], e = window.electronStore;
279
- if (e && e.delete && e.getAll)
280
- try {
281
- for (const s of a)
282
- await e.delete(s);
283
- await e.set("authenticated", !1), console.log("[ElectronStorageSync] Cleared auth data from electron-store");
284
- return;
285
- } catch (s) {
286
- console.error("[ElectronStorageSync] Failed to clear auth:", s);
398
+ function clearDeviceStoreAuth() {
399
+ return __async(this, null, function* () {
400
+ var _a, _b, _c;
401
+ const authKeys = [
402
+ "token",
403
+ "user",
404
+ "authenticated",
405
+ "roles",
406
+ "dashAuth",
407
+ "dashAuthTimestamp",
408
+ "dashSystemValues",
409
+ "socketConnectionState",
410
+ "SerializedAuthContext",
411
+ "tenant_id",
412
+ "user_id"
413
+ ];
414
+ const electronStore = window.electronStore;
415
+ if (electronStore && electronStore.delete && electronStore.getAll) {
416
+ try {
417
+ for (const key of authKeys) {
418
+ yield electronStore.delete(key);
419
+ }
420
+ yield electronStore.set("authenticated", false);
421
+ console.log("[ElectronStorageSync] Cleared auth data from electron-store");
422
+ return;
423
+ } catch (err) {
424
+ console.error("[ElectronStorageSync] Failed to clear auth:", err);
425
+ }
287
426
  }
288
- const t = window?.Capacitor?.Plugins?.Preferences || window?.Capacitor?.Preferences;
289
- if (t && t.remove)
290
- try {
291
- for (const s of a)
292
- await t.remove({ key: s });
293
- await t.set({ key: "authenticated", value: "false" }), console.log("[CapacitorStorageSync] Cleared auth data from Preferences");
294
- } catch (s) {
295
- console.error("[CapacitorStorageSync] Failed to clear auth:", s);
427
+ const Preferences = ((_b = (_a = window == null ? void 0 : window.Capacitor) == null ? void 0 : _a.Plugins) == null ? void 0 : _b.Preferences) || ((_c = window == null ? void 0 : window.Capacitor) == null ? void 0 : _c.Preferences);
428
+ if (Preferences && Preferences.remove) {
429
+ try {
430
+ for (const key of authKeys) {
431
+ yield Preferences.remove({ key });
432
+ }
433
+ yield Preferences.set({ key: "authenticated", value: "false" });
434
+ console.log("[CapacitorStorageSync] Cleared auth data from Preferences");
435
+ } catch (err) {
436
+ console.error("[CapacitorStorageSync] Failed to clear auth:", err);
437
+ }
296
438
  }
439
+ });
297
440
  }
298
441
  export {
299
- h as AuthPersistenceService,
300
- E as clearDeviceStoreAuth,
301
- d as syncDeviceStoreToLocalStorage,
302
- y as syncLocalStorageToDeviceStore
442
+ AuthPersistenceService,
443
+ clearDeviceStoreAuth,
444
+ syncDeviceStoreToLocalStorage,
445
+ syncLocalStorageToDeviceStore
303
446
  };
package/package.json CHANGED
@@ -1,52 +1,51 @@
1
1
  {
2
- "name": "@dashadmin/dash-auth",
3
- "version": "1.3.24",
4
- "private": false,
5
- "license": "MIT",
6
- "main": "dist/index.js",
7
- "devDependencies": {
8
- "@commitlint/cli": "latest",
9
- "@commitlint/config-conventional": "latest",
10
- "@types/jest": "latest",
11
- "@types/node": "latest",
12
- "@types/react": "latest",
13
- "@types/react-dom": "latest",
14
- "@typescript-eslint/eslint-plugin": "latest",
15
- "@typescript-eslint/parser": "latest",
16
- "eslint-config-airbnb-typescript": "latest",
17
- "eslint-config-prettier": "latest",
18
- "eslint-plugin-html": "latest",
19
- "eslint-plugin-import": "latest",
20
- "eslint-plugin-jsdoc": "latest",
21
- "eslint-plugin-json": "latest",
22
- "eslint-plugin-prettier": "latest",
23
- "ts-jest": "latest",
24
- "ts-loader": "latest",
25
- "tsconfig-paths": "latest",
26
- "typescript": "latest",
27
- "vite": "^5.2.0",
28
- "rollup": "^4.0.0"
29
- },
30
- "dependencies": {
31
- "@mui/material": "^7.3.10",
32
- "@dashadmin/dash-utils": "1.3.24"
33
- },
34
- "publishConfig": {
35
- "name": "@dashadmin/dash-auth",
36
- "access": "public"
37
- },
38
- "files": [
39
- "dist"
40
- ],
41
- "exports": {
42
- ".": {
43
- "import": "./dist/index.js"
44
- },
45
- "./src/*": {
46
- "import": "./dist/*.js"
47
- }
48
- },
49
- "scripts": {
50
- "build": "rm -rf dist && node ../dash-build/compile.mjs && vite build"
51
- }
52
- }
2
+ "name": "@dashadmin/dash-auth",
3
+ "version": "1.3.26",
4
+ "license": "MIT",
5
+ "main": "dist/index.cjs",
6
+ "devDependencies": {
7
+ "@commitlint/cli": "latest",
8
+ "@commitlint/config-conventional": "latest",
9
+ "@types/jest": "latest",
10
+ "@types/node": "latest",
11
+ "@types/react": "latest",
12
+ "@types/react-dom": "latest",
13
+ "@typescript-eslint/eslint-plugin": "latest",
14
+ "@typescript-eslint/parser": "latest",
15
+ "eslint-config-airbnb-typescript": "latest",
16
+ "eslint-config-prettier": "latest",
17
+ "eslint-plugin-html": "latest",
18
+ "eslint-plugin-import": "latest",
19
+ "eslint-plugin-jsdoc": "latest",
20
+ "eslint-plugin-json": "latest",
21
+ "eslint-plugin-prettier": "latest",
22
+ "ts-jest": "latest",
23
+ "ts-loader": "latest",
24
+ "tsconfig-paths": "latest",
25
+ "typescript": "latest"
26
+ },
27
+ "dependencies": {
28
+ "@mui/material": "^7.3.10",
29
+ "@dashadmin/dash-utils": "workspace:*"
30
+ },
31
+ "module": "dist/index.js",
32
+ "types": "dist/index.d.ts",
33
+ "exports": {
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.cjs"
38
+ }
39
+ },
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "registry": "https://registry.npmjs.org"
43
+ },
44
+ "scripts": {
45
+ "build": "tsup"
46
+ },
47
+ "files": [
48
+ "dist"
49
+ ],
50
+ "type": "module"
51
+ }
@@ -1 +0,0 @@
1
- var d=Object.defineProperty;var h=(a,e,t)=>e in a?d(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var l=(a,e,t)=>h(a,typeof e!="symbol"?e+"":e,t);import{dashStorage as r}from"@dashadmin/dash-utils";class c{static saveAuth(e){try{const t={auth:e.auth};e.auth?.tenantImages?r.setItem(this.TENANT_IMAGES_KEY,JSON.stringify(e.auth.tenantImages)):r.removeItem(this.TENANT_IMAGES_KEY),e.auth?.tenantSettings?r.setItem(this.TENANT_SETTINGS_KEY,JSON.stringify(e.auth.tenantSettings)):r.removeItem(this.TENANT_SETTINGS_KEY),e.systemValues&&r.setItem(this.SYSTEM_VALUES_KEY,JSON.stringify(e.systemValues));const s={...t};delete s._loggedOut,delete s._loggedOutAt,r.setItem(this.AUTH_KEY,JSON.stringify(s)),r.setItem(this.TIMESTAMP_KEY,Date.now().toString())}catch(t){console.error("Failed to save auth data:",t)}}static setAuth(e){try{e.token&&r.setItem("token",e.token),e.user&&r.setItem("user",JSON.stringify(e.user)),e.systemValues&&r.setItem(this.SYSTEM_VALUES_KEY,JSON.stringify(e.systemValues)),r.setItem("authenticated","true"),this.saveAuth({auth:{user:e.user,token:e.token,refreshToken:e.refreshToken},systemValues:e.systemValues}),console.log("Auth data set successfully")}catch(t){console.error("Failed to set auth data:",t)}}static getToken(){try{return r.getItem("token")}catch(e){return console.error("Failed to get token:",e),null}}static getUser(){try{const e=r.getItem("user");return e?JSON.parse(e):null}catch(e){return console.error("Failed to get user data:",e),null}}static getAuth(){try{const e=r.getItem(this.AUTH_KEY),t=r.getItem(this.TIMESTAMP_KEY);if(!e||!t)return null;const s=parseInt(t);if((Date.now()-s)/(1e3*60*60)>this.EXPIRY_HOURS)return this.clearAuth(),null;const i=JSON.parse(e);return i._loggedOut?(console.log("Auth data exists but user is marked as logged out"),null):i}catch(e){return console.error("Failed to retrieve auth data:",e),this.clearAuth(),null}}static markAsLoggedOut(){try{const e=r.getItem(this.AUTH_KEY);if(e){const s={...JSON.parse(e),_loggedOut:!0,_loggedOutAt:Date.now()};r.setItem(this.AUTH_KEY,JSON.stringify(s)),console.log("Auth data marked as logged out but preserved in localStorage")}r.removeItem("token"),r.setItem("authenticated","false"),r.removeItem("user")}catch(e){console.error("Failed to mark auth as logged out:",e)}}static getTenantImages(){try{const e=r.getItem(this.TENANT_IMAGES_KEY);return e?JSON.parse(e):null}catch(e){return console.error("Failed to get tenant images:",e),null}}static setTenantImages(e){try{r.setItem(this.TENANT_IMAGES_KEY,JSON.stringify(e))}catch(t){console.error("Failed to set tenant images:",t)}}static getTenantSettings(){try{const e=r.getItem(this.TENANT_SETTINGS_KEY);return e?JSON.parse(e):null}catch(e){return console.error("Failed to get tenant settings:",e),null}}static setTenantSettings(e){try{r.setItem(this.TENANT_SETTINGS_KEY,JSON.stringify(e))}catch(t){console.error("Failed to set tenant settings:",t)}}static clearTenantSettings(){try{r.removeItem(this.TENANT_SETTINGS_KEY)}catch(e){console.error("Failed to clear tenant settings:",e)}}static clearTenantImages(){try{r.removeItem(this.TENANT_IMAGES_KEY)}catch(e){console.error("Failed to clear tenant images:",e)}}static getSystemValues(){try{const e=r.getItem(this.SYSTEM_VALUES_KEY);return e?JSON.parse(e):null}catch(e){return console.error("Failed to get system values:",e),null}}static setSystemValues(e){try{r.setItem(this.SYSTEM_VALUES_KEY,JSON.stringify(e))}catch(t){console.error("Failed to set system values:",t)}}static getSystemValue(e){try{const t=this.getSystemValues();return t?t[e]:null}catch(t){return console.error(`Failed to get system value for key '${e}':`,t),null}}static getPointOfSales(){return this.getSystemValue("point_of_sales")}static clearAuth(){r.removeItem(this.AUTH_KEY),r.removeItem(this.TIMESTAMP_KEY),r.removeItem("token"),r.removeItem("user"),r.setItem("authenticated","false")}static clearAllAuthData(){r.removeItem(this.AUTH_KEY),r.removeItem(this.TIMESTAMP_KEY),r.removeItem(this.TENANT_IMAGES_KEY),r.removeItem(this.TENANT_SETTINGS_KEY),r.removeItem(this.SYSTEM_VALUES_KEY),r.removeItem("token"),r.removeItem("user"),r.setItem("authenticated","false")}static getStoredAuthData(){try{const e=r.getItem("token"),t=r.getItem("user"),s=r.getItem(this.SYSTEM_VALUES_KEY),o=r.getItem(this.AUTH_KEY),n=r.getItem(this.TENANT_IMAGES_KEY),i=r.getItem(this.TENANT_SETTINGS_KEY),u=t?JSON.parse(t):null,g=s?JSON.parse(s):null,S=o?JSON.parse(o):null,y=n?JSON.parse(n):null,m=i?JSON.parse(i):null;return{token:e,user:u,systemValues:g,auth:S?.auth||null,tenantImages:y,tenantSettings:m}}catch(e){return console.error("Failed to get stored auth data:",e),null}}static isAuthValid(){return this.getAuth()!==null}static getPermissions(){try{const e=r.getItem("roles");if(e==="guest")return Promise.resolve("guest");const t=this.getAuth();if(t?.auth?.user?.roles){const s={roles:t.auth.user.roles.map(o=>typeof o=="string"?o:o.name)};return Promise.resolve(s)}if(e)try{const s=JSON.parse(e),o={roles:Array.isArray(s)?s.map(n=>typeof n=="string"?n:n.name):[s]};return Promise.resolve(o)}catch(s){return console.error("Failed to parse roles from localStorage:",s),Promise.resolve("null")}return Promise.resolve("null")}catch(e){return console.error("Failed to get permissions:",e),Promise.resolve("null")}}}l(c,"AUTH_KEY","dashAuth"),l(c,"TIMESTAMP_KEY","dashAuthTimestamp"),l(c,"TENANT_IMAGES_KEY","dashTenantImages"),l(c,"TENANT_SETTINGS_KEY","dashTenantSettings"),l(c,"SYSTEM_VALUES_KEY","dashSystemValues"),l(c,"EXPIRY_HOURS",24);async function I(){const a=window.electronStore;if(a&&a.getAll)try{const t=await a.getAll();if(t&&typeof t=="object"){for(const[s,o]of Object.entries(t))window.localStorage.setItem(s,JSON.stringify(o));console.log("[ElectronStorageSync] Synced electron-store to localStorage")}return}catch(t){console.error("[ElectronStorageSync] Failed to sync:",t)}const e=window?.Capacitor?.Plugins?.Preferences||window?.Capacitor?.Preferences;if(e&&e.keys&&e.get)try{const{keys:t}=await e.keys();for(const s of t){const{value:o}=await e.get({key:s});o!==null&&window.localStorage.setItem(s,o)}console.log("[CapacitorStorageSync] Synced Preferences to localStorage")}catch(t){console.error("[CapacitorStorageSync] Failed to sync:",t)}}async function f(){const a=window.electronStore;if(a&&a.set)try{for(let t=0;t<window.localStorage.length;t++){const s=window.localStorage.key(t);if(!s)continue;const o=window.localStorage.getItem(s);try{await a.set(s,JSON.parse(o))}catch{await a.set(s,o)}}console.log("[ElectronStorageSync] Synced localStorage to electron-store");return}catch(t){console.error("[ElectronStorageSync] Failed to sync:",t)}const e=window?.Capacitor?.Plugins?.Preferences||window?.Capacitor?.Preferences;if(e&&e.set)try{for(let t=0;t<window.localStorage.length;t++){const s=window.localStorage.key(t);if(!s)continue;const o=window.localStorage.getItem(s);o!==null&&await e.set({key:s,value:o})}console.log("[CapacitorStorageSync] Synced localStorage to Preferences")}catch(t){console.error("[CapacitorStorageSync] Failed to sync:",t)}}async function A(){const a=["token","user","authenticated","roles","dashAuth","dashAuthTimestamp","dashSystemValues","socketConnectionState","SerializedAuthContext","tenant_id","user_id"],e=window.electronStore;if(e&&e.delete&&e.getAll)try{for(const s of a)await e.delete(s);await e.set("authenticated",!1),console.log("[ElectronStorageSync] Cleared auth data from electron-store");return}catch(s){console.error("[ElectronStorageSync] Failed to clear auth:",s)}const t=window?.Capacitor?.Plugins?.Preferences||window?.Capacitor?.Preferences;if(t&&t.remove)try{for(const s of a)await t.remove({key:s});await t.set({key:"authenticated",value:"false"}),console.log("[CapacitorStorageSync] Cleared auth data from Preferences")}catch(s){console.error("[CapacitorStorageSync] Failed to clear auth:",s)}}export{c as AuthPersistenceService,A as clearDeviceStoreAuth,I as syncDeviceStoreToLocalStorage,f as syncLocalStorageToDeviceStore};