Authentication

Auth Middleware

Extracting and verifying JWT tokens from requests.

1 min readDocumentationEdit this page

Usage#

typescript
import { authenticate } from '@AxilJS/auth'
 
app.get('/profile', authenticate(jwt), (req, res) => {
  res.json(req.locals.user)
})

Token sources#

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

typescript
authenticate(jwt, { required: false })
// req.locals.user is undefined for unauthenticated requests

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY