Files
BYW-VoiceHub/server/api/user/2fa/generate.post.ts
T
Henry_Du 4ce4649aaf
Build VoiceHub FPK / build-fpk (push) Failing after 56m5s
Build and Push Docker Image / build-and-push (push) Canceled after 1h9m35s
初次提交
2026-09-06 08:19:02 +08:00

29 lines
846 B
TypeScript

import otplib from 'otplib'
const { authenticator } = otplib
import QRCode from 'qrcode'
import { db, userIdentities, eq, and } from '~/drizzle/db'
export default defineEventHandler(async (event) => {
const user = event.context.user
if (!user) {
throw createError({ statusCode: 401, message: '未授权访问' })
}
// 检查是否已开启2FA
const existing = await db.query.userIdentities.findFirst({
where: and(eq(userIdentities.userId, user.id), eq(userIdentities.provider, 'totp'))
})
if (existing) {
throw createError({ statusCode: 400, message: '已开启双重认证' })
}
const secret = authenticator.generateSecret()
const otpauth = authenticator.keyuri(user.username, 'VoiceHub', secret)
// 生成QR Code Data URL
const qrCode = await QRCode.toDataURL(otpauth)
return { secret, qrCode }
})