From 2e32f06cfe0e90086acb8b476df6039d7cbbdb11 Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Sat, 25 Aug 2018 10:41:02 +0800 Subject: [PATCH] added routing for updating user profile --- server.js | 37 +++++++++++++++++++++++++++++++++++-- users.json | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 987e348..5ff4ef3 100644 --- a/server.js +++ b/server.js @@ -32,6 +32,7 @@ function isAuthenticated({phone, password}){ } // check if the number already exists in database +//this function's implementation is commented out for the time being because of the token mismatch issue, this will be implemanted later on. function isInDatabase({phone}){ return userdb.users.findIndex(user => user.phone === phone) !== -1; } @@ -56,6 +57,16 @@ function verificationCode(){ // //return userdb.users.reduce((max, b) => Math.max(max, b.id),userdb.users[0].id); // } +//Function to find a specific id in users.json file +//This function is depricted for the time being, in case we plan to use it in any way +function findUserId(data, id){ + for (var i = 0; i { const {phone, password} = req.body; if (isAuthenticated({phone, password}) === false) { @@ -69,10 +80,10 @@ function verificationCode(){ }); server.post('/send-verification', (req, res)=>{ - const {phone, password} = req.body; + const {phone} = req.body; const id = userdb.users.length + 1; //if(isInDatabase({phone} === false)){ - userdb.users.push({id: id, phone: phone, password: password}); + userdb.users.push({id: id, phone: phone, password: null, name: null, role: null}); fs.writeFile('users.json', JSON.stringify(userdb), function(err){ if(err) throw err; @@ -89,6 +100,28 @@ function verificationCode(){ res.status(200).json({verification_code}); }); + server.post('/update-user', (req, res)=>{ + const {name, role, password, id} = req.body; + //var i = findUserId(userdb.users, id); + for (var i = 0; i { if (req.headers.authorization === undefined || req.headers.authorization.split(' ')[0] !== 'Bearer') { const status = 401; diff --git a/users.json b/users.json index 4adce21..7af00da 100644 --- a/users.json +++ b/users.json @@ -1 +1 @@ -{"users":[{"id":1,"phone":"01234567891","password":"123456789"},{"id":2,"phone":"01234567892","password":"123456789"},{"id":3,"phone":"01234567893","password":"123456789"}]} \ No newline at end of file +{"users":[{"id":1,"phone":"01234567891","password":"123456789","name":"Forth User","role":"1"},{"id":2,"phone":"01234567892","password":"123456789"},{"id":3,"phone":"01234567893","password":"123456789"},{"id":4,"phone":"01234567894","password":"123456789","name":"Forth User","role":"1"}]} \ No newline at end of file