Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let connection = require('./connect.js')
  2. let userModel = require('./userModel.js')
  3. let crypto = require('crypto'),
  4.     algorithm = 'aes-256-ctr',
  5.     password = 'DOIJZDOIJZAOIDJAZOIDJAZODIjzDONZAOIDJZACoiaoicaziodnaoiznioOINDAOINOIZND';
  6. let base64url = require('base64url')
  7. let moment = require('moment');
  8.  
  9. let async = require('async')
  10.  
  11. class User {
  12.     constructor(user) {
  13.         this.user = user
  14.     }
  15.  
  16.     get username() {
  17.         return this.user.trueName;
  18.     }
  19.  
  20.     get email() {
  21.         return this.user.email;
  22.     }
  23.  
  24.     get created_at() {
  25.         return moment(this.user.created_at);
  26.     }
  27.  
  28.     static log(req, cb) {
  29.         let self = this;
  30.         let clearname = req.body.username.toLowerCase();
  31.         userModel.findOne({username: clearname}, function (err, user) {
  32.             if (!req.body.password) {
  33.                 req.flash('signinError', "Enter a valid password");
  34.                 cb(null);
  35.                 return;
  36.             }
  37.             if (!/^.{4,15}$/.test(req.body.password)) {
  38.                 req.flash('signinError', "Enter a valid password");
  39.                 cb(null);
  40.                 return;
  41.             }
  42.             if (!user) {
  43.                 req.flash('signinError', "User not found");
  44.                 cb(null);
  45.                 return;
  46.             }
  47.             if (user.password != User.encrypt(req.body.password)) {
  48.                 req.flash('signinError', "Password not matching");
  49.                 cb(null);
  50.                 return;
  51.             }
  52.             if (!user.confirmed) {
  53.                 req.flash('signinError', "Account disabled, check your mail");
  54.                 cb(null);
  55.                 return;
  56.             }
  57.             if (req.body.remember !== undefined) {
  58.                 if (req.body.remember == 'on') {
  59.                     req.session.cookie.expires = false; // Cookie expires at end of session
  60.                 }
  61.             } else {
  62.                 req.session.cookie.maxAge = 30 * 60 * 1000; // Cookie expires after 30 min
  63.             }
  64.  
  65.             req.logIn(user, function (err) {
  66.                 if (err) throw(err);
  67.                 cb(user);
  68.             });
  69.  
  70.         });
  71.     }
  72.  
  73.     static parseUserCreation(req) {
  74.         if (!this.parseUsername(req.body.username, req, 'signupError'))
  75.             return false;
  76.         if (!this.parsePassword(req.body.password, req.body.password2, req, 'signupError'))
  77.             return false;
  78.         if (!this.parseEmail(req.body.email, req, 'signupError'))
  79.             return false;
  80.         return true;
  81.     }
  82.  
  83.     static create(req, cb) {
  84.         let self = this;
  85.  
  86.         if (!this.parseUserCreation(req)) {
  87.             cb(null)
  88.         } else {
  89.             async.waterfall([
  90.                     function (callback) {
  91.                         //Check username
  92.                         userModel.findOne({username: req.body.username}, function (err, user) {
  93.                             if (err) callback(err);
  94.                             callback(null, user);
  95.                         })
  96.                     },
  97.                     function (usr, callback) {
  98.                         //Check email
  99.                         if (usr) {
  100.                             req.flash('signupError', 'Username already taken')
  101.                             callback(null)
  102.                         } else {
  103.                             userModel.findOne({email: req.body.email}, function (err, user) {
  104.                                 if (err) callback(err);
  105.                                 callback(null, user);
  106.                             })
  107.                         }
  108.                     }], function (err, result) {
  109.                     if (result) {
  110.                         req.flash('signupError', "Email already taken")
  111.                         cb(null);
  112.                     } else {
  113.                         let newUser = new userModel({
  114.                             username: req.body.username,
  115.                             trueName: req.body.username,
  116.                             firstName: req.body.firstName,
  117.                             lastName: req.body.lastName,
  118.                             email: req.body.email,
  119.                             password: self.encrypt(req.body.password),
  120.                             created_at: Date.now(),
  121.                             token: self.randomString(50),
  122.                         });
  123.                         newUser.save(function (err, obj) {
  124.                             if (err) throw err;
  125.                             cb(obj);
  126.                         })
  127.                     }
  128.                 }
  129.             )
  130.         }
  131.     }
  132.  
  133.     static modify(id, key, value, cb) {
  134.         console.log('Id : ' + id);
  135.         userModel.findOne({id: id}, function (obj) {
  136.             console.log('User : ' + obj);
  137.  
  138.         });
  139.     }
  140.  
  141.     static encrypt(text) {
  142.         let cipher = crypto.createCipher(algorithm, password);
  143.         let crypted = cipher.update(text, 'utf8', 'hex');
  144.         crypted += cipher.final('hex');
  145.         return crypted;
  146.     }
  147.  
  148.     static decrypt(text) {
  149.         let decipher = crypto.createDecipher(algorithm, password);
  150.         let dec = decipher.update(text, 'hex', 'utf8');
  151.         dec += decipher.final('utf8');
  152.         return dec;
  153.     }
  154.  
  155.     static randomString(size) {
  156.         return base64url(crypto.randomBytes(size));
  157.     }
  158.  
  159.     //Parsing
  160.     static parseUsername(username, req, errorName) {
  161.         if (!username) {
  162.             req.flash(errorName, 'Enter a valid username');
  163.             return false;
  164.         }
  165.         if (username.length < 4 || username.length > 15) {
  166.             req.flash(errorName, 'Enter a username greater than 3 characters and lower than 15');
  167.             return false;
  168.         }
  169.  
  170.         let pattern = /^[a-zA-Z0-9_-]{4,15}$/;
  171.         if (!pattern.test(username)) {
  172.             req.flash(errorName, 'Enter a valid username');
  173.             return false;
  174.         }
  175.         return true;
  176.     };
  177.  
  178.     static parseEmail(email, req, errorName) {
  179.         if (!email) {
  180.             req.flash(errorName, 'Enter a valid email');
  181.             return false;
  182.         }
  183.         let pattern = /^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
  184.         if (!pattern.test(email)) {
  185.             req.flash(errorName, 'Enter a valid email');
  186.             return false;
  187.         }
  188.         return true;
  189.     }
  190.  
  191.     static parsePassword(pass, pass2, req, errorName) {
  192.         if (!pass || !pass2) {
  193.             req.flash(errorName, 'Enter a valid password');
  194.             return false;
  195.         }
  196.  
  197.         let pattern = /^.{4,15}$/;
  198.         if (!pattern.test(pass)) {
  199.             req.flash(errorName, 'Enter a valid password');
  200.             return false;
  201.         }
  202.         if (pass != pass2) {
  203.             req.flash(errorName, "Passwords doesn't match");
  204.             return false;
  205.         }
  206.         return true;
  207.     }
  208. }
  209.  
  210. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement