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
+16
-7
@@ -16,7 +16,7 @@ const registerSchema = z.object({
|
||||
.min(3, "Username must be at least 3 characters")
|
||||
.max(25, "Username must be at most 25 characters")
|
||||
.regex(/^[A-Za-z0-9_\-=?!@:.,]+$/, "Username contains invalid characters"),
|
||||
mail: z.string().email("Enter a valid email address"),
|
||||
mail: z.string().email("Enter a valid email address").optional().or(z.literal("")),
|
||||
password: z
|
||||
.string()
|
||||
.min(8, "Password must be at least 8 characters")
|
||||
@@ -43,6 +43,7 @@ export async function register(prevState: string | null, formData: FormData): Pr
|
||||
}
|
||||
|
||||
const { username, mail, password, look } = parsed.data;
|
||||
const hasEmail = !!mail;
|
||||
const ip = await clientIp();
|
||||
|
||||
// Throttle sign-ups per IP (5 per 10 minutes) to curb account spam.
|
||||
@@ -88,7 +89,7 @@ export async function register(prevState: string | null, formData: FormData): Pr
|
||||
data: {
|
||||
username,
|
||||
password: await hashPassword(password),
|
||||
mail,
|
||||
mail: hasEmail ? mail : null,
|
||||
accountCreated: now,
|
||||
ipRegister: ip,
|
||||
ipCurrent: ip,
|
||||
@@ -97,14 +98,22 @@ export async function register(prevState: string | null, formData: FormData): Pr
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
try {
|
||||
await sendVerification(created.id, mail);
|
||||
} catch {
|
||||
// No-op: account is created; user can request a new link later.
|
||||
if (hasEmail) {
|
||||
try {
|
||||
await sendVerification(created.id, mail);
|
||||
} catch {
|
||||
// No-op: account is created; user can request a new link later.
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return "Could not create the account (is the username unique?)";
|
||||
}
|
||||
|
||||
redirect("/login?registered=1");
|
||||
if (hasEmail) {
|
||||
redirect("/login?registered=1");
|
||||
} else {
|
||||
const { signIn } = await import("@/lib/auth");
|
||||
await signIn("credentials", { username, password, redirect: false });
|
||||
redirect("/verify?method=discord");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user