Necrotizing Pineapple Music

    @nicolasparada/jwt-middleware

    0.3.1 • Public • Published

    @nicolasparada/jwt-middleware npm version

    JSON Web Token Middleware

    jsonwebtoken is a peerDependency, so make sure to install it as well.

    Usage

    const framework = require('@nicolasparada/web-framework')
    const jwt = require('@nicolasparada/jwt-middleware')
    const { sign } = require('jsonwebtoken')
    
    const app = framework()
    const SECRET = 'shared-secret'
    
    const makeResponse = (statusCode, body, headers = {}) => ({ statusCode, headers, body })
    
    app.get('/login', ({ query: { username, password } }) => {
      if (!(username === 'admin' && password === 'lol')) {
        return makeResponse(401, { message: 'Wrong credentials.' })
      }
      const token = sign({ username }, SECRET, { expiresIn: 60 * 60 * 24 * 7 })
      return makeResponse(201, { token })
    })
    
    app.get('/me', jwt({ secret: SECRET }), req => req.user)
    
    app.listen(80, '127.0.0.1', () => {
      console.log('Server running at http://localhost/')
    })
    curl "http://localhost/login?username=admin&password=lol"
    curl -H "Authorization: Bearer token-here" http://localhost/me

    Options

    • secret: Shared secret to verify the token (string or buffer required).
    • passthrough: Whether you want to allow to pass middlewares through, in case, the request user will be null (defaults to false).
    • verifyOptions: Object to be passed down to jsonwebtoken.verify.
    • key: Key to use in the request to put the decoded token in (defaults to user).
    • tokenKey: Key to use in the request to put the token in (defaults to token).

    Install

    npm i @nicolasparada/jwt-middleware

    DownloadsWeekly Downloads

    1

    Version

    0.3.1

    License

    MIT

    Last publish

    Collaborators

    • nicolasparada