mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi.git
synced 2026-08-22 22:14:30 +00:00
Merge branch 'feature/login' into 'master'
Feature/login See merge request CIEFWorldwideSdnBhd/izyim-mockapi!1
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/node_modules
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"comments": [
|
||||
{
|
||||
"id": 1,
|
||||
"body": "some comment",
|
||||
"postId": 1
|
||||
}
|
||||
],
|
||||
"profile": {
|
||||
"name": "typicode"
|
||||
}
|
||||
}
|
||||
Generated
+1795
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "izyim-mockapi",
|
||||
"version": "1.0.0",
|
||||
"description": "JSON api mock server for IZYIM module",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "json-server --watch ./db.json",
|
||||
"start-auth": "node server.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi.git"
|
||||
},
|
||||
"keywords": [
|
||||
"IZYIM",
|
||||
"JSON",
|
||||
"json-server"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi#README",
|
||||
"dependencies": {
|
||||
"json-server": "^0.14.0",
|
||||
"jsonwebtoken": "^8.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
# JSON Server for IZYIM
|
||||
|
||||
## Getting Started
|
||||
We will be using JSON Server + JWT Auth for this project's login functionality. (Later further updates will be added.)
|
||||
|
||||
## Prerequisites
|
||||
- [Node.js & NPM](https://nodejs.org/en/download/).
|
||||
- [Postman Software](https://www.getpostman.com/) to test the API.
|
||||
|
||||
### Installation
|
||||
|
||||
Run the following command first.
|
||||
|
||||
```bash
|
||||
$ npm install
|
||||
$ npm run start-auth
|
||||
```
|
||||
|
||||
### Usage
|
||||
On Postman, navigate to http://localhost:3000/auth/login and use x-www-form-urlencoded/JSON with 'phone' and 'password' fields.
|
||||
|
||||
User details can be found in users.json.
|
||||
|
||||
Follow [this](https://www.techiediaries.com/fake-api-jwt-json-server/) or [this](https://github.com/techiediaries/fake-api-jwt-json-server) for further details.
|
||||
@@ -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');
|
||||
})
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"users": [
|
||||
{
|
||||
"id": 1,
|
||||
"phone": "01234567891",
|
||||
"password": "123456789",
|
||||
"token": "abcd1234token"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"phone": "01234567892",
|
||||
"password": "123456789",
|
||||
"token": "wxyz9876token"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user