UNPKG

3.94 kBMarkdownView Raw
1[![Build Status](https://img.shields.io/travis/Authmaker/authmaker-verify-express/master.svg)](https://travis-ci.org/Authmaker/authmaker-verify-express)
2[![Code Climate](https://img.shields.io/codeclimate/github/Authmaker/authmaker-verify-express.svg)](https://codeclimate.com/github/Authmaker/authmaker-verify-express)
3
4[![Version npm](https://img.shields.io/npm/v/authmaker-verify-express.svg)](https://www.npmjs.com/package/authmaker-verify-express)
5[![Dependencies](https://img.shields.io/david/Authmaker/authmaker-verify-express.svg)](https://david-dm.org/Authmaker/authmaker-verify-express)
6[![npm Downloads](https://img.shields.io/npm/dm/authmaker-verify-express.svg)](https://www.npmjs.com/package/authmaker-verify-express)
7
8# Authmaker Verify Express
9This package allows you to use [Authmaker verify](https://www.npmjs.com/package/authmaker-verify) extremely easily in an ExpressJS based Node application.
10
11## Installation
12```
13npm install --save authmaker-verify-express
14```
15
16### Usage
17This package currently makes use of the Mongo connection for Authmaker Verify so you need to initialise the database connection before use:
18```
19var authmakerVerify = require('authmaker-verify');
20authmakerVerify.connectMongo(nconf);
21```
22
23You need to pass a [nconf](https://github.com/indexzero/nconf) object into the connectMongo call that has access to at least the following parameters:
24
25```
26{
27 "mongo": {
28 "authmaker": {
29 "db": "your-db-name",
30 "host": "localhost",
31 "port": 27017
32 }
33 }
34}
35```
36
37you can also optionally include `username` and `password`. Each of these config entries are accessed asynchronously so you can use any of the asynchronous stores for nconf
38
39#### Middlewares
40To actually use this package you just need to include middlewares in your ExpressJS app. Here are a few examples of ways you can use authmaker-verify-express. Please note that all of these examples use a simple "success" callback that does nothing but responds with a 200 response code. They have also already imported required modules:
41
42```
43var authmakerVerifyExpress = require('authmaker-verify-express');
44var express = require('express');
45var app = express();
46
47function success(req, res){
48 return res.send("Success");
49}
50```
51
52Requires users with valid, in date access tokens in the request:
53```
54app.get('/verify': [authmakerVerifyExpress.mongo(), success]);
55```
56
57Requires users with valid, in date access tokens with the scope "my_awesome_permission":
58```
59app.get('/scope': [authmakerVerifyExpress.mongo("my_awesome_permission"), success]);
60```
61
62Requires users with valid, in date access tokens with a rate limited scope (suffix `_limit_<num>_<timeframe>`)
63```
64app.get('/jointrated': [authmakerVerifyExpress.mongoRateLimited("face"), success]);
65```
66
67Requires users with valid, in date access token but if they don't have a rate limited scope it uses `face_limit_10_minutes` as a default scope:
68```
69app.get('/defaultScope': [authmakerVerifyExpress.mongoRateLimited("face", "face_limit_10_minutes"), success]);
70```
71
72### API
73
74#### mongoRateLimited - function - returns middleware
75```
76mongoRateLimited: function(tag, defaultScope)
77```
78
79#### mongoRateLimitedDefault - function - returns middleware
80```
81mongoRateLimitedDefault: function(tag, defaultScope)
82```
83
84#### mongo - function - returns middleware
85```
86mongo: function(tag, options)
87```
88
89options.passError (optional) if true passes error via middleware
90
91#### connectMongo - function
92```
93connectMongo: function(nconf) {
94 //initialise the db
95 authmakerVerify.connectMongo(nconf);
96}
97```
98
99#### authmakerVerify - object
100If you ever need to access the [authmaker-verify](https://github.com/Authmaker/authmaker-verify) object that is powering authmaker-verify-express to access any lower level apis you can access it directly like this:
101```
102var authmakerVerifyExpress = require('authmaker-verify-express');
103authmakerVerifyExpress.authmakerVerify;
104```