npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-jwt-couch-lib

v1.1.4

Published

API implementation to send requests to the hapi server and authenticate users, create, delete, etc.

Downloads

12

Readme

hapi-jwt-couch

Hapi plugin to validate users using hapi-auth-jwt, storing user information and encrypted passwords in a couchdb instance.

This plugin also provides a 'recover my password' option by setting up an email account using nodemailer.

Edit the "message" portion of the configuration. The strings @USERNAME@, @SERVER@ and @TOKEN@ are replaced before sending the email.

Usage


npm install hapi-jwt-couch-lib

Usage and available functions implemented with promises


var hapijwtcouch = require("hapi-jwt-couch-lib");

setServer

Set the server uri ex.


hapijwtcouch.setServer('http://localhost:9090');

getServer

Returns the server uri


var serveruri = hapijwtcouch.getServer();
console.log(serveruri);// http://localhost:9090

setAgentOptions

Set the request object with specific options documentation at https://www.npmjs.com/package/request


//When using an https connection and you don't want to set the certificate. 
var agentoptions = {
	rejectUnauthorized: false
}
hapijwtcouch.setAgentOptions(agentoptions);

promptUsernamePassword()

Use a terminal to prompt for user name and password


hapijwtcouch.promptUsernamePassword()
.then(function(user){
	console.log(user);//{'email': '[email protected]', 'password': 'password'}
});

promptServer

Use the terminal to prompt for server URI


hapijwtcouch.promptServer()
.then(function(server){
	console.log(server);//{'uri': 'http://localhost:9090'}
});

start

Function to prompt for username, password and server. It will login the user as well. After login, you may execute any other operation.


hapijwtcouch.start()
.then(function(res){
	console.log(res);//{'token': 'usertoken'}
});

setUserToken

Set the user authentication token to use the request library


hapijwtcouch.setUserToken({'token': 'sometoken'});

getUserToken

Gets the user token


var token = hapijwtcouch.getUserToken(); 
console.log(token);//{'token': 'sometoken'}

createUser

Creates a user in the database


hapijwtcouch.createUser({
    name: 'name',
	userEmail: '[email protected]',
	password: 'password'
});

resetPassword

Sends an email to recover the password with a link


hapijwtcouch.resetPassword({email: '[email protected]'}).
then(function(response){
	console.log(response);//'An email has been sent to recover your password.'
});

userLogin

Logs in the user and returns a token


hapijwtcouch.userLogin({email: '[email protected]', password: 'somepassword'}).
then(function(response){
	console.log(response);//{token: 'JWTusertoken'}
});

getUser

Gets the user currently logged in the application.


hapijwtcouch.getUser().
then(function(user){
	console.log(user);
	//		{ _id: 'b5b17dc4968b2382a4f778e8c600049a',
	// 		  _rev: '4-55f9ec8dfcf949155ed8ece07bc2a388',
	// 		  name: 'User name',
	// 		  email: '[email protected]',
	// 		  type: 'user',
	// 		  scope: [ 'default', 'admin' ] 
	// 		}
	//You can add any additional field to the user document, the shown above are default ones
});

getUsers

Gets all the users in the database uses admin scope


//Must be admin
hapijwtcouch.getUsers().
then(function(response){
	console.log(response);//array of users
});

updateUser

Update basic user information


//we are going to update the user information by adding the field projects

hapijwtcouch.getUser()
.then(function(user){
	user.projects = [{"somenewstuff": "new"}];
	//NOTE: The field scope will be deleted from this update. The admin user is the only one that 
	//can modify the 'scope' of the user. Use the function 'updateUsers' to modify the scope.
	return hapijwtcouch.updateUser(user);
})
.then(function(response){
	console.log(response);
});

updateUsers

The user must have admin scope.


//we are going to make another user admin

hapijwtcouch.getUsers()
.then(function(users){
	var user = users[0]; //Find user in array
	user.scope = ['default', 'admin'];		
	return hapijwtcouch.updateUser(user);
})
.then(function(response){
	console.log(response);
});

deleteUser

Deletes self from the DB


hapijwtcouch.deleteUser()
.then(function(response){
	console.log(response);
});

deleteUsers

Deletes a user from the DB, must be admin


hapijwtcouch.deleteUsers(user)
.then(function(response){
	console.log(response);
});

Testing

Start the test server


node testserver.js

Run all tests


npm test