feat(webui): rename account #616
13
src/controllers/custom/renameAccountController.ts
Normal file
13
src/controllers/custom/renameAccountController.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getAccountForRequest } from "@/src/services/loginService";
|
||||
|
||||
export const renameAccountController: RequestHandler = async (req, res) => {
|
||||
|
||||
const account = await getAccountForRequest(req);
|
||||
![]() ⚠️ Potential issue Add null-check for the account.
📝 Committable suggestion
_:warning: Potential issue_
**Add null-check for the account.**
If the user is unauthenticated or the account can't be retrieved, account may be null. Add a guard to ensure you don't attempt to modify properties of a null object.
```diff
- const account = await getAccountForRequest(req);
+ const account = await getAccountForRequest(req);
+ if (!account) {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
```
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
const account = await getAccountForRequest(req);
if (!account) {
return res.status(401).json({ error: "Unauthorized" });
}
`````
</details>
<!-- suggestion_end -->
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
if (typeof req.query.newname == "string") {
|
||||
account.DisplayName = req.query.newname;
|
||||
await account.save();
|
||||
res.end();
|
||||
} else {
|
||||
res.status(400).end();
|
||||
}
|
||||
};
|
@ -5,6 +5,7 @@ import { getItemListsController } from "@/src/controllers/custom/getItemListsCon
|
||||
import { pushArchonCrystalUpgradeController } from "@/src/controllers/custom/pushArchonCrystalUpgradeController";
|
||||
import { popArchonCrystalUpgradeController } from "@/src/controllers/custom/popArchonCrystalUpgradeController";
|
||||
import { deleteAccountController } from "@/src/controllers/custom/deleteAccountController";
|
||||
import { renameAccountController } from "@/src/controllers/custom/renameAccountController";
|
||||
|
||||
import { createAccountController } from "@/src/controllers/custom/createAccountController";
|
||||
import { addItemController } from "@/src/controllers/custom/addItemController";
|
||||
@ -19,6 +20,7 @@ customRouter.get("/getItemLists", getItemListsController);
|
||||
customRouter.get("/pushArchonCrystalUpgrade", pushArchonCrystalUpgradeController);
|
||||
customRouter.get("/popArchonCrystalUpgrade", popArchonCrystalUpgradeController);
|
||||
customRouter.get("/deleteAccount", deleteAccountController);
|
||||
customRouter.get("/renameAccount", renameAccountController);
|
||||
|
||||
customRouter.post("/createAccount", createAccountController);
|
||||
customRouter.post("/addItem", addItemController);
|
||||
|
@ -39,7 +39,8 @@
|
||||
<ul class="dropdown-menu dropdown-menu-end">
|
||||
<li><a class="dropdown-item" href="/webui/" onclick="logout();">Logout</a></li>
|
||||
<li><hr class="dropdown-divider"></li>
|
||||
<li><a class="dropdown-item" onclick="deleteAccount();">Delete Account</a></li>
|
||||
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();renameAccount();">Rename Account</a></li>
|
||||
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();deleteAccount();">Delete Account</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
@ -61,6 +61,15 @@ function logout() {
|
||||
localStorage.removeItem("password");
|
||||
}
|
||||
|
||||
function renameAccount() {
|
||||
const newname = window.prompt("What would you like to change your account name to?");
|
||||
if (newname) {
|
||||
fetch("/custom/renameAccount?" + window.authz + "&newname=" + newname).then(() => {
|
||||
$(".displayname").text(newname);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function deleteAccount() {
|
||||
if (
|
||||
window.confirm(
|
||||
@ -71,7 +80,7 @@ function deleteAccount() {
|
||||
")? This action cannot be undone."
|
||||
)
|
||||
) {
|
||||
fetch("/custom/deleteAccount?" + window.authz).then(function () {
|
||||
fetch("/custom/deleteAccount?" + window.authz).then(() => {
|
||||
logout();
|
||||
single.loadRoute("/webui/"); // Show login screen
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user
💡 Codebase verification
Add error handling for getAccountForRequest to maintain consistency
The
getAccountForRequest
function inloginService.ts
already has robust error handling by throwing errors for invalid parameters. However, whileinventoryController
properly handles these errors with try-catch and returns a 400 status,renameAccountController
lacks similar error handling. This inconsistency could lead to unhandled promise rejections and poor user experience.renameAccountController.ts
to handle potential errors fromgetAccountForRequest
:🔗 Analysis chain
Implement robust error handling for account retrieval.
Currently, if getAccountForRequest fails or returns null, the code doesn't handle that scenario gracefully. Consider verifying that the account object is valid before proceeding.
Let me analyze the implementation of
getAccountForRequest
and its usage inrenameAccountController
. From the results, I can see that:getAccountForRequest
already has built-in error handling:accountId
is missingnonce
is missing or invalidIn
renameAccountController
, the function is used without try-catch, but ininventoryController
, it's properly wrapped in try-catch with a 400 response.Let me verify if there are any other usages or error handling patterns.
Let me try a different pattern to catch all error handling patterns around getAccountForRequest.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 2514
Script:
Length of output: 92
Script:
Length of output: 2843