@verisure-italy/express-authentication-middleware
v1.9.1
Published
Express middleware for Verisure authentication
Readme
@verisure-italy/express-authentication-middleware
Express middleware for Bearer-token authentication backed by DynamoDB. It resolves the token, validates expiration, loads the user, and populates req._auth with a typed request contract.
Installation
pnpm add @verisure-italy/express-authentication-middlewareMain Exports
dynamoAuthMiddleware(settings)createAuthMiddleware(dependencies)AuthenticationErrortype Settingstype AuthResponse- repository-facing types for custom integrations and tests
Runtime Flow
- read
Authorization: Bearer ... - if the header is missing or empty, call
next()with no error - query the token repository on the
token-index - reject invalid or expired tokens
- resolve the user by id or, when configured, by username
- populate
req._auth
Settings
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| dynamoConfig | Partial<DynamoConfig> | No | DynamoDB overrides forwarded to @verisure-italy/dynamo-kit |
| tokenTableName | string | No | Token table name. Defaults to access_token |
| userTableName | string | No | User table name. Defaults to user |
| userLookup.field | 'auto' | 'id' | 'username' | No | User lookup strategy |
| userLookup.usernameIndexName | string | No | Secondary index used for username lookup. Defaults to username-index |
AuthResponse
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| user | UserDetails | Yes | Resolved AAA user |
| token.accessToken | string | Yes | Raw bearer token |
| token.accessTokenExpiresAt | string | Yes | ISO string derived from the token expiration |
| token.scope | string[] | Yes | Parsed scope array |
Express Integration
import express from 'express'
import { dynamoAuthMiddleware } from '@verisure-italy/express-authentication-middleware'
const app = express()
app.use(dynamoAuthMiddleware({
tokenTableName: 'access_token',
userTableName: 'user',
dynamoConfig: {
region: 'eu-west-1',
endpoint: 'http://localhost:8000',
},
}))
app.get('/me', (req, res) => {
if (!req._auth) {
return res.status(401).json({ error: 'Not authenticated' })
}
res.json(req._auth)
})Repository-Level Integration
Use createAuthMiddleware() when you want to provide your own repositories:
import { createAuthMiddleware } from '@verisure-italy/express-authentication-middleware'
const middleware = createAuthMiddleware({
tokenRepo,
userRepo,
userLookup: {
field: 'auto',
},
})Notes
- Importing the package automatically augments Express so
req._authis typed. scopeis always exposed asstring[].- The token lookup requires a
token-indexsecondary index. - In
automode the middleware triesuserRepo.get(id)first, then falls back to username lookup.
