Adding «CREATE» User Functionality

// routes/admin.js
const express = require('express');
const router = express.Router();
const utility = require('../utility');

router.get('/', function(req, res, next){
  if(req.session.user)
    res.redirect('/admin/dashboard');
  else
    res.redirect('/admin/login');
});

router.get('/login', function(req, res, next){
  const login = require('../controllers/login');
  login.getLogin(res);
});

router.post('/login', function(req, res, next){
  const login = require('../controllers/login');
  login.postLogin(req, res);
});

router.get('/dashboard', function(req, res, next){
  const index = require('../controllers/dashboard/index');
  index.getIndex(req, res);
});

router.get('/logout', function(req, res, next){
  if(req.session.user){
    req.session.destroy(function (err) {
      res.redirect('/');
    });  
  }else{
    res.redirect('/');
  }
});

router.get('/author', function(req, res, next){
  const author = require('../controllers/dashboard/author');
  author.getAuthor(req, res);
});

router.post('/author', function(req, res, next){
  const author = require('../controllers/dashboard/author')
  author.postAuthor(req, res);
});

module.exports = router;
// controllers/dashboard/author.js
class Author{
  constructor(){
    this.deepcopy = require('deepcopy');
    this.vdict = require('../../config');
    this.utility = require('../../utility');
    this.usersdb = require('../../models/usersdb');
    this.emailCheck = require('email-check');
  }

  getAuthor(req, res){
    if(req.session.user){
      const data = this.deepcopy(this.vdict);
      data.site_title = 'ទំព័រ​អ្នក​និពន្ធ';
      data.date = this.utility.setDate();
      res.render('dashboard/author', data);
    }else{
      res.redirect('/admin/login');
    }
  }

  postAuthor(req, res){
    const data = this.deepcopy(this.vdict);
    data.site_title = 'ទំព័រ​អ្នក​និពន្ធ';
    data.date = this.utility.setDate();

    if(req.session.user.role == 'Admin'){
      const self = this;
      this.usersdb.checkEmail(req, function(user){
        if(user){
          data.message = 'Email នេះ​មាន​គេ​ប្រើ​ប្រាស់​ហើយ​';
          res.render('dashboard/author', data);
        }else{
          self.emailCheck(req.body.email)
            .then(function (result) {
              if(result){
                self.usersdb.insertUser(req, function(user, err){
                  if(!err)
                    data.message = `អ្នក​និពន្ធ​ ${user.username} ត្រូវ​បានចុះ​បញ្ជី​រួច​ហើយ`;
                  else
                    data.message = err;

                  res.render('dashboard/author', data);
                });
              }
            })
            .catch(function (err) {
              data.message = 'Email នេះ​មិន​ត្រឹមត្រូវ​ទេ';
              res.render('dashboard/author', data);
            });
        }
      });
    }else{
      data.message = 'មាន​តែ Administrator ទេ ​ដែល​អាច​ចុះ​បញ្ជី​អ្នក​និពន្ធ​បាន​';
      res.render('dashboard/author', data);
    }
  }

}//end class

module.exports = new Author();
// models/usersdb.js
class Usersdb{
  constructor(){
    const mongoose = require('mongoose');
    const bcrypt = require('bcryptjs');

    const usersSchema = new mongoose.Schema({
      username: {type: String, required: true},
      password: {type: String, required: true},
      email: {type: String, required: true},
      role: {type: String, required: true},
      info: {type: String, required: true},
      date: {type: Date, required: true}
    });

    const users = mongoose.model('users', usersSchema);
    this.users = users;
    this.bcrypt = bcrypt;

    users.findOne(function (err, user){
      if (err) return console.error(err);
      if(!user){
        const hash = bcrypt.hashSync('password', 12);
        const root = new users({username:'root', password:hash, email:'root@multimedia.com', role:'Admin', info:'test', date: new Date()});
        root.save(function (err, root){
          if (err) return console.error(err);
        });
      }
    });
  }

  checkUser(req, callback){
    this.users.findOne({email:req.body.email}, function (err, user){
      if (err) return console.error(err);
      return callback(user);
    });
  }

  checkEmail(req, callback){
    this.users.findOne({email:req.body.email}, function (err, user){
      if (err) return console.error(err);
      return callback(user);
    });
  }

  insertUser(req, callback){
    const hash = this.bcrypt.hashSync(req.body.password, 12);
    const user = new (this.users)({username:req.body.username, password:hash, email:req.body.email, role:req.body.role, info:req.body.info, date: new Date(req.body.date)});
    user.save(function (err, user){
      if (err) return callback(false, err);
      return callback(user, false)
    });
  }

}//end class

module.exports = new Usersdb();

GitHub: "https://github.com/Sokhavuth/multimedia
Heroku: https://khmerweb-multimedia.herokuapp.com/

Comments

Popular posts from this blog