10 changed files with 70 additions and 15 deletions
@ -0,0 +1,7 @@ |
|||||
|
import { readBody } from "h3"; |
||||
|
import { forgotPassword } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler(async (event) => { |
||||
|
const body = await readBody<{ email?: string }>(event); |
||||
|
return forgotPassword(event, body.email ?? ""); |
||||
|
}); |
||||
@ -0,0 +1,10 @@ |
|||||
|
import { readBody } from "h3"; |
||||
|
import { loginWithSession } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler(async (event) => { |
||||
|
const body = await readBody<{ email?: string; password?: string }>(event); |
||||
|
return loginWithSession(event, { |
||||
|
email: body.email ?? "", |
||||
|
password: body.password ?? "", |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,3 @@ |
|||||
|
import { logoutSession } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler((event) => logoutSession(event)); |
||||
@ -0,0 +1,3 @@ |
|||||
|
import { registerWithSession } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler((event) => registerWithSession(event)); |
||||
@ -0,0 +1,13 @@ |
|||||
|
import { readBody } from "h3"; |
||||
|
import { resetPassword } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler(async (event) => { |
||||
|
const body = await readBody<{ |
||||
|
token?: string; |
||||
|
new_password?: string; |
||||
|
}>(event); |
||||
|
return resetPassword(event, { |
||||
|
token: body.token ?? "", |
||||
|
new_password: body.new_password ?? "", |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,7 @@ |
|||||
|
import { readBody } from "h3"; |
||||
|
import { verifyEmail } from "../../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler(async (event) => { |
||||
|
const body = await readBody<{ token?: string }>(event); |
||||
|
return verifyEmail(event, body.token ?? ""); |
||||
|
}); |
||||
@ -1,15 +0,0 @@ |
|||||
import { usersTable } from "drizzle-pkg/lib/schema/schema"; |
|
||||
import { dbGlobal } from "drizzle-pkg/lib/db"; |
|
||||
import log4js from "logger"; |
|
||||
|
|
||||
const logger = log4js.getLogger("APP") |
|
||||
|
|
||||
export default defineEventHandler(async (event) => { |
|
||||
logger.info("hello: world"); |
|
||||
const users = await dbGlobal.select().from(usersTable) |
|
||||
logger.info("users (formatted): %s \n", JSON.stringify(users, null, 2)); |
|
||||
return { |
|
||||
hello: 'world', |
|
||||
users: users, |
|
||||
} |
|
||||
}) |
|
||||
@ -0,0 +1,3 @@ |
|||||
|
import { getCurrentUser } from "../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler((event) => getCurrentUser(event)); |
||||
@ -0,0 +1,7 @@ |
|||||
|
import { readBody } from "h3"; |
||||
|
import { patchMe } from "../services/auth"; |
||||
|
|
||||
|
export default defineEventHandler(async (event) => { |
||||
|
const body = await readBody<{ name?: string; age?: number }>(event); |
||||
|
return patchMe(event, body ?? {}); |
||||
|
}); |
||||
@ -0,0 +1,17 @@ |
|||||
|
import { send, setResponseStatus } from "h3"; |
||||
|
|
||||
|
/** |
||||
|
* 将 API 路由上 `createError({ data: { error } })` 的 body 以 JSON 返回(与规格一致)。 |
||||
|
*/ |
||||
|
export default defineNitroPlugin((nitroApp) => { |
||||
|
nitroApp.hooks.hook("error", async (error, ctx) => { |
||||
|
const event = ctx.event; |
||||
|
if (!event?.path?.startsWith("/api/")) return; |
||||
|
const err = error as { statusCode?: number; data?: unknown }; |
||||
|
const data = err.data as { error?: { code: string; message: string } } | undefined; |
||||
|
if (!data?.error) return; |
||||
|
if (event.node.res.headersSent) return; |
||||
|
setResponseStatus(event, err.statusCode ?? 500); |
||||
|
await send(event, JSON.stringify(data), "application/json"); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue