Skip to content

Commit dfc6358

Browse files
authored
[staff] Suraface error as alert & send token via header (#4033)
## Description ## Tests
2 parents ce61965 + 9f95678 commit dfc6358

File tree

10 files changed

+617
-62
lines changed

10 files changed

+617
-62
lines changed

infra/staff/src/App.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ const App: React.FC = () => {
119119
const startTime = Date.now();
120120
try {
121121
const encodedEmail = encodeURIComponent(email);
122-
const encodedToken = encodeURIComponent(token);
123-
const url = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
124-
console.log(`Fetching data from URL: ${url}`);
125-
const response = await fetch(url);
122+
123+
const url = `${apiOrigin}/admin/user?email=${encodedEmail}`;
124+
const response = await fetch(url, {
125+
headers: {
126+
"Content-Type": "application/json",
127+
"X-AUTH-TOKEN": token,
128+
},
129+
});
126130
if (!response.ok) {
127131
throw new Error("Network response was not ok");
128132
}

infra/staff/src/components/ChangeEmail.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@ const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
3131

3232
useEffect(() => {
3333
const fetchUserID = async () => {
34-
const token = getToken();
3534
const email = getEmail();
3635
setNewEmail(email); // Set initial email state
3736

3837
const encodedEmail = encodeURIComponent(email);
39-
const encodedToken = encodeURIComponent(token);
40-
const url = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
4138

39+
const url = `${apiOrigin}/admin/user?email=${encodedEmail}`;
4240
try {
4341
const response = await fetch(url, {
4442
method: "GET",
4543
headers: {
4644
"Content-Type": "application/json",
47-
"X-AUTH-TOKEN": token,
45+
"X-AUTH-TOKEN": getToken(),
4846
},
4947
});
5048

@@ -78,7 +76,7 @@ const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
7876
event.preventDefault();
7977

8078
const token = getToken();
81-
const url = `${apiOrigin}/admin/user/change-email?token=${token}`;
79+
const url = `${apiOrigin}/admin/user/change-email`;
8280

8381
const body = {
8482
userID,

infra/staff/src/components/CloseFamily.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ const CloseFamily: React.FC<CloseFamilyProps> = ({
4747
}
4848

4949
const encodedEmail = encodeURIComponent(email);
50-
const encodedToken = encodeURIComponent(token);
5150

5251
// Fetch user data
53-
const userUrl = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
54-
const userResponse = await fetch(userUrl);
52+
const userUrl = `${apiOrigin}/admin/user?email=${encodedEmail}`;
53+
const userResponse = await fetch(userUrl, {
54+
method: "GET",
55+
headers: {
56+
"Content-Type": "application/json",
57+
"X-Auth-Token": token,
58+
},
59+
});
5560
if (!userResponse.ok) {
5661
throw new Error("Failed to fetch user data");
5762
}
@@ -63,11 +68,14 @@ const CloseFamily: React.FC<CloseFamilyProps> = ({
6368
}
6469

6570
// Close family action
66-
const closeFamilyUrl = `${apiOrigin}/admin/user/close-family?token=${encodedToken}`;
71+
const closeFamilyUrl = `${apiOrigin}/admin/user/close-family`;
6772
const body = JSON.stringify({ userId });
6873
const closeFamilyResponse = await fetch(closeFamilyUrl, {
6974
method: "POST",
70-
headers: { "Content-Type": "application/json" },
75+
headers: {
76+
"Content-Type": "application/json",
77+
"X-Auth-Token": token,
78+
},
7179
body: body,
7280
});
7381

@@ -78,9 +86,12 @@ const CloseFamily: React.FC<CloseFamilyProps> = ({
7886

7987
handleCloseFamily(); // Notify parent component of successful action
8088
handleClose(); // Close dialog on successful action
81-
console.log("Family closed successfully");
8289
} catch (error) {
83-
console.error("Error closing family:", error);
90+
if (error instanceof Error) {
91+
alert(error.message);
92+
} else {
93+
alert("Failed to close family");
94+
}
8495
} finally {
8596
setLoading(false);
8697
}

infra/staff/src/components/DeleteAccont.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ const DeleteAccount: React.FC<DeleteAccountProps> = ({ open, handleClose }) => {
2121
try {
2222
const encodedEmail = encodeURIComponent(getEmail());
2323
console.log(encodedEmail);
24-
const encodedToken = encodeURIComponent(getToken());
25-
console.log(encodedToken);
26-
const deleteUrl = `${apiOrigin}/admin/user/delete?email=${encodedEmail}&token=${encodedToken}`;
27-
const response = await fetch(deleteUrl, { method: "DELETE" });
24+
const token = getToken();
25+
26+
const deleteUrl = `${apiOrigin}/admin/user/delete?email=${encodedEmail}`;
27+
const response = await fetch(deleteUrl, {
28+
method: "DELETE",
29+
headers: { "X-Auth-Token": token },
30+
});
2831
if (!response.ok) {
2932
throw new Error("Failed to delete user account");
3033
}
3134
handleClose(); // Close dialog on successful delete
3235
console.log("Account deleted successfully");
3336
} catch (error) {
34-
console.error("Error deleting user account:", error);
37+
if (error instanceof Error) {
38+
alert("Failed to delete the account: " + error.message);
39+
} else {
40+
alert("An error occurred while deleting the account");
41+
}
3542
}
3643
};
3744

infra/staff/src/components/Disable2FA.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ const Disable2FA: React.FC<Disable2FAProps> = ({
4747
}
4848

4949
const encodedEmail = encodeURIComponent(email);
50-
const encodedToken = encodeURIComponent(token);
5150

5251
// Fetch user data
5352
const userUrl = `${apiOrigin}/admin/user?email=${encodedEmail}`;
5453
const userResponse = await fetch(userUrl, {
5554
method: "GET",
5655
headers: {
5756
"Content-Type": "application/json",
58-
"X-Auth-Token": encodedToken,
57+
"X-Auth-Token": token,
5958
},
6059
});
6160
if (!userResponse.ok) {
@@ -75,7 +74,7 @@ const Disable2FA: React.FC<Disable2FAProps> = ({
7574
method: "POST",
7675
headers: {
7776
"Content-Type": "application/json",
78-
"X-Auth-Token": encodedToken,
77+
"X-Auth-Token": token,
7978
},
8079
body: body,
8180
});
@@ -84,12 +83,15 @@ const Disable2FA: React.FC<Disable2FAProps> = ({
8483
const errorResponse = await disableResponse.text();
8584
throw new Error(`Failed to disable 2FA: ${errorResponse}`);
8685
}
87-
8886
handleDisable2FA(); // Notify parent component of successful disable
8987
handleClose(); // Close dialog on successful disable
9088
console.log("2FA disabled successfully");
9189
} catch (error) {
92-
console.error("Error disabling 2FA:", error);
90+
if (error instanceof Error) {
91+
alert(error.message);
92+
} else {
93+
alert("Failed to disable 2FA");
94+
}
9395
} finally {
9496
setLoading(false);
9597
}

infra/staff/src/components/DisablePasskeys.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
4747
}
4848

4949
const encodedEmail = encodeURIComponent(email);
50-
const encodedToken = encodeURIComponent(token);
5150

5251
// Fetch user data
53-
const userUrl = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
54-
const userResponse = await fetch(userUrl);
52+
const userUrl = `${apiOrigin}/admin/user?email=${encodedEmail}`;
53+
const userResponse = await fetch(userUrl, {
54+
method: "GET",
55+
headers: {
56+
"Content-Type": "application/json",
57+
"X-Auth-Token": token,
58+
},
59+
});
5560
if (!userResponse.ok) {
5661
throw new Error("Failed to fetch user data");
5762
}
@@ -63,11 +68,14 @@ const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
6368
}
6469

6570
// Disable passkeys action
66-
const disablePasskeysUrl = `${apiOrigin}/admin/user/disable-passkeys?token=${encodedToken}`;
71+
const disablePasskeysUrl = `${apiOrigin}/admin/user/disable-passkeys`;
6772
const body = JSON.stringify({ userId });
6873
const disablePasskeysResponse = await fetch(disablePasskeysUrl, {
6974
method: "POST",
70-
headers: { "Content-Type": "application/json" },
75+
headers: {
76+
"Content-Type": "application/json",
77+
"X-Auth-Token": token,
78+
},
7179
body: body,
7280
});
7381

@@ -80,7 +88,11 @@ const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
8088
handleClose(); // Close dialog on successful action
8189
console.log("Passkeys disabled successfully");
8290
} catch (error) {
83-
console.error("Error disabling passkeys:", error);
91+
if (error instanceof Error) {
92+
alert(error.message);
93+
} else {
94+
alert("Failed to disable passkeys");
95+
}
8496
} finally {
8597
setLoading(false);
8698
}

infra/staff/src/components/FamilyComponentTable.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ const FamilyTableComponent: React.FC = () => {
4040
const fetchData = async () => {
4141
try {
4242
const encodedEmail = encodeURIComponent(getEmail());
43-
const encodedToken = encodeURIComponent(getToken());
44-
const url = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
45-
const response = await fetch(url);
43+
const token = getToken();
44+
const url = `${apiOrigin}/admin/user?email=${encodedEmail}`;
45+
const response = await fetch(url, {
46+
method: "GET",
47+
headers: {
48+
"Content-Type": "application/json",
49+
"X-Auth-Token": token,
50+
},
51+
});
4652
if (!response.ok) {
4753
throw new Error("Network response was not ok");
4854
}

infra/staff/src/components/StorageBonusTableComponent.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ const StorageBonusTableComponent: React.FC = () => {
3737
const fetchData = async () => {
3838
try {
3939
const encodedEmail = encodeURIComponent(getEmail());
40-
const encodedToken = encodeURIComponent(getToken());
41-
const url = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
42-
const response = await fetch(url);
40+
const token = getToken();
41+
const url = `${apiOrigin}/admin/user?email=${encodedEmail}`;
42+
const response = await fetch(url, {
43+
method: "GET",
44+
headers: {
45+
"Content-Type": "application/json",
46+
"X-Auth-Token": token,
47+
},
48+
});
4349
if (!response.ok) {
4450
throw new Error("Failed to fetch bonus data");
4551
}

infra/staff/src/components/UpdateSubscription.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
6363
const email = getEmail();
6464
const token = getToken();
6565
const encodedEmail = encodeURIComponent(email);
66-
const encodedToken = encodeURIComponent(token);
67-
const url = `${apiOrigin}/admin/user?email=${encodedEmail}&token=${encodedToken}`;
68-
69-
const response = await fetch(url);
66+
const url = `${apiOrigin}/admin/user?email=${encodedEmail}`;
67+
const response = await fetch(url, {
68+
headers: {
69+
"X-AUTH-TOKEN": token,
70+
},
71+
});
7072
if (!response.ok) {
7173
throw new Error("Network response was not ok");
7274
}
@@ -172,7 +174,11 @@ const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
172174
console.log("Subscription updated successfully");
173175
onClose();
174176
} catch (error) {
175-
console.error("Error updating subscription:", error);
177+
if (error instanceof Error) {
178+
alert(`Failed to update subscription: ${error.message}`);
179+
} else {
180+
alert("Failed to update subscription");
181+
}
176182
}
177183
})().catch((error: unknown) => {
178184
console.error("Unhandled promise rejection:", error);

0 commit comments

Comments
 (0)