feat: refactor authentication logic for admin and user login, improve error handling

This commit is contained in:
2025-06-18 16:59:36 +08:00
parent 0ca2d51669
commit ad78f713a3
5 changed files with 35 additions and 8219 deletions

View File

@ -1,23 +1,15 @@
import { defineEventHandler, readBody, setResponseStatus, useRuntimeConfig } from 'h3';
import { defineEventHandler, readBody, createError, useRuntimeConfig } from 'h3';
import { adminLoginLogic } from './admin/_logic';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const { password } = body;
const config = useRuntimeConfig(event);
if (!password) {
setResponseStatus(event, 400);
return { message: '请填写密码' };
const { adminPassword } = useRuntimeConfig();
try {
const body = await readBody(event);
return await adminLoginLogic(body, adminPassword);
} catch (error: any) {
throw createError({
statusCode: error.statusCode || 500,
statusMessage: error.message,
});
}
const adminPassword = config.adminPassword;
if (!adminPassword || password !== adminPassword) {
setResponseStatus(event, 401);
return { message: '密码错误' };
}
return {
message: '管理员登录成功!',
};
});