feat: refactor authentication logic for admin and user login, improve error handling
This commit is contained in:
@ -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: '管理员登录成功!',
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user