From 0d606489bf96cdce0fbbc24c3d848281091fc402 Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Thu, 9 Aug 2018 12:39:44 +0800 Subject: [PATCH] added login, follow the readme --- db.json | 22 ----------------- package.json | 4 +++- readme.md | 12 +++++++++- server.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ users.json | 16 +++++++++++++ 5 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 server.js create mode 100644 users.json diff --git a/db.json b/db.json index e11544a..bdbd9de 100644 --- a/db.json +++ b/db.json @@ -1,26 +1,4 @@ { - "users": [ - { - "id": 1, - "phone": "01234567891", - "password": "123456789", - "token": "abcd1234token" - } - ], - "login": [ - { - "id": 1, - "phone": "01234567891", - "password": "123456789", - "token": "abcd1234token" - }, - { - "id": 2 - }, - { - "id": 3 - } - ], "comments": [ { "id": 1, diff --git a/package.json b/package.json index 4aa83c9..f4c42cc 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "JSON api mock server for IZYIM module", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "json-server --watch ./db.json", + "start-auth": "node server.js" }, "repository": { "type": "git", diff --git a/readme.md b/readme.md index ad25894..ddf64c9 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,11 @@ -# JSON Server for IZYIM \ No newline at end of file +# JSON Server for IZYIM + +## Login + +```bash +$ npm run api-auth +``` + +Follow [this link](https://www.techiediaries.com/fake-api-jwt-json-server/) [or this link](https://github.com/techiediaries/fake-api-jwt-json-server) for further details + +Use x-www-form-urlencoded in Postman, user details are in users.json \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..7e92ad6 --- /dev/null +++ b/server.js @@ -0,0 +1,67 @@ +const fs = require('fs'); +const bodyParser = require('body-parser'); +const jsonServer = require('json-server'); +const jwt = require('jsonwebtoken'); + +const server = jsonServer.create(); + +const router = jsonServer.router('./db.json'); + +const userdb = JSON.parse(fs.readFileSync('./users.json', 'UTF-8')); + +server.use(bodyParser.urlencoded({extended: true})) +server.use(bodyParser.json()) +//server.use(jsonServer.defaults()); + +const SECRET_KEY = '123456789'; +const expiresIn = '1h'; + +// Create a token from a payload +function createToken(payload){ + return jwt.sign(payload, SECRET_KEY, {expiresIn}); + } + +// Verify the token +function verifyToken(token){ + return jwt.verify(token, SECRET_KEY, (err, decode) => decode !== undefined ? decode : err); + } + + // Check if the user exists in database +function isAuthenticated({phone, password}){ + return userdb.users.findIndex(user => user.phone === phone && user.password === password) !== -1; + } + + server.post('/auth/login', (req, res) => { + const {phone, password} = req.body; + if (isAuthenticated({phone, password}) === false) { + const status = 401; + const message = 'Incorrect phone or password'; + res.status(status).json({status, message}); + return; + } + const access_token = createToken({phone, password}); + res.status(200).json({access_token}); + }); + + server.use(/^(?!\/auth).*$/, (req, res, next) => { + if (req.headers.authorization === undefined || req.headers.authorization.split(' ')[0] !== 'Bearer') { + const status = 401; + const message = 'Bad authorization header'; + res.status(status).json({status, message}); + return; + } + try { + verifyToken(req.headers.authorization.split(' ')[1]); + next(); + } catch (err) { + const status = 401; + const message = 'Error: access_token is not valid'; + res.status(status).json({status, message}); + } + }); + + server.use(router); + + server.listen(3000, () => { + console.log('Run Auth API Server'); + }) \ No newline at end of file diff --git a/users.json b/users.json new file mode 100644 index 0000000..9f1892a --- /dev/null +++ b/users.json @@ -0,0 +1,16 @@ +{ + "users": [ + { + "id": 1, + "phone": "01234567891", + "password": "123456789", + "token": "abcd1234token" + }, + { + "id": 2, + "phone": "01234567892", + "password": "123456789", + "token": "wxyz9876token" + } + ] +} \ No newline at end of file