1
0
Fork 0

add email verification actions

dufisgsd
Lea 2023-08-09 23:57:16 +02:00
parent 1349951135
commit 80ba4b7d77
Signed by: lea
GPG Key ID: 1BAFFE8347019C42
2 changed files with 81 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import {
disableAccount, disableAccount,
queueAccountDeletion, queueAccountDeletion,
restoreAccount, restoreAccount,
verifyAccountEmail,
} from "@/lib/actions"; } from "@/lib/actions";
import dayjs from "dayjs"; import dayjs from "dayjs";
@ -86,6 +87,56 @@ export function AccountActions({
</AlertDialogFooter> </AlertDialogFooter>
</AlertDialogContent> </AlertDialogContent>
</AlertDialog> </AlertDialog>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
className="flex-1"
disabled={accountDraft.verification.status == "Verified"}
>
{accountDraft.verification.status == "Verified" ? "Email is verified" : "Verify email"}
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Mark Email as verified
</AlertDialogTitle>
<AlertDialogDescription>
Verification status is currently {accountDraft.verification.status}.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={async () => {
try {
await verifyAccountEmail(account._id);
toast({ title: "Verified email" });
setAccountDraft({ ...accountDraft, verification: { status: "Verified" } });
} catch(e) {
toast({
title: "Failed to verify",
description: String(e),
variant: "destructive",
})
}
}}
>
Mark verified
</AlertDialogAction>
<AlertDialogAction
disabled={!(accountDraft.verification.status != "Verified" && accountDraft.verification.token)}
onClick={() => {
navigator.clipboard.writeText(`https://app.revolt.chat/login/verify/${(accountDraft.verification as any).token}`);
toast({ title: "Copied verification link" })
}}
>
Copy link
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog> <AlertDialog>
<AlertDialogTrigger asChild> <AlertDialogTrigger asChild>

View File

@ -5,6 +5,7 @@ import { PLATFORM_MOD_ID, RESTRICT_ACCESS_LIST } from "./constants";
import mongo, { import mongo, {
Account, Account,
createDM, createDM,
fetchAccountById,
fetchChannels, fetchChannels,
fetchMembershipsByUser, fetchMembershipsByUser,
fetchMessages, fetchMessages,
@ -211,6 +212,35 @@ export async function changeAccountEmail(userId: string, email: string) {
) )
} }
export async function verifyAccountEmail(userId: string) {
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
await checkPermission("accounts/update/email", userId);
const account = await fetchAccountById(userId);
if (!account) throw new Error("couldn't find account");
if (account.verification.status == "Verified") throw new Error("already verified");
let email = account.email;
if (account.verification.status == "Moving") {
email = account.verification.new_email;
}
await mongo()
.db("revolt")
.collection<Account>("accounts")
.updateOne(
{ _id: userId },
{
$set: {
email: email,
email_normalised: email, // <-- should be fine but someone should fix this in the future
verification: { status: "Verified" },
}
}
)
}
export async function suspendUser(userId: string) { export async function suspendUser(userId: string) {
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access"; if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";