简体中文
了解如何在ShipThing应用程序中保护API端点。
honoAuthMiddlewareWrap
import { honoAuthMiddlewareWrap } from "@repo/auth/middleware"; import { Hono } from "hono"; const app = new Hono(); const hello = app.basePath("/hello").get( "/", honoAuthMiddlewareWrap, // ... (c) => { const { name } = c.req.query(); return c.json({ message: `Hello ${name || "World"}!` }); } ); export default hello;
auth
import { getAuth, honoAuthMiddlewareWrap } from '@repo/auth/middleware'; import { Hono } from "hono"; const app = new Hono(); const hello = app.basePath('/hello').get('/', honoAuthMiddlewareWrap, (c) => { const auth = getAuth(c); if (!auth?.userId) { return c.json({ message: '您尚未登录。', }); } return c.json({ message: '您已登录!', userId: auth.userId, }); }); export default hello;