Better Auth para Express 5: monte as rotas de auth e proteja APIs com requireAuth
Código3 arquivosContexto~405 tokensanálise aprovada
Instalar
$
genpm add @yohangel/auth-expressO que você recebe
- Código em src/lib/auth-express/, 3 arquivos. (4,3 kB)
- Regras de IA em src/lib/auth-express/AGENTS.md, mais arquivos de regras da IDE.
- Resolve @yohangel/auth para você.
README
Este pacote não tem README.
~405 tokens→ src/lib/auth-express/AGENTS.md→ .cursor/rules/genpm-yohangel-auth-express.mdc
Isto é exatamente o que sua IA lê quando trabalha em src/lib/auth-express. Nada mais é adicionado ao contexto dela.
@yohangel/auth-express — rules for AI agents
Purpose
Connects @yohangel/auth to Express 5: mounts Better Auth on /api/auth/* and provides requireAuth / optionalAuth middleware that put the session in res.locals.
Module map
index.ts—mountAuth(app, opts),requireAuth,optionalAuth,AuthLocalstype,createAuthre-export.
Integration (do this after installing)
- Order matters — mount auth BEFORE body parsers:
import express from 'express'; import { mountAuth, requireAuth } from './lib/auth-express/index.js'; const app = express(); mountAuth(app, { sendEmail }); // /api/auth/* app.use(express.json()); app.get('/api/me', requireAuth, (req, res) => res.json({ id: res.locals.user.id })); - Frontend on another origin:
app.use(cors({ origin: ['https://app.example.com'], credentials: true }))beforemountAuth, and list that origin inAUTH_TRUSTED_ORIGINS. - Behind a proxy/CDN set
app.set('trust proxy', 1)andAUTH_IP_HEADERso rate limits use the real client IP. - Express 4 is not supported (route syntax
*splatis Express 5). Project must be ESM ("type": "module").
Conventions
- Protect routes with
requireAuth; readres.locals.user/res.locals.session(typed byAuthLocals). - Authorization (who can do what) goes in your handlers after
requireAuth, never on the client.
Don't
- Don't put
express.json()beforemountAuth(Better Auth must read the raw body). - Don't use
cors({ origin: '*', credentials: true }). - Don't return
res.locals.session(contains the token) to the client.
# @yohangel/auth-express — rules for AI agents
## Purpose
Connects `@yohangel/auth` to Express 5: mounts Better Auth on `/api/auth/*` and provides `requireAuth` / `optionalAuth` middleware that put the session in `res.locals`.
## Module map
- `index.ts` — `mountAuth(app, opts)`, `requireAuth`, `optionalAuth`, `AuthLocals` type, `createAuth` re-export.
## Integration (do this after installing)
1. Order matters — mount auth BEFORE body parsers:
```ts
import express from 'express';
import { mountAuth, requireAuth } from './lib/auth-express/index.js';
const app = express();
mountAuth(app, { sendEmail }); // /api/auth/*
app.use(express.json());
app.get('/api/me', requireAuth, (req, res) => res.json({ id: res.locals.user.id }));
```
2. Frontend on another origin: `app.use(cors({ origin: ['https://app.example.com'], credentials: true }))` before `mountAuth`, and list that origin in `AUTH_TRUSTED_ORIGINS`.
3. Behind a proxy/CDN set `app.set('trust proxy', 1)` and `AUTH_IP_HEADER` so rate limits use the real client IP.
4. Express 4 is not supported (route syntax `*splat` is Express 5). Project must be ESM (`"type": "module"`).
## Conventions
- Protect routes with `requireAuth`; read `res.locals.user` / `res.locals.session` (typed by `AuthLocals`).
- Authorization (who can do what) goes in your handlers after `requireAuth`, never on the client.
## Don't
- Don't put `express.json()` before `mountAuth` (Better Auth must read the raw body).
- Don't use `cors({ origin: '*', credentials: true })`.
- Don't return `res.locals.session` (contains the token) to the client.
A árvore exata que será injetada, após o .genpmignore. Fixada em
MIT License
Copyright (c) 2026 Yohangel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Este pacote não declara servidores MCP.
| Versão | Commit | Publicado | Análise |
|---|---|---|---|
| 1.0.0 | 7145b9d | há 3 horas | ✔ análise aprovada |
- npm
- nenhum
- proposto
- O GenPM propõe o comando npm e só o executa se você disser sim.
- análise
- análise aprovada · 0 achados
- commit
- auth-express@1.0.0 → 7145b9d58055b5145085782c958b7577f1d65276 · verificado após o download
- scripts
- Nenhum. O GenPM nunca executa código de pacotes.
- licença
- MIT
- denúncia
- Viu algo errado?