@dashadmin/dash-auth 1.3.25 → 1.3.28
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/README.md +35 -1
- package/dist/index.cjs +473 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +340 -197
- package/package.json +50 -51
- package/dist/AuthPersistanceService.js +0 -1
package/README.md
CHANGED
|
@@ -1 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dashadmin/dash-auth
|
|
2
|
+
|
|
3
|
+
> Authentication utilities and auth provider for Dash.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Token handling, session helpers and the auth provider used by the admin to authenticate requests and guard resources.
|
|
8
|
+
|
|
9
|
+
## Key exports & features
|
|
10
|
+
|
|
11
|
+
- Auth provider
|
|
12
|
+
- Token/session helpers
|
|
13
|
+
- Login state utilities
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# From the public npm registry
|
|
19
|
+
npm install @dashadmin/dash-auth
|
|
20
|
+
|
|
21
|
+
# Or the local Verdaccio registry (unscoped)
|
|
22
|
+
npm install dash-auth --registry http://localhost:4873
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Internal Dash dependencies
|
|
26
|
+
|
|
27
|
+
`@dashadmin/dash-utils`
|
|
28
|
+
|
|
29
|
+
## Development & publishing
|
|
30
|
+
|
|
31
|
+
This package is part of the **dash-frontend-core** monorepo. See [`DEVELOPMENT.md`](../../DEVELOPMENT.md) for the source-modification workflow, local Verdaccio publishing, npm publishing and version-bump policy.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
_Part of the [Dash Framework](https://www.npmjs.com/org/dashadmin) · `@dashadmin` scope._
|
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
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -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.d.ts
ADDED
|
@@ -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 };
|