> ## 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.

# Protect API endpoints

> Learn how to protect your API endpoints in your ShipThing application.

## Check if the user is authenticated

To protect your API endpoints, you can use the `honoAuthMiddlewareWrap` in your router or on a specific endpoint.

```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;
```

The middlware will throw an error if the user is not authenticated.

The `honoAuthMiddlewareWrap` will also provide the `auth` infromation to the endpoint. If the handler is reached, both objects are available in the `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: 'You are not logged in.',
    });
  }

  return c.json({
    message: 'You are logged in!',
    userId: auth.userId,
  });
});

export default hello;
```
