# hapi-jwt-couch-lib

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

Latest version **1.1.4** (published 2020-06-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install hapi-jwt-couch-lib
pnpm add hapi-jwt-couch-lib
yarn add hapi-jwt-couch-lib
bun add hapi-jwt-couch-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2020-06-10 |
| First published | 2017-10-09 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 24.1 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 16 |
| Author | juanprietob@gmail.com |
| Maintainers | juanprietob |
| Keywords | Hapi, JWT, couchdb, auth |

## Links

- npm: https://www.npmjs.com/package/hapi-jwt-couch-lib
- Repository: https://github.com/juanprietob/clusterpost
- Homepage: https://github.com/juanprietob/clusterpost#readme
- Issues: https://github.com/juanprietob/clusterpost/issues
- npm.io page: https://npm.io/package/hapi-jwt-couch-lib

## Dependencies (6)

- [prompt](https://npm.io/package/prompt.md) ^1.0.0
- [request](https://npm.io/package/request.md) ^2.88.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.1
- [@hapi/joi](https://npm.io/package/@hapi/joi.md) ^15.0.3
- [underscore](https://npm.io/package/underscore.md) ^1.8.3
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.0.1

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.1.4 (latest) — 2020-06-10
- 1.3.0 — 2019-07-31
- 1.2.1 — 2019-07-30
- 1.2.0 — 2019-07-30
- 1.1.3 — 2019-05-19
- 1.1.2 — 2018-08-03
- 1.1.1 — 2018-04-23
- 1.1.0 — 2018-04-19
- 1.0.1 — 2017-10-09

## README

# hapi-jwt-couch

Hapi plugin to validate users using [hapi-auth-jwt](https://github.com/ryanfitz/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](https://github.com/nodemailer/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@email.com', '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@email.com',
		password: 'password'
	});
----

#### resetPassword

Sends an email to recover the password with a link

----
	hapijwtcouch.resetPassword({email: 'email@email.com'}).
	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@email.com', 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@email.com',
		// 		  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
----

---
_Source: https://npm.io/package/hapi-jwt-couch-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
