1
0
Fork 0

feat: add button to change a user's email

dufisgsd
Lea 2023-08-09 23:28:11 +02:00
parent 4f6be05162
commit 1349951135
Signed by: lea
GPG Key ID: 1BAFFE8347019C42
3 changed files with 69 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import type { Account } from "@/lib/db";
import { User } from "revolt-api";
import {
cancelAccountDeletion,
changeAccountEmail,
disableAccount,
queueAccountDeletion,
restoreAccount,
@ -24,6 +25,8 @@ import {
AlertDialogTitle,
AlertDialogTrigger,
} from "../../ui/alert-dialog";
import { AlertDialogDescription } from "@radix-ui/react-alert-dialog";
import { Input } from "@/components/ui/input";
dayjs.extend(relativeTime);
export function AccountActions({
@ -36,9 +39,54 @@ export function AccountActions({
const { toast } = useToast();
const [accountDraft, setAccountDraft] = useState(account);
const [emailDraft, setEmailDraft] = useState("");
return (
<div className="flex gap-2">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button className="flex-1">
Change Email
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Update account email
</AlertDialogTitle>
<AlertDialogDescription>
<Input
placeholder={account.email}
onChange={(e) => setEmailDraft(e.currentTarget.value)}
value={emailDraft}
/>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
disabled={!/^.+@.+$/.test(emailDraft)}
onClick={async () => {
try {
await changeAccountEmail(account._id, emailDraft);
setEmailDraft("");
toast({ title: "Updated email" });
window.location.reload();
} catch (err) {
toast({
title: "Failed to execute action",
description: String(err),
variant: "destructive",
});
}
}}
>
Change
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button

View File

@ -8,6 +8,7 @@ type Permission =
| `accounts${
| ""
| `/fetch${"" | "/by-id"}`
| "/update/email"
| "/disable"
| "/restore"
| `/deletion${"" | "/queue" | "/cancel"}`}`
@ -104,6 +105,7 @@ const PermissionSets = {
"accounts/restore",
"accounts/deletion/queue",
"accounts/deletion/cancel",
"accounts/update/email",
] as Permission[],
// Moderate users

View File

@ -192,6 +192,25 @@ export async function disableAccount(userId: string) {
});
}
export async function changeAccountEmail(userId: string, email: string) {
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
await checkPermission("accounts/update/email", userId);
await mongo()
.db("revolt")
.collection<Account>("accounts")
.updateOne(
{ _id: userId },
{
$set: {
email: email,
email_normalised: email,
verification: { status: "Verified" },
}
}
)
}
export async function suspendUser(userId: string) {
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";