This sample project demonstrates how to set up a user authentication API with Hapi.js using JSON Web Tokens. There are several endpoints exposed in the sample, including user login and signup, along with an example of a protected instructors
resource.
The API for the course is served at https://user-authentication-api.now.sh/api
Clone the repo, then:
npm install
node server.js
The app will be served at localhost:3001
.
To setup the API locally, you will need to run MongoDB or have an MLab instance. Create a .env
file and populate it with the following values:
SECRET_KEY=<secret_key>
MLAB_USER=<mlab_user>
MLAB_PASSWORD=<secret_key>
MLAB_DOMAIN=<domain>
MLAB_DB=<database>
- Used for signing up a user. Accepts
username
,email
, andpassword
to create a user. Returns a JWT.
- Used for logging a user in. Accepts
username
oremail
andpassword
to authenticate a user. Returns a JWT.
- Returns all users in the database. Requires a valid JWT with an
admin
scope.
- Updates a user. Requires a valid JWT with an
admin
scope.
- Returns all
instructors
in the database. Requires a valid JWT.
- Returns a specific
instructor
in the database. Requires a valid JWT.
- Saves a new
instructor
in the database. Requires a valid JWT with anadmin
scope.
- Deletes an instructor with a specific
id
. Requires a valid JWT with anadmin
scope.
- Checks whether a user already exists or not. Useful for doing async form validation.