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
|
9 | This 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 | ```
|
13 | npm install --save authmaker-verify-express
|
14 | ```
|
15 |
|
16 | ### Usage
|
17 | This package currently makes use of the Mongo connection for Authmaker Verify so you need to initialise the database connection before use:
|
18 | ```
|
19 | var authmakerVerify = require('authmaker-verify');
|
20 | authmakerVerify.connectMongo(nconf);
|
21 | ```
|
22 |
|
23 | You 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 |
|
37 | you 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
|
40 | To 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 | ```
|
43 | var authmakerVerifyExpress = require('authmaker-verify-express');
|
44 | var express = require('express');
|
45 | var app = express();
|
46 |
|
47 | function success(req, res){
|
48 | return res.send("Success");
|
49 | }
|
50 | ```
|
51 |
|
52 | Requires users with valid, in date access tokens in the request:
|
53 | ```
|
54 | app.get('/verify': [authmakerVerifyExpress.mongo(), success]);
|
55 | ```
|
56 |
|
57 | Requires users with valid, in date access tokens with the scope "my_awesome_permission":
|
58 | ```
|
59 | app.get('/scope': [authmakerVerifyExpress.mongo("my_awesome_permission"), success]);
|
60 | ```
|
61 |
|
62 | Requires users with valid, in date access tokens with a rate limited scope (suffix `_limit_<num>_<timeframe>`)
|
63 | ```
|
64 | app.get('/jointrated': [authmakerVerifyExpress.mongoRateLimited("face"), success]);
|
65 | ```
|
66 |
|
67 | Requires 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 | ```
|
69 | app.get('/defaultScope': [authmakerVerifyExpress.mongoRateLimited("face", "face_limit_10_minutes"), success]);
|
70 | ```
|
71 |
|
72 | ### API
|
73 |
|
74 | #### mongoRateLimited - function - returns middleware
|
75 | ```
|
76 | mongoRateLimited: function(tag, defaultScope)
|
77 | ```
|
78 |
|
79 | #### mongoRateLimitedDefault - function - returns middleware
|
80 | ```
|
81 | mongoRateLimitedDefault: function(tag, defaultScope)
|
82 | ```
|
83 |
|
84 | #### mongo - function - returns middleware
|
85 | ```
|
86 | mongo: function(tag, options)
|
87 | ```
|
88 |
|
89 | options.passError (optional) if true passes error via middleware
|
90 |
|
91 | #### connectMongo - function
|
92 | ```
|
93 | connectMongo: function(nconf) {
|
94 | //initialise the db
|
95 | authmakerVerify.connectMongo(nconf);
|
96 | }
|
97 | ```
|
98 |
|
99 | #### authmakerVerify - object
|
100 | If 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 | ```
|
102 | var authmakerVerifyExpress = require('authmaker-verify-express');
|
103 | authmakerVerifyExpress.authmakerVerify;
|
104 | ```
|