Build a REST API(s) from JSON file with Authentication

Suman Kunwar
Feb 9 · 2 min read
npm install -g hai-server
{"posts": [{ "id": 1, "title": "hai-server", "author": "sumn2u" }],"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],"profile": { "name": "sumn2u" }}
hai-server --watch db.json
{ "id": 1, "title": "hai-server", "author": "sumn2u" }

How to restrict access to an API?

{"secretKey":"123456789","expiresIn": "1h","users": [{"id": 1,"name": "bruno","email": "bruno@email.com","password": "bruno"},{"id": 2,"name": "techie","email": "techie@email.com","password": "techie"},{"id": 3,"name": "nilson","email": "nilson@email.com","password": "nilson"}],"authenticationURL":"/auth/login","authenticatedURL":["/comments"]}
hai-server --watch db.json --auth auth.json --port 3000

How to login?

POST http://localhost:3000/auth/login
{
"email": "nilson@email.com",
"password":"nilson"
}
{
"access_token": "<ACCESS_TOKEN>"
}
Authorization: Bearer <ACCESS_TOKEN>

Conclusion

Suman Kunwar

Written by

Javascript & Open Source Enthusiast, Adventure Lover

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade