Files
wzdj/old/scripts/set-user-password.ts
2026-04-20 14:56:28 +08:00

49 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 重置指定手机号的登录密码userAuths.credential
* 用法pnpm exec tsx scripts/set-user-password.ts [手机号] [新密码]
* 默认15880802661 key123456
*/
import { config } from "dotenv"
import { resolve } from "path"
config({ path: resolve(process.cwd(), ".env.local") })
import { MongoClient } from "mongodb"
import { hashPassword } from "../lib/auth-utils"
import { COLLECTIONS } from "../lib/db/mongo/collections"
const phone = process.argv[2]?.trim() || "15880802661"
const newPassword = process.argv[3] || "key123456"
async function main() {
const uri = process.env.MONGODB_URI
if (!uri) {
console.error("缺少 MONGODB_URI请在 .env.local 配置")
process.exit(1)
}
const client = new MongoClient(uri)
await client.connect()
try {
const db = client.db("wanzhi_esports")
const auths = db.collection(COLLECTIONS.userAuths)
const hash = hashPassword(newPassword)
const r = await auths.updateOne(
{ authType: "phone", authId: phone },
{ $set: { credential: hash, updatedAt: new Date() } },
)
if (r.matchedCount === 0) {
console.error(`未找到手机号 ${phone} 的认证记录,请先注册或检查号码`)
process.exit(1)
}
console.log(`已更新 ${phone} 的登录密码(${newPassword.length} 位)`)
} finally {
await client.close()
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})