Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/fastify-auth-mongo-jwt
Sample user-management (signup, login) with Fastify and JWT
https://github.com/mcollina/fastify-auth-mongo-jwt
Last synced: about 1 month ago
JSON representation
Sample user-management (signup, login) with Fastify and JWT
- Host: GitHub
- URL: https://github.com/mcollina/fastify-auth-mongo-jwt
- Owner: mcollina
- License: mit
- Created: 2018-10-08T05:03:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-05T20:55:14.000Z (over 5 years ago)
- Last Synced: 2024-11-01T12:42:22.145Z (about 2 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 19
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-auth-mongo-jwt
Sample user-management (signup, login) with Fastify and JWT, on top of
[MongoDB](https://www.mongodb.com).This plugin sends credentials in plain text, and it implies that
HTTPS is used to protect the application.## Install
```
npm i @matteo.collina/fastify-auth-mongo-jwt
```## Usage
```js
'use strict'const Fastify = require('fastify')
const AuthMongoJwt = require('@matteo.collina/fastify-auth-mongo-jwt')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:
```json
{
"username": 'a unique thing',
"password": 'a long password'
}
```It will return a JWT token, encapsulated with an object:
```json
{
"status": 'ok',
"token": 'a jwt token'
}
```### GET /me
Requires the `Authorization: Bearer TOKEN` header.
Returns the current user if the token is valid```json
{
"username": 'a unique thing'
}
```### POST LOGIN
Accepts the following body:
```json
{
"username": 'a unique thing',
"password": 'a long password'
}
```It will return a JWT token, encapsulated with an object:
```json
{
"status": 'ok',
"token": 'a jwt token'
}
```## API
It adds the same decorators of
[fastify-jwt](https://github.com/fastify/fastify-jwt).### createUser(app, { username, password })
Utility function to help writing unit tests against this module. It
returns a JWT `token` for the given user and an `inject` function to
call HTTP endpoint with that token.Example:
```js
const Fastify = require('fastify')
const AuthMongoJwt = require('@matteo.collina/fastify-auth-mongo-jwt')
const { createUser } = AuthMongoJwtasync function run () {
const app = Fastify({
logger: {
level: 'error'
}
})app.register(AuthMongoJwt, {
auth: {
secret: 'thisisalongsecretjustfortests'
},
mongodb: {
url: 'mongodb://mongo/mydb',
w: 1,
useNewUrlParser: true
}
})app.register(async function (app, opts) {
app.addHook('preValidation', function (req, reply) {
return req.jwtVerify()
})app.get('/username', async function (req, reply) {
return req.user.username
})
}, { prefix: '/protected' })const {
token, // this is the JWT token
inject // utility function to inject with that token
} = await createUser(app)const res = await inject({
url: '/username',
method: 'GET'
})console.log(JSON.parse(res.body))
}run()
```## License
MIT