Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let connection = require('./connect.js')
- let userModel = require('./userModel.js')
- let crypto = require('crypto'),
- algorithm = 'aes-256-ctr',
- password = 'DOIJZDOIJZAOIDJAZOIDJAZODIjzDONZAOIDJZACoiaoicaziodnaoiznioOINDAOINOIZND';
- let base64url = require('base64url')
- let moment = require('moment');
- let async = require('async')
- class User {
- constructor(user) {
- this.user = user
- }
- get username() {
- return this.user.trueName;
- }
- get email() {
- return this.user.email;
- }
- get created_at() {
- return moment(this.user.created_at);
- }
- static log(req, cb) {
- let self = this;
- let clearname = req.body.username.toLowerCase();
- userModel.findOne({username: clearname}, function (err, user) {
- if (!req.body.password) {
- req.flash('signinError', "Enter a valid password");
- cb(null);
- return;
- }
- if (!/^.{4,15}$/.test(req.body.password)) {
- req.flash('signinError', "Enter a valid password");
- cb(null);
- return;
- }
- if (!user) {
- req.flash('signinError', "User not found");
- cb(null);
- return;
- }
- if (user.password != User.encrypt(req.body.password)) {
- req.flash('signinError', "Password not matching");
- cb(null);
- return;
- }
- if (!user.confirmed) {
- req.flash('signinError', "Account disabled, check your mail");
- cb(null);
- return;
- }
- if (req.body.remember !== undefined) {
- if (req.body.remember == 'on') {
- req.session.cookie.expires = false; // Cookie expires at end of session
- }
- } else {
- req.session.cookie.maxAge = 30 * 60 * 1000; // Cookie expires after 30 min
- }
- req.logIn(user, function (err) {
- if (err) throw(err);
- cb(user);
- });
- });
- }
- static parseUserCreation(req) {
- if (!this.parseUsername(req.body.username, req, 'signupError'))
- return false;
- if (!this.parsePassword(req.body.password, req.body.password2, req, 'signupError'))
- return false;
- if (!this.parseEmail(req.body.email, req, 'signupError'))
- return false;
- return true;
- }
- static create(req, cb) {
- let self = this;
- if (!this.parseUserCreation(req)) {
- cb(null)
- } else {
- async.waterfall([
- function (callback) {
- //Check username
- userModel.findOne({username: req.body.username}, function (err, user) {
- if (err) callback(err);
- callback(null, user);
- })
- },
- function (usr, callback) {
- //Check email
- if (usr) {
- req.flash('signupError', 'Username already taken')
- callback(null)
- } else {
- userModel.findOne({email: req.body.email}, function (err, user) {
- if (err) callback(err);
- callback(null, user);
- })
- }
- }], function (err, result) {
- if (result) {
- req.flash('signupError', "Email already taken")
- cb(null);
- } else {
- let newUser = new userModel({
- username: req.body.username,
- trueName: req.body.username,
- firstName: req.body.firstName,
- lastName: req.body.lastName,
- email: req.body.email,
- password: self.encrypt(req.body.password),
- created_at: Date.now(),
- token: self.randomString(50),
- });
- newUser.save(function (err, obj) {
- if (err) throw err;
- cb(obj);
- })
- }
- }
- )
- }
- }
- static modify(id, key, value, cb) {
- console.log('Id : ' + id);
- userModel.findOne({id: id}, function (obj) {
- console.log('User : ' + obj);
- });
- }
- static encrypt(text) {
- let cipher = crypto.createCipher(algorithm, password);
- let crypted = cipher.update(text, 'utf8', 'hex');
- crypted += cipher.final('hex');
- return crypted;
- }
- static decrypt(text) {
- let decipher = crypto.createDecipher(algorithm, password);
- let dec = decipher.update(text, 'hex', 'utf8');
- dec += decipher.final('utf8');
- return dec;
- }
- static randomString(size) {
- return base64url(crypto.randomBytes(size));
- }
- //Parsing
- static parseUsername(username, req, errorName) {
- if (!username) {
- req.flash(errorName, 'Enter a valid username');
- return false;
- }
- if (username.length < 4 || username.length > 15) {
- req.flash(errorName, 'Enter a username greater than 3 characters and lower than 15');
- return false;
- }
- let pattern = /^[a-zA-Z0-9_-]{4,15}$/;
- if (!pattern.test(username)) {
- req.flash(errorName, 'Enter a valid username');
- return false;
- }
- return true;
- };
- static parseEmail(email, req, errorName) {
- if (!email) {
- req.flash(errorName, 'Enter a valid email');
- return false;
- }
- let pattern = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
- if (!pattern.test(email)) {
- req.flash(errorName, 'Enter a valid email');
- return false;
- }
- return true;
- }
- static parsePassword(pass, pass2, req, errorName) {
- if (!pass || !pass2) {
- req.flash(errorName, 'Enter a valid password');
- return false;
- }
- let pattern = /^.{4,15}$/;
- if (!pattern.test(pass)) {
- req.flash(errorName, 'Enter a valid password');
- return false;
- }
- if (pass != pass2) {
- req.flash(errorName, "Passwords doesn't match");
- return false;
- }
- return true;
- }
- }
- module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement