varminer-app-header 2.2.0 → 2.2.2

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.js CHANGED
@@ -77,77 +77,38 @@ function getStoredUserDetails() {
77
77
  return null;
78
78
  }
79
79
  }
80
- const getUserDataFromStorage = () => {
81
- // Prefer IAM user details when available (same origin, after verify-OTP)
82
- const iamUser = getStoredUserDetails();
83
- if (iamUser) {
84
- const name = [iamUser.firstName, iamUser.lastName].filter(Boolean).join(" ").trim();
85
- const role = iamUser.roles?.length ? iamUser.roles[0] : iamUser.workInfo?.jobTitle ?? "";
86
- return {
87
- name: name || "",
88
- email: iamUser.email || "",
89
- role: role || "",
90
- avatar: undefined,
91
- initials: name ? name.split(/\s+/).map((s) => s[0]).join("").slice(0, 2).toUpperCase() : undefined,
92
- };
80
+ // --- Header-owned persist store (unique key, no conflict with other apps) ---
81
+ /** Unique localStorage key for this header package. Used only for access token; user details come from IAM (linn-i-am-userDetails). */
82
+ const PERSIST_HEADER_KEY = "persist:varminer-app-header";
83
+ /**
84
+ * Store auth (e.g. accessToken) in the header's own persist key. Call this after login so the header can use the token for profile picture etc.
85
+ * User details are not stored here; they come from IAM (linn-i-am-userDetails).
86
+ */
87
+ function setHeaderAuth(auth) {
88
+ try {
89
+ const payload = auth ? { auth } : {};
90
+ localStorage.setItem(PERSIST_HEADER_KEY, JSON.stringify(payload));
93
91
  }
94
- const userDbString = localStorage.getItem("persist:userdb");
95
- if (userDbString) {
96
- try {
97
- const parsedOuter = JSON.parse(userDbString);
98
- const parseNested = (value) => {
99
- if (typeof value === 'string') {
100
- try {
101
- return JSON.parse(value);
102
- }
103
- catch {
104
- return value;
105
- }
106
- }
107
- return value;
108
- };
109
- let userData = null;
110
- if (parsedOuter.userDetails) {
111
- const parsedUserDetails = parseNested(parsedOuter.userDetails);
112
- userData = parsedUserDetails.user || parsedUserDetails.data || parsedUserDetails;
113
- }
114
- if (!userData && parsedOuter.user) {
115
- const parsedUser = parseNested(parsedOuter.user);
116
- userData = parsedUser.user || parsedUser.data || parsedUser.currentUser || parsedUser;
117
- }
118
- if (!userData && parsedOuter.authDetails) {
119
- const parsedAuthDetails = parseNested(parsedOuter.authDetails);
120
- if (parsedAuthDetails.auth) {
121
- userData = parsedAuthDetails.auth.user || parsedAuthDetails.auth.userData;
122
- }
123
- if (!userData) {
124
- userData = parsedAuthDetails.user || parsedAuthDetails.userData || parsedAuthDetails.currentUser;
125
- }
126
- }
127
- if (!userData && parsedOuter.profile) {
128
- const parsedProfile = parseNested(parsedOuter.profile);
129
- userData = parsedProfile.user || parsedProfile.data || parsedProfile;
130
- }
131
- if (userData) {
132
- let name = userData.name || userData.fullName || userData.userName || "";
133
- if (!name && (userData.firstName || userData.lastName)) {
134
- name = [userData.firstName, userData.lastName].filter(Boolean).join(' ');
135
- }
136
- return {
137
- name: name || "",
138
- email: userData.email || userData.emailAddress || "",
139
- role: userData.role || userData.userRole || userData.designation || userData.title || "",
140
- avatar: userData.avatar || userData.profilePicture || userData.imageUrl || userData.profileImage,
141
- initials: userData.initials,
142
- };
143
- }
144
- }
145
- catch (err) {
146
- console.error("Error parsing user data from localStorage:", err);
147
- return null;
148
- }
92
+ catch (err) {
93
+ console.error("Error setting header auth:", err);
149
94
  }
150
- return null;
95
+ }
96
+ /**
97
+ * User display data: from IAM only (linn-i-am-userDetails). No dependency on persist:userdb.
98
+ */
99
+ const getUserDataFromStorage = () => {
100
+ const iamUser = getStoredUserDetails();
101
+ if (!iamUser)
102
+ return null;
103
+ const name = [iamUser.firstName, iamUser.lastName].filter(Boolean).join(" ").trim();
104
+ const role = iamUser.roles?.length ? iamUser.roles[0] : iamUser.workInfo?.jobTitle ?? "";
105
+ return {
106
+ name: name || "",
107
+ email: iamUser.email || "",
108
+ role: role || "",
109
+ avatar: undefined,
110
+ initials: name ? name.split(/\s+/).map((s) => s[0]).join("").slice(0, 2).toUpperCase() : undefined,
111
+ };
151
112
  };
152
113
  const getNotificationCountFromStorage = () => {
153
114
  const userDbString = localStorage.getItem("persist:userdb");
@@ -263,26 +224,49 @@ const decodeJWT = (token) => {
263
224
  return null;
264
225
  }
265
226
  };
227
+ const emptyStorageResult = () => ({
228
+ auth: null,
229
+ user: null,
230
+ authDetails: null,
231
+ userDetails: null,
232
+ profile: null,
233
+ notifications: null,
234
+ messages: null,
235
+ app: null,
236
+ decodedToken: null,
237
+ rawData: null,
238
+ });
266
239
  /**
267
- * Get all data from localStorage (persist:userdb) including decoded accessToken
268
- * This function reads all available data without hardcoding and doesn't require any parameters
269
- * @returns Comprehensive object containing all available data from localStorage and decoded JWT payload
240
+ * Get auth (and decoded token) for header use. Reads from header's own persist key first; falls back to persist:userdb only for access token.
241
+ * User details are not read from userdb—use getStoredUserDetails / getUserDataFromStorage (IAM) for that.
242
+ * @returns Object with auth, decodedToken, and other keys (user/userDetails/profile null when using header key)
270
243
  */
271
244
  const getAllDataFromStorage = () => {
245
+ // Prefer header-owned persist key (unique, no conflict with other apps)
246
+ const headerStr = localStorage.getItem(PERSIST_HEADER_KEY);
247
+ if (headerStr) {
248
+ try {
249
+ const parsed = JSON.parse(headerStr);
250
+ const auth = parsed?.auth;
251
+ const accessToken = auth?.accessToken;
252
+ if (accessToken) {
253
+ const decodedToken = decodeJWT(accessToken);
254
+ return {
255
+ ...emptyStorageResult(),
256
+ auth: auth,
257
+ decodedToken: decodedToken ? Object.fromEntries(Object.entries(decodedToken).map(([k, v]) => [k, v ?? null])) : null,
258
+ rawData: parsed,
259
+ };
260
+ }
261
+ }
262
+ catch (err) {
263
+ console.error("Error parsing header persist:", err);
264
+ }
265
+ }
266
+ // Fallback: read access token from persist:userdb (e.g. legacy or shared auth)
272
267
  const userDbString = localStorage.getItem("persist:userdb");
273
268
  if (!userDbString) {
274
- return {
275
- auth: null,
276
- user: null,
277
- authDetails: null,
278
- userDetails: null,
279
- profile: null,
280
- notifications: null,
281
- messages: null,
282
- app: null,
283
- decodedToken: null,
284
- rawData: null,
285
- };
269
+ return emptyStorageResult();
286
270
  }
287
271
  try {
288
272
  const parsedOuter = JSON.parse(userDbString);
@@ -360,19 +344,7 @@ const getAllDataFromStorage = () => {
360
344
  }
361
345
  catch (err) {
362
346
  console.error("Error parsing all data from localStorage:", err);
363
- return {
364
- auth: null,
365
- user: null,
366
- authDetails: null,
367
- userDetails: null,
368
- profile: null,
369
- notifications: null,
370
- messages: null,
371
- app: null,
372
- decodedToken: null,
373
- rawData: null,
374
- error: err instanceof Error ? err.message : 'Unknown error',
375
- };
347
+ return { ...emptyStorageResult(), error: err instanceof Error ? err.message : "Unknown error" };
376
348
  }
377
349
  };
378
350
  /**
@@ -514,13 +486,19 @@ const fetchProfilePictureAsBlobUrl = async (baseUrl = "http://objectstore.impact
514
486
  // Generate message ID and correlation ID if not provided
515
487
  const msgId = messageId || `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
516
488
  const corrId = correlationId || `corr-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
489
+ const allData = getAllDataFromStorage();
490
+ const accessToken = allData.auth?.accessToken;
491
+ const headers = {
492
+ 'X-Message-Id': msgId,
493
+ 'X-Correlation-Id': corrId,
494
+ };
495
+ if (accessToken) {
496
+ headers['Authorization'] = `Bearer ${accessToken}`;
497
+ }
517
498
  // Step 1: Fetch the profile picture path from API (returns JSON with filePath)
518
499
  const apiResponse = await fetch(profilePictureUrl, {
519
500
  method: 'GET',
520
- headers: {
521
- 'X-Message-Id': msgId,
522
- 'X-Correlation-Id': corrId,
523
- },
501
+ headers,
524
502
  });
525
503
  // Check if the API response is successful
526
504
  if (!apiResponse.ok) {
@@ -1149,6 +1127,7 @@ const AppHeader = ({ language: languageProp }) => {
1149
1127
 
1150
1128
  exports.AppHeader = AppHeader;
1151
1129
  exports.DrawerProvider = DrawerProvider;
1130
+ exports.PERSIST_HEADER_KEY = PERSIST_HEADER_KEY;
1152
1131
  exports.USER_DETAILS_STORAGE_KEY = USER_DETAILS_STORAGE_KEY;
1153
1132
  exports.fetchProfilePictureAsBlobUrl = fetchProfilePictureAsBlobUrl;
1154
1133
  exports.getAllDataFromStorage = getAllDataFromStorage;
@@ -1159,6 +1138,7 @@ exports.getProfilePictureUrl = getProfilePictureUrl;
1159
1138
  exports.getStoredUserDetails = getStoredUserDetails;
1160
1139
  exports.getTranslations = getTranslations;
1161
1140
  exports.getUserDataFromStorage = getUserDataFromStorage;
1141
+ exports.setHeaderAuth = setHeaderAuth;
1162
1142
  exports.setI18nLocaleToStorage = setI18nLocaleToStorage;
1163
1143
  exports.translations = translations;
1164
1144
  exports.useDrawer = useDrawer;