Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Created October 29, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sysetup/d58a8e62e3e59b1201b59917cc42291b to your computer and use it in GitHub Desktop.
Save Sysetup/d58a8e62e3e59b1201b59917cc42291b to your computer and use it in GitHub Desktop.
Insert a new register to a JSON file with NodeJS.
'use restrict';
/**
* Local module dependences.
*/
var db = require('./db');
/**
* Function that get all data entered in the signup form, check if user exist calling db.engine.findByUsername() function. Return the properly messages to the view according if user exist and password match.
*
* Return the username and email typed to be put in the input-text fields again in case the password do match.
*
* If the register is successfuly redirect the user to login page. If the user is already registered before redirect him to login page sending the message: 'You had already registered before, please login.'
*/
var postSignup = function (req, res) {
var username = req.body.username;
var email = req.body.email;
var pwd = req.body.password;
var pwdconfirm = req.body.password_confirm;
db.engine.findByUsername(username, function (err, user) {
if (err) throw err;
if (!user) {
if (pwd == pwdconfirm) {
db.engine.createUser(req.body);
req.flash('messageSuccess', ['You are now registered, please login.']);
res.redirect('/login');
} else {
req.flash('message', ['Passwords don\'t match.', username, email]);
res.redirect('/signup');
}
} else if (user.username == username) {
req.flash('message', ['The user \'' +username+ '\' has been already registered before.']);
res.redirect('/signup');
}
});
};
module.exports = {
postSignup: postSignup,
};
'use strict';
/**
* Local module dependences.
*/
var engine = require('./engine');
var users = require('./data/users');
module.exports = {
users: users,
engine: engine
};
'use strict';
/**
* Module dependences.
*/
var path = require('path');
var fs = require('fs');
const bcrypt = require('bcrypt');
var file = path.join(__dirname, 'data/users.json'); //Get file to edit: users.json
var users;
/**
* Read the file got and parse it to JSON format.
*/
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
users = JSON.parse(data);
});
/**
*
* @param {username string} username
* @param {callback} done
*/
var findByUsername = function (username, done) {
process.nextTick(function () {
for (var i = 0, len = users.length; i < len; i++) {
var record = users[i];
if (record.username === username) {
return done(null, record);
}
}
return done(null, null);
});
};
/**
* Create the user form signup form.
*/
var createUser = function (user) {
var station = users.length; //Get the number of user registered in the file.
var usersjson;
encriptPwd(user.password, function (encripted) {
users[station] = {}; //Create the new object to storage the data from the new user in the position get from station var.
users[station].id = '' + station; //Set the id for the new user.
users[station].status = 'active';
users[station].role = 'member';
users[station].username = user.username;
users[station].password = encripted;
users[station].displayName = 'Member';
users[station].emails = {};
users[station].emails.email1 = user.email;
users[station].created = new Date();
users[station].updated = '';
usersjson = JSON.stringify(users, null, ' '); //Convert users object to json format.
/**
* Write the new user set to users.json file.
*/
fs.writeFile(file, usersjson, 'utf8', function (err) {
if (err) throw err;
});
});
};
/**
* Password encription. Using bcrypt module hash function.
*/
var encriptPwd = function (pwd, cb) {
bcrypt.hash(pwd, 10, function (err, hash) {
if (err) throw err;
cb(hash);
});
};
module.exports = {
findByUsername: findByUsername,
createUser: createUser,
};
[
{
"id": "0",
"status": "active",
"role": "admin",
"username": "admin",
"password": "$2a$10$AVDoabDNpyyjGg0zZv9ciRdz5ZQjej7C4QW0PHMia",
"displayName": "Admin",
"emails": {
"email1": "admin@sysetup.com"
},
"created": "3020-10-15T02:32:51.038Z",
"updated": ""
},
{ //This's the new register which will be created with all keys as the first one.
... ...
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment