Skip to content

Commit

Permalink
feat(AdminUserPanel): add reset auth option (fixes #410)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Sep 13, 2023
1 parent 003b29b commit b6ab511
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apps/server/src/api/admin/reset-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Response, Request } from "express";
import { Auth } from "../../lib/Auth.js";
import type { Middleware, RequestMethods } from "../../lib/types.js";
import type Server from "../../Server.js";

export default async function handler(server: Server, req: Request, res: Response) {
try {
const { domain: domainName } = req.body;
if (typeof domainName !== "string") {
res.status(400).send({ message: "Invalid domain provided." });
return;
}

const domain = server.domains.get(domainName);
if (!domain) {
res.status(400).send({ message: "Invalid domain provided." });
return;
}

await domain.resetAuth();
res.sendStatus(204);
} catch (err) {
server.logger.fatal(`[RESET:POST]: Fatal error while resetting the encryption key`, err);
res.status(500).send({ message: "Internal server error occured, please try again later." });
}
}

export const methods: RequestMethods[] = ["post"];
export const middleware: Middleware[] = [Auth.adminMiddleware.bind(Auth)];
25 changes: 25 additions & 0 deletions apps/web/src/app/admin/users/_table/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ export const columns: ColumnDef<Domain>[] = [
}
}

async function resetUser() {
try {
await axios.post("/api/admin/reset-user", { domain: domain!.domain });
toast({
title: "User Reset",
description: `${domain!.domain} has been reset. They can now use the default backup code "paperplane-cdn".`
});
} catch (err) {
const _error = "isAxiosError" in err ? (err as AxiosError<{ message: string }>).response?.data.message : "";
const error = _error || "n/a";

toast({
variant: "destructive",
title: "Uh oh! Something went wrong",
description: `There was a problem with your request: ${error}`,
action: (
<ToastAction altText="Try again" onClick={deleteUser}>
Try again
</ToastAction>
)
});
}
}

return (
<DropdownMenu>
<UpdateDialog domain={domain} isOpen={isOpen} setIsOpen={setIsOpen} />
Expand All @@ -118,6 +142,7 @@ export const columns: ColumnDef<Domain>[] = [
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => setIsOpen(true)}>Edit</DropdownMenuItem>
<DropdownMenuItem onClick={deleteUser}>Delete</DropdownMenuItem>
<DropdownMenuItem onClick={resetUser}>Reset Auth</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
Expand Down

0 comments on commit b6ab511

Please sign in to comment.