mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim-mockapi.git
synced 2026-08-19 04:24:09 +00:00
added func to add users to users.json and generating verification codes
This commit is contained in:
Generated
+2755
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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"}]}
|
||||
Reference in New Issue
Block a user