This repository contains an example of authenticated REST API. The REST API was developed using Restify and the authentication method uses JWT.
This app is not docker based, so you should install Node.js, in order to run the API server.
Node.js >= 10.x
- In the project folder, generate a pair of public and private keys. The pair will be used to sign and verify the JWT token.
openssl genrsa -out secret.key 1024
openssl rsa -in secret.key -outform PEM -pubout -out public.key
-
Run
npm install
to install required dependencies for this project; -
Create .env file and fill in the following env variables:
PORT=8080
JWT_AUDIENCE=<YOUR JWT AUDIENCE HERE>
JWT_ISSUER=<YOUR JWT ISSUER HERE>
- Run API using:
npm run start
or
node index.js
- Run tests using the following command:
npm run test
Sign in route
curl --header "Content-Type: application/json" \
--request POST \
--data '{"userId":1,"userEmail": "your email here", "userName": "your user name here"}' \
http://localhost:8080/login/signIn
Validate JWT token route
curl --header "Content-Type: application/json" \
--request POST \
--data '{"token": "your token here"}' \
http://localhost:8080/login/validateToken
Public route
curl http://localhost:8080/public
Private route
curl -H "Authorization: bearer <YOUR_JWT_HERE>" http://localhost:8080/private
- Include docker and docker-compose;
- Add database connection using some ORM or ODM;
- Include frontend example using a JS library (e.g. React.js).