auth-basic-jwt
Basic auth + JWT middleware for Express
Initialization
const authModule = const auth =
Note that the "userLogin" parameter must match the expected basic auth login
Options:
token: filter* : or var, // Data to put in the token.user attribute // (default is the whole user param without the pass attribute) // must return a "role" attribute in order to be compared with the // auth.hasRole(...) method. decode* : function(token) or var, // Data to put in the req.user attribute // (default is the token.user data) exp : function(user) or var, iss : function(user) or var, sub : function(user) or var, aud : function(user) or var, } session: filter* : , // Data to put in the req.user attribute // (default is the whole user param without the pass attribute) // must return a "role" attribute in order to be compared with the // auth.hasRole(...) method. } password: compare*: :boolean // function used to compare // the user password (user.pass) and the provided credential (pass). // Default is "user.pass == pass" } , // method ) login: { path: string // path to match for a jwt request (default '/login') method: string // method to match for a jwt request (default 'POST') }
- Functions marked with
*
can return a promise. - Note that the user parameter is the object forwarded by your userGetter.
- Be careful: if you don't set token.filter, user must be an object, in order to let the default filter delete the pass attribute (if you use mongoose for example ensure that it have been converted with the toObject method (or define the session & token filters))
Usage
Example of usage:
your-auth-config.js
{ return email: userLogin pass: 'password' roles: 'user' }// OR // { return Promise;} const app = ;const auth = secret: 'SECRET' getter: userGetter /* options */; moduleexports = auth;
express entry point
/// require ... ///const auth = ;app; const routeA = ;const routeB = ;const routeC = ; app; app;app;app;app; app; // catch errors that are instance of AuthenticationError
Note that auth.user
and auth.admin
are just aliases of auth.hasRole('user')
and auth.hasRole('admin')
.
RouteA.js
/// require ... ///const auth = router; moduleexports = router;