@dashadmin/dash-auth 1.0.0

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 ADDED
@@ -0,0 +1 @@
1
+ # INSTRUCTIONS
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Auto-generated permissive types for @dashadmin/dash-auth.
2
+ // Full typings can be regenerated with a tolerant tsc emit.
3
+ export {};
package/index.js ADDED
@@ -0,0 +1,303 @@
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) {
10
+ try {
11
+ const t = {
12
+ auth: e.auth
13
+ // Only save auth.auth, not auth.user
14
+ };
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);
20
+ }
21
+ }
22
+ static setAuth(e) {
23
+ 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({
25
+ auth: {
26
+ user: e.user,
27
+ token: e.token,
28
+ refreshToken: e.refreshToken
29
+ // Include any other auth-related data
30
+ },
31
+ systemValues: e.systemValues
32
+ }), console.log("Auth data set successfully");
33
+ } catch (t) {
34
+ console.error("Failed to set auth data:", t);
35
+ }
36
+ }
37
+ static getToken() {
38
+ try {
39
+ return r.getItem("token");
40
+ } catch (e) {
41
+ return console.error("Failed to get token:", e), null;
42
+ }
43
+ }
44
+ static getUser() {
45
+ 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;
50
+ }
51
+ }
52
+ static getAuth() {
53
+ try {
54
+ const e = r.getItem(this.AUTH_KEY), t = r.getItem(this.TIMESTAMP_KEY);
55
+ if (!e || !t)
56
+ 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;
64
+ }
65
+ }
66
+ static markAsLoggedOut() {
67
+ try {
68
+ const e = r.getItem(this.AUTH_KEY);
69
+ if (e) {
70
+ const s = {
71
+ ...JSON.parse(e),
72
+ _loggedOut: !0,
73
+ _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");
76
+ }
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);
80
+ }
81
+ }
82
+ static getTenantImages() {
83
+ 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;
88
+ }
89
+ }
90
+ static setTenantImages(e) {
91
+ try {
92
+ r.setItem(this.TENANT_IMAGES_KEY, JSON.stringify(e));
93
+ } catch (t) {
94
+ console.error("Failed to set tenant images:", t);
95
+ }
96
+ }
97
+ static getTenantSettings() {
98
+ 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;
103
+ }
104
+ }
105
+ static setTenantSettings(e) {
106
+ try {
107
+ r.setItem(this.TENANT_SETTINGS_KEY, JSON.stringify(e));
108
+ } catch (t) {
109
+ console.error("Failed to set tenant settings:", t);
110
+ }
111
+ }
112
+ static clearTenantSettings() {
113
+ try {
114
+ r.removeItem(this.TENANT_SETTINGS_KEY);
115
+ } catch (e) {
116
+ console.error("Failed to clear tenant settings:", e);
117
+ }
118
+ }
119
+ static clearTenantImages() {
120
+ try {
121
+ r.removeItem(this.TENANT_IMAGES_KEY);
122
+ } catch (e) {
123
+ console.error("Failed to clear tenant images:", e);
124
+ }
125
+ }
126
+ static getSystemValues() {
127
+ 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;
132
+ }
133
+ }
134
+ static setSystemValues(e) {
135
+ try {
136
+ r.setItem(this.SYSTEM_VALUES_KEY, JSON.stringify(e));
137
+ } catch (t) {
138
+ console.error("Failed to set system values:", t);
139
+ }
140
+ }
141
+ static getSystemValue(e) {
142
+ 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;
147
+ }
148
+ }
149
+ static getPointOfSales() {
150
+ return this.getSystemValue("point_of_sales");
151
+ }
152
+ static clearAuth() {
153
+ r.removeItem(this.AUTH_KEY), r.removeItem(this.TIMESTAMP_KEY), r.removeItem("token"), r.removeItem("user"), r.setItem("authenticated", "false");
154
+ }
155
+ 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");
157
+ }
158
+ static getStoredAuthData() {
159
+ 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;
161
+ return {
162
+ token: e,
163
+ user: l,
164
+ systemValues: i,
165
+ auth: u?.auth || null,
166
+ tenantImages: S,
167
+ tenantSettings: g
168
+ };
169
+ } catch (e) {
170
+ return console.error("Failed to get stored auth data:", e), null;
171
+ }
172
+ }
173
+ static isAuthValid() {
174
+ return this.getAuth() !== null;
175
+ }
176
+ static getPermissions() {
177
+ try {
178
+ const e = r.getItem("roles");
179
+ if (e === "guest")
180
+ 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
186
+ )
187
+ };
188
+ return Promise.resolve(s);
189
+ }
190
+ if (e)
191
+ try {
192
+ const s = JSON.parse(e), o = {
193
+ roles: Array.isArray(s) ? s.map((n) => typeof n == "string" ? n : n.name) : [s]
194
+ };
195
+ return Promise.resolve(o);
196
+ } catch (s) {
197
+ return console.error("Failed to parse roles from localStorage:", s), Promise.resolve("null");
198
+ }
199
+ return Promise.resolve("null");
200
+ } catch (e) {
201
+ return console.error("Failed to get permissions:", e), Promise.resolve("null");
202
+ }
203
+ }
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");
214
+ }
215
+ return;
216
+ } catch (t) {
217
+ console.error("[ElectronStorageSync] Failed to sync:", t);
218
+ }
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);
226
+ }
227
+ console.log("[CapacitorStorageSync] Synced Preferences to localStorage");
228
+ } catch (t) {
229
+ console.error("[CapacitorStorageSync] Failed to sync:", t);
230
+ }
231
+ }
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);
244
+ }
245
+ }
246
+ console.log("[ElectronStorageSync] Synced localStorage to electron-store");
247
+ return;
248
+ } catch (t) {
249
+ console.error("[ElectronStorageSync] Failed to sync:", t);
250
+ }
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 });
259
+ }
260
+ console.log("[CapacitorStorageSync] Synced localStorage to Preferences");
261
+ } catch (t) {
262
+ console.error("[CapacitorStorageSync] Failed to sync:", t);
263
+ }
264
+ }
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);
287
+ }
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);
296
+ }
297
+ }
298
+ export {
299
+ h as AuthPersistenceService,
300
+ E as clearDeviceStoreAuth,
301
+ d as syncDeviceStoreToLocalStorage,
302
+ y as syncLocalStorageToDeviceStore
303
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@dashadmin/dash-auth",
3
+ "version": "1.0.0",
4
+ "description": "dash-auth — DASH framework package",
5
+ "license": "MIT",
6
+ "author": "Francisco Aranda <farandal@gmail.com>",
7
+ "type": "module",
8
+ "main": "index.js",
9
+ "module": "index.js",
10
+ "types": "index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./index.d.ts",
14
+ "import": "./index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "**/*"
19
+ ],
20
+ "sideEffects": false,
21
+ "dependencies": {
22
+ "@mui/material": "^7.3.10",
23
+ "@dashadmin/dash-utils": "^1.0.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "engines": {
29
+ "node": ">=18"
30
+ }
31
+ }