@rtorcato/api-auth-hono
v1.0.0
Published
Hono middleware for @rtorcato/api-auth — JWT authentication with Bearer-header and cookie support.
Maintainers
Readme
@rtorcato/api-auth-hono
Hono middleware for @rtorcato/api-auth — JWT authentication with Bearer-header and cookie support.
Install
pnpm add @rtorcato/api-auth @rtorcato/api-auth-honohono is a peer dependency — you bring your own version.
Usage
import { Hono } from 'hono'
import { authMiddleware, type AuthVariables } from '@rtorcato/api-auth-hono'
const app = new Hono<{ Variables: AuthVariables }>()
app.use(authMiddleware(process.env.JWT_SECRET))
app.get('/me', (c) => c.json({ user: c.get('user') }))Type the app with { Variables: AuthVariables } to get a typed c.get('user').
authMiddleware(secret, options?)— throwsUnauthorizedError(missing_token/invalid_token) when the token is absent or invalid. Pair with@rtorcato/api-errors-hono'serrorHandlerto translate it to a JSON response.optionalAuthMiddleware(secret, options?)— setsc.get('user')when a valid token is present, otherwise passes through untouched.
Options: { cookieName?: string; verifyOptions?: VerifyOptions }. The token is read from the Authorization: Bearer … header first, then the cookie named cookieName (default token).
Related
@rtorcato/api-auth— framework-agnostic core@rtorcato/api-auth-express— Express adapter
Source: https://github.com/rtorcato/api-common/tree/main/packages/api-auth-hono
