Auth Middleware
Extracting and verifying JWT tokens from requests.
1 min readEdit this page
Usage
import { authenticate } from '@restjs/auth'
app.get('/profile', authenticate(jwt), (req, res) => {
res.json(req.locals.user)
})Token sources
authenticate(jwt, { source: 'header' }) // Authorization: Bearer <token>
authenticate(jwt, { source: 'cookie' }) // cookie named 'token'
authenticate(jwt, { source: 'query' }) // ?token=...
authenticate(jwt, { source: ['header', 'cookie'] })Optional auth
authenticate(jwt, { required: false })
// req.locals.user is undefined for unauthenticated requests