added func to add users to users.json and generating verification codes

This commit is contained in:
Aqeeb Imtiaz Harun
2018-08-14 13:15:21 +08:00
parent 5144ac52eb
commit 16ff0a7833
4 changed files with 2788 additions and 17 deletions
+2755
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -25,6 +25,7 @@
"homepage": "https://gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi#README",
"dependencies": {
"json-server": "^0.14.0",
"jsonwebtoken": "^8.3.0"
"jsonwebtoken": "^8.3.0",
"npm": "^6.3.0"
}
}
+30
View File
@@ -31,6 +31,18 @@ function isAuthenticated({phone, password}){
return userdb.users.findIndex(user => user.phone === phone && user.password === password) !== -1;
}
// check if the number already exists in database
function isInDatabase({phone}){
return userdb.users.findIndex(user => user.phone === phone) !== -1;
}
//create verification code for phone number
function verificationCode(){
var val = Math.floor(1000 + Math.random() * 9000);
console.log(val);
return val;
}
server.post('/auth/login', (req, res) => {
const {phone, password} = req.body;
if (isAuthenticated({phone, password}) === false) {
@@ -43,6 +55,24 @@ function isAuthenticated({phone, password}){
res.status(200).json({access_token});
});
server.post('/send-verification', (req, res)=>{
const {phone, password} = req.body;
//if(isInDatabase({phone} === false)){
userdb.users.push({phone: phone, password: password});
fs.writeFile('users.json', JSON.stringify(userdb), function(err){if(err) throw err;
console.log('added to json');
});
//}
// else{
// console.log('number exists');
// const status = 409;
// const message = 'Number already exists';
// res.status(status).json({status, message});
// }
const verification_code = verificationCode();
res.status(200).json({verification_code});
});
server.use(/^(?!\/auth).*$/, (req, res, next) => {
if (req.headers.authorization === undefined || req.headers.authorization.split(' ')[0] !== 'Bearer') {
const status = 401;
+1 -16
View File
@@ -1,16 +1 @@
{
"users": [
{
"id": 1,
"phone": "01234567891",
"password": "123456789",
"token": "abcd1234token"
},
{
"id": 2,
"phone": "01234567892",
"password": "123456789",
"token": "wxyz9876token"
}
]
}
{"users":[{"id":1,"phone":"01234567891","password":"123456789"},{"id":2,"phone":"01234567892","password":"123456789"},{"phone":"01234567895","password":"123456789"},{"phone":"01234567894","password":"123456789"}]}