Add Discord verification option for users without email
This commit is contained in:
1 parent
eb01b14379
commit
43c0ba6614
6 files changed
+257
-56
No files matched your search
@@ -0,0 +1,55 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
|
||||
export async function linkDiscordId(discordId: string): Promise<string | null> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return "Niet ingelogd";
|
||||
|
||||
const userId = Number(session.user.id);
|
||||
if (!discordId || !/^\d{17,20}$/.test(discordId.trim())) {
|
||||
return "Ongeldig Discord ID";
|
||||
}
|
||||
|
||||
const discordIdClean = discordId.trim();
|
||||
|
||||
// Check if this Discord ID is already linked to another account.
|
||||
try {
|
||||
const existing = await prisma.socialAccounts.findUnique({
|
||||
where: { provider_providerId: { provider: "discord", providerId: discordIdClean } },
|
||||
select: { userId: true },
|
||||
});
|
||||
if (existing && Number(existing.userId) !== userId) {
|
||||
return "Dit Discord ID is al gekoppeld aan een ander account";
|
||||
}
|
||||
} catch {
|
||||
return "Fout bij controleren Discord ID";
|
||||
}
|
||||
|
||||
try {
|
||||
// Upsert: create or update the social_accounts entry.
|
||||
await prisma.socialAccounts.upsert({
|
||||
where: { provider_providerId: { provider: "discord", providerId: discordIdClean } },
|
||||
create: {
|
||||
userId: BigInt(userId),
|
||||
provider: "discord",
|
||||
providerId: discordIdClean,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
update: { userId: BigInt(userId), updatedAt: new Date() },
|
||||
});
|
||||
|
||||
// Mark user as verified.
|
||||
await prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { mailVerified: "1" },
|
||||
});
|
||||
|
||||
return null; // success
|
||||
} catch (e) {
|
||||
console.error("[link-discord] Failed:", (e as Error).message);
|
||||
return "Fout bij koppelen van Discord account";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user