From 49e7185ee8afc903967c08ac69892ca42f8542e2 Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:02:51 +0800 Subject: [PATCH 1/2] Update README.md --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a095a3..670a9af 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,55 @@ You should send this authorization with any request to the protected endpoints Authorization: Bearer ``` +Usage: + +Login: +``` + let userToken = null; + axios.post('http://localhost:8000/auth/login',{ + "email": "nilson@email.com", + "password":"nilson" + }).then(function (response) { + console.log (response.data); + userToken = response.data.access_token; + }).catch(function (error) { + console.log(error); + }); +``` + +Read: +``` + const config = { + headers: { + Authorization: "Bearer " + userToken + } + }; + axios.get('http://localhost:8000/products',config) + .then(function (response) { + console.log (response.data); + }).catch(function (error) { + console.log(error); + }); +``` + +List of APIs: + +| API | Usage | +| --- | --- | +| `POST /auth/login` | User Login | +| `POST /auth/register` | Register New User | +| `GET /products` | for getting the products | +| `GET /products/` | for getting a single product by id | +| `POST /products` | for creating a new product | +| `PUT /products/` | for updating a product by id | +| `PATCH /products/` | for partially updating a product by id | +| `DELETE /products/` | for deleting a product by id | + + Check out these tutorials: - [Mocking a REST API Back-End for Your Angular App with JSON-Server and Faker.js](https://www.techiediaries.com/angular-mock-backend) - [Building a Fake and JWT Protected REST API with json-server](https://www.techiediaries.com/fake-api-jwt-json-server) -- [Angular 9 Tutorial: Build an Example App with Angular CLI, Angular Router, HttpClient & Angular Material](https://www.shabang.dev/angular-tutorial-build-an-example-app-with-angular-cli-router-httpclient-and-angular-material/) From b22665831459ea8db7df6b0addcb9ac656be95d9 Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:04:01 +0800 Subject: [PATCH 2/2] Add files via upload --- package.json | 3 +-- server.js | 41 ++++++++++++++++++++++------------------- users.json | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 9615634..aa4bb42 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,7 @@ "author": "ME:)", "license": "ISC", "dependencies": { - "body-parser": "^1.19.0", - "json-server": "^0.14.2", + "json-server": "^0.16.3", "jsonwebtoken": "^8.1.0" } } diff --git a/server.js b/server.js index 97f16c0..4871771 100644 --- a/server.js +++ b/server.js @@ -1,23 +1,26 @@ -const fs = require('fs') -const bodyParser = require('body-parser') -const jsonServer = require('json-server') -const jwt = require('jsonwebtoken') +const jsonFileForUsers = 'users.json'; +const jsonFileForData = 'database.json'; -const server = jsonServer.create() -const router = jsonServer.router('./database.json') -const userdb = JSON.parse(fs.readFileSync('./users.json', 'UTF-8')) +const SECRET_KEY = 'ectawgwkpagm' // this is the secret key for the authorization +const expiresIn = '1h' // this is the expiration time for jwt token -server.use(bodyParser.urlencoded({extended: true})) -server.use(bodyParser.json()) -server.use(jsonServer.defaults()); +const fs = require('fs'); +const express = require('express'); +const jsonServer = require('json-server'); +const jwt = require('jsonwebtoken'); + +const server = jsonServer.create(); +const router = jsonServer.router(`./${jsonFileForData}`); +const userdb = JSON.parse(fs.readFileSync(`./${jsonFileForUsers}`, 'UTF-8')); -const SECRET_KEY = '123456789' +server.use(express.urlencoded({extended: true})); +server.use(express.json()); +server.use(jsonServer.defaults()); -const expiresIn = '1h' // Create a token from a payload function createToken(payload){ - return jwt.sign(payload, SECRET_KEY, {expiresIn}) + return jwt.sign(payload, SECRET_KEY, {expiresIn}); } // Verify the token @@ -43,7 +46,7 @@ server.post('/auth/register', (req, res) => { return } -fs.readFile("./users.json", (err, data) => { +fs.readFile(`./${jsonFileForUsers}`, (err, data) => { if (err) { const status = 401 const message = err @@ -52,14 +55,14 @@ fs.readFile("./users.json", (err, data) => { }; // Get current users data - var data = JSON.parse(data.toString()); + let dataObj = JSON.parse(data.toString()); // Get the id of last user - var last_item_id = data.users[data.users.length-1].id; + let last_item_id = dataObj.users[dataObj.users.length-1].id; //Add new user - data.users.push({id: last_item_id + 1, email: email, password: password}); //add some data - var writeData = fs.writeFile("./users.json", JSON.stringify(data), (err, result) => { // WRITE + dataObj.users.push({id: last_item_id + 1, email: email, password: password}); //add some data + fs.writeFile(`./${jsonFileForUsers}`, JSON.stringify(dataObj), (err, result) => { // WRITE if (err) { const status = 401 const message = err @@ -75,7 +78,7 @@ fs.readFile("./users.json", (err, data) => { res.status(200).json({access_token}) }) -// Login to one of the users from ./users.json +// User Login server.post('/auth/login', (req, res) => { console.log("login endpoint called; request body:"); console.log(req.body); diff --git a/users.json b/users.json index 7a0daf6..f18e697 100644 --- a/users.json +++ b/users.json @@ -1 +1,49 @@ -{"users":[{"id":1,"email":"bruno@email.com","password":"bruno"},{"id":2,"email":"techie@email.com","password":"techie"},{"id":3,"email":"nilson@email.com","password":"nilson"},{"id":4,"email":"nilson1@email.com","password":"nilson"},{"id":5,"email":"nilson2@email.com","password":"nilson"},{"id":6,"email":"nilson3@email.com","password":"nilson"},{"id":7,"email":"nilson4@email.com","password":"nilson"},{"id":8,"email":"nilson7@email.com","password":"nilson"},{"id":9,"email":"nilson8@email.com","password":"nilson"}]} \ No newline at end of file +{ + "users":[ + { + "id":1, + "email":"bruno@email.com", + "password":"bruno" + }, + { + "id":2, + "email":"techie@email.com", + "password":"techie" + }, + { + "id":3, + "email":"nilson@email.com", + "password":"nilson" + }, + { + "id":4, + "email":"nilson1@email.com", + "password":"nilson" + }, + { + "id":5, + "email":"nilson2@email.com", + "password":"nilson" + }, + { + "id":6, + "email":"nilson3@email.com", + "password":"nilson" + }, + { + "id":7, + "email":"nilson4@email.com", + "password":"nilson" + }, + { + "id":8, + "email":"nilson7@email.com", + "password":"nilson" + }, + { + "id":9, + "email":"nilson8@email.com", + "password":"nilson" + } + ] +} \ No newline at end of file