Skip to content

Commit 935c8d7

Browse files
committed
feat: validated by normalized email / username
1 parent 90f7e4f commit 935c8d7

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

app/api/auth/register/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export const register = async (
2929
return '您的用户名已经有人注册了, 请修改'
3030
}
3131

32-
const sameEmailUser = await prisma.user.findUnique({ where: { email } })
32+
const normalizedEmail = email.toLowerCase()
33+
const sameEmailUser = await prisma.user.findFirst({
34+
where: { email: { equals: normalizedEmail, mode: 'insensitive' } }
35+
})
3336
if (sameEmailUser) {
3437
return '您的邮箱已经有人注册了, 请修改'
3538
}

app/api/forgot/one/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ export const stepOne = async (
1414
// OR: [{ email: input.name }, { name: input.name }]
1515
// }
1616
// })
17+
const normalizedInput = input.name.toLowerCase()
1718
const user = await prisma.user.findFirst({
18-
where: { email: input.name }
19+
where: {
20+
OR: [
21+
{ email: { equals: normalizedInput, mode: 'insensitive' } },
22+
{ name: { equals: normalizedInput, mode: 'insensitive' } }
23+
]
24+
}
1925
})
2026
if (!user) {
2127
return '用户未找到'

app/api/forgot/two/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ export const stepTwo = async (input: z.infer<typeof stepTwoSchema>) => {
1111
return '两次密码输入不一致'
1212
}
1313

14+
const normalizedInput = input.name.toLowerCase()
1415
const user = await prisma.user.findFirst({
1516
where: {
16-
OR: [{ email: input.name }, { name: input.name }]
17+
OR: [
18+
{ email: { equals: normalizedInput, mode: 'insensitive' } },
19+
{ name: { equals: normalizedInput, mode: 'insensitive' } }
20+
]
1721
}
1822
})
1923
if (!user) {

app/api/utils/verifyVerificationCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export const verifyVerificationCode = async (
1010
return false
1111
}
1212

13-
return userProvidedCode === storedCode
13+
return userProvidedCode.toLowerCase() === storedCode.toLowerCase()
1414
}

migration/fixDuplicateUser.mjs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { PrismaClient } from '@prisma/client'
2+
import crypto from 'crypto'
3+
4+
const prisma = new PrismaClient()
5+
6+
const generateRandomEmail = () => {
7+
const randomString = crypto.randomBytes(8).toString('hex')
8+
return `${randomString}@example.com`
9+
}
10+
11+
const main = async () => {
12+
console.log('开始清理重复邮箱任务...')
13+
14+
console.log('正在获取所有用户信息...')
15+
const allUsers = await prisma.user.findMany()
16+
console.log(`共找到 ${allUsers.length} 个用户。`)
17+
18+
const emailGroups = new Map()
19+
for (const user of allUsers) {
20+
if (user.email) {
21+
const lowerEmail = user.email.toLowerCase()
22+
if (!emailGroups.has(lowerEmail)) {
23+
emailGroups.set(lowerEmail, [])
24+
}
25+
emailGroups.get(lowerEmail).push(user)
26+
}
27+
}
28+
29+
const userIdsToUpdate = []
30+
for (const [lowerEmail, usersInGroup] of emailGroups.entries()) {
31+
if (usersInGroup.length > 1) {
32+
console.log(
33+
`\n发现重复邮箱组: ${lowerEmail} (共 ${usersInGroup.length} 个账户)`
34+
)
35+
36+
usersInGroup.sort((a, b) => {
37+
if (b.moemoepoint !== a.moemoepoint) {
38+
return b.moemoepoint - a.moemoepoint
39+
}
40+
41+
return (
42+
new Date(a.register_time).getTime() -
43+
new Date(b.register_time).getTime()
44+
)
45+
})
46+
47+
const userToKeep = usersInGroup[0]
48+
const usersToReset = usersInGroup.slice(1)
49+
50+
console.log(
51+
` - 准备保留: 用户ID ${userToKeep.id} (Email: ${userToKeep.email}, Points: ${userToKeep.moemoepoint})`
52+
)
53+
usersToReset.forEach((u) => {
54+
console.log(
55+
` - 准备重置: 用户ID ${u.id} (Email: ${u.email}, Points: ${u.moemoepoint})`
56+
)
57+
userIdsToUpdate.push(u.id)
58+
})
59+
}
60+
}
61+
62+
if (userIdsToUpdate.length > 0) {
63+
console.log(`\n共找到 ${userIdsToUpdate.length} 个账户需要重置邮箱。`)
64+
console.log('正在执行数据库更新...')
65+
66+
const updatePromises = userIdsToUpdate.map((id) =>
67+
prisma.user.update({
68+
where: { id },
69+
data: { email: generateRandomEmail() }
70+
})
71+
)
72+
73+
await prisma.$transaction(updatePromises)
74+
75+
console.log('所有需要重置的账户邮箱已更新完毕!')
76+
} else {
77+
console.log('\n没有找到需要清理的重复邮箱账户。')
78+
}
79+
}
80+
81+
main()
82+
.catch((e) => {
83+
console.error('脚本执行出错:', e)
84+
process.exit(1)
85+
})
86+
.finally(async () => {
87+
await prisma.$disconnect()
88+
})

0 commit comments

Comments
 (0)