refactor: centralize validation, error handling, and formatting logic

Extract shared auth logic and validation rules to shared/auth.ts
Introduce utility functions for HTTP errors and user input parsing
Standardize error messages and date formatting across the app
This commit is contained in:
2026-04-12 20:29:39 +08:00
parent 377a9617be
commit 07e5d42005
23 changed files with 294 additions and 267 deletions

View File

@@ -2,28 +2,15 @@ import { DEFAULT_USER_PASSWORD } from '~~/shared/auth'
import { requireRole } from '../../../../utils/auth'
import { hashPassword } from '../../../../utils/password'
import { getUserById, updateUserPassword } from '../../../../utils/user-repository'
import { updateUserPassword } from '../../../../utils/user-repository'
import { requireExistingUser, requireUserIdParam } from '../../../../utils/users'
export default defineEventHandler(async (event) => {
await requireRole(event, 'super_admin')
const userId = getRouterParam(event, 'id')
const userId = requireUserIdParam(event)
if (!userId) {
throw createError({
statusCode: 400,
statusMessage: 'User id is required'
})
}
const user = await getUserById(userId)
if (!user) {
throw createError({
statusCode: 404,
statusMessage: 'User not found'
})
}
await requireExistingUser(userId)
const passwordHash = await hashPassword(DEFAULT_USER_PASSWORD)
@@ -33,7 +20,7 @@ export default defineEventHandler(async (event) => {
mustChangePassword: true
})
const updatedUser = await getUserById(userId)
const updatedUser = await requireExistingUser(userId, 'Unable to load updated user')
return {
user: updatedUser,