提交正式官网
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { generateToken, md5 } from '~/server/utils/auth'
|
||||
import { getUserByUsername } from '~/server/utils/db'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{ username?: string; password?: string }>(event)
|
||||
const { username, password } = body || {}
|
||||
|
||||
if (!username?.trim() || !password) {
|
||||
return fail('用户名和密码不能为空')
|
||||
}
|
||||
|
||||
const user = await getUserByUsername(username.trim())
|
||||
if (!user || user.password !== md5(password)) {
|
||||
return fail('用户名或密码错误')
|
||||
}
|
||||
|
||||
if (user.status === 'inactive') {
|
||||
return fail('用户已被禁用,无法登录')
|
||||
}
|
||||
|
||||
return ok({ token: generateToken(user.id) }, '登录成功')
|
||||
})
|
||||
Reference in New Issue
Block a user