🔐 A boilerplate application with JWT Authentication and Authorization strategies backed by Hapi and PostgreSQL.
Description • How To Use • Database Models • Scopes • Users • Endpoints • Plugins • Environment Variables
This project shows how to implement a consistent authentication and authorization system. There are a few endopoints, each one with a different authorization.
Use the Database Dump and the environment variable JWT_SECRET=MLH.gH)V#vj6m'J3 in order to execute the examples described below.
If you want to know more about how JWT works, please check the following links:
https://github.com/dwyl/hapi-auth-jwt2
https://github.com/dwyl/learn-json-web-tokens
https://jwt.io/introduction/
git clone
npm install
node index.js to run the server.
| Name | Type |
|---|---|
| id | UUID |
| username | VARCHAR |
| password | VARCHAR (ENCRYPTED) |
| created_at | TIMESTAMP WITH TIME ZONE |
| updated_at | TIMESTAMP WITH TIME ZONE |
| Name | Type |
|---|---|
| id | UUID |
| user_id | UUID |
| token | VARCHAR |
| revoked | BOOLEAN |
| created_at | TIMESTAMP WITH TIME ZONE |
| updated_at | TIMESTAMP WITH TIME ZONE |
| Name | Type |
|---|---|
| id | UUID |
| name | VARCHAR |
| created_at | TIMESTAMP WITH TIME ZONE |
| updated_at | TIMESTAMP WITH TIME ZONE |
| Name | Type |
|---|---|
| id | UUID |
| user_id | UUID |
| scope_id | UUID |
| created_at | TIMESTAMP WITH TIME ZONE |
| updated_at | TIMESTAMP WITH TIME ZONE |
You can download and use the Database Dump
- Admin: can access every section
- A: can only access section A
- B: can only access section B
- C: can only access section C
- Admin: can access every section
- user1: can only access section A and C
- user2: can only access section B
- user3: can only access section B
You can add as many scopes as you want and manage the permissions by adding or removing data from the user_scope table.
POST /users/login
POST /users/logout
POST /users/register
GET /users/sectionA
GET /users/sectionB
GET /users/sectionC
@hapi/boom
@hapi/glue
bcryptjs
bookshelf
jsonwebtoken
knex
pg
This plugin sets up the knex and bookshelf configurations.
This plugin takes care of the authentication and authorization events.
HOST
PORT
JWT_SECRET
DB_HOST
DB_NAME
DB_USER
DB_PASS