> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shipthing.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 保护API端点

> 了解如何在ShipThing应用程序中保护API端点。

## 检查用户是否已认证

要保护您的API端点，您可以在路由器或特定端点上使用`honoAuthMiddlewareWrap`。

```ts hello/index.ts theme={"system"}
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;
```

如果用户未认证，中间件将抛出错误。

`honoAuthMiddlewareWrap`还会向端点提供`auth`信息。如果到达处理程序，`auth`中这两个对象都可用。

```ts hello/index.ts theme={"system"}
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;
```
