@beetle/fastify-auth-jwt-mongo

0.2.0 • Public • Published

fastify-auth-jwt-mongo

fastify-auth-mongo-jwt

Sample user-management (signup, login) with Fastify and JWT, on top of MongoDB.

This plugin sends credentials in plain text, and it implies that HTTPS is used to protect the application.

Install

npm intall @beetle/fastify-auth-jwt-mongo

Usage

'use strict'

const Fastify = require('fastify')
const AuthMongoJwt = require('@beetle/fastify-auth-jwt-mongo')

const app = Fastify()

app.register(AuthMongoJwt, {
  auth: {
    secret: 'thisisalongsecretjustfortests'
  },
  mongodb: {
    url: 'mongodb://mongo/mydb',
    w: 1,
    useNewUrlParser: true
  }
})

app.register(async function (app, opts) {
  app.addHook('preHandler', function (req, reply) {
    return req.jwtVerify()
  })

  app.get('/username', async function (req, reply) {
    return req.user.username
  })
}, { prefix: '/protected' })

REST routes

This fastify plugin offers the following routes.

POST /signup

Accepts the following body:

{
  "username": 'a unique thing',
  "password": 'a long password'
}

It will return a JWT token, encapsulated with an object:

{
  "status": 'ok',
  "token": 'a jwt token'
}

GET /me

Requires the Authorization: Bearer TOKEN header. Returns the current user if the token is valid

{
  "username": 'a unique thing'
}

POST LOGIN

Accepts the following body:

{
  "username": 'a unique thing',
  "password": 'a long password'
}

It will return a JWT token, encapsulated with an object:

{
  "status": 'ok',
  "token": 'a jwt token'
}

API

It adds the same decorators of fastify-jwt.

License

MIT

Package Sidebar

Install

npm i @beetle/fastify-auth-jwt-mongo

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

15.3 kB

Total Files

7

Last publish

Collaborators

  • davide.dantonio