Skip to content

Commit fc64474

Browse files
authored
Merge pull request #1116 from Bonymol-aot/FWF-5971/PR-comments-resolve
Code optimized
2 parents 52bdd80 + 9bcb223 commit fc64474

File tree

1 file changed

+34
-33
lines changed
  • forms-flow-nav/src/services/user

1 file changed

+34
-33
lines changed

forms-flow-nav/src/services/user/index.js

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,60 @@ import { RequestService,StorageService } from "@formsflow/service";
22
import API from "../../endpoints/index";
33
import { WEB_BASE_CUSTOM_URL } from "../../constants/constants";
44

5-
const extractUserIdFromToken = (token) => {
6-
if (!token) return "";
7-
const raw = String(token).replace(/^Bearer\s+/i, "").trim();
8-
const parts = raw.split(".");
9-
if (parts.length < 2) return "";
10-
try {
11-
const base64Url = parts[1];
12-
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
13-
const padded = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), "=");
14-
const atobFn = typeof atob === "function" ? atob : (typeof window !== "undefined" ? window.atob : null);
15-
if (!atobFn) return "";
16-
const json = JSON.parse(atobFn(padded));
17-
return json?.sub || "";
18-
} catch (e) {
19-
return "";
20-
}
21-
};
22-
export const fetchUserLoginDetails = () => {
23-
const userId = extractUserIdFromToken(StorageService.get(StorageService.User.AUTH_TOKEN));
24-
if (!userId) return;
25-
const apiUserLoginDetails = API.USER_LOGIN_DETAILS(userId);
26-
RequestService.httpGETRequest(apiUserLoginDetails, null, StorageService.get(StorageService.User.AUTH_TOKEN))
5+
const getUserId = () => {
6+
try {
7+
const userDetail =
8+
JSON.parse(StorageService.get(StorageService.User.USER_DETAILS)) || {};
9+
return userDetail?.sub || "";
10+
} catch {
11+
return "";
12+
}
13+
};
14+
15+
export const fetchUserLoginDetails = () => {
16+
const userId = getUserId();
17+
if (!userId) return;
18+
19+
const apiUserLoginDetails = API.USER_LOGIN_DETAILS(userId);
20+
21+
return RequestService.httpGETRequest(
22+
apiUserLoginDetails,
23+
null,
24+
StorageService.get(StorageService.User.AUTH_TOKEN)
25+
)
2726
.then((res) => {
28-
localStorage.setItem("USER_LOGIN_DETAILS", JSON.stringify(res.data));
27+
localStorage.setItem("USER_LOGIN_DETAILS", JSON.stringify(res.data));
2928
})
3029
.catch((error) => {
3130
console.error(error);
3231
});
3332
};
3433

34+
3535
/**
3636
* Trigger a reset password email/link for the current user.
3737
*/
3838

3939
export const requestResetPassword = () => {
40+
const userId = getUserId();
4041
const token = StorageService.get(StorageService.User.AUTH_TOKEN);
41-
const userId = extractUserIdFromToken(token);
42+
4243
if (!userId) {
43-
return Promise.reject(new Error("User id not found in token"));
44+
return Promise.reject(new Error("User id not found in user details"));
4445
}
46+
47+
4548
const redirectUri =
4649
(WEB_BASE_CUSTOM_URL && String(WEB_BASE_CUSTOM_URL).trim()) ||
4750
(typeof window !== "undefined" ? window.location.origin : "");
48-
const urlWithRedirect = `${API.RESET_PASSWORD(userId)}?redirect_uri=${encodeURIComponent(
49-
redirectUri
50-
)}`;
51-
return RequestService.httpPUTRequest(
52-
urlWithRedirect,
53-
{},
54-
token
55-
);
51+
52+
const urlWithRedirect =
53+
`${API.RESET_PASSWORD(userId)}?redirect_uri=${encodeURIComponent(redirectUri)}`;
54+
55+
return RequestService.httpPUTRequest(urlWithRedirect, {}, token);
5656
};
5757

58+
5859
/**
5960
* @param {string} userId
6061
* @param {Object} profileData

0 commit comments

Comments
 (0)