Advertisement
Guest User

Untitled

a guest
Mar 19th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var pool = require('./mysql-pool');
  2. var bcrypt = require('bcrypt');
  3.  
  4. module.exports = {
  5.   get: function(req, res) {
  6.   },
  7.   validate: function(req, res) {
  8.     var response = {};
  9.     var data = req.query;
  10.     if (typeof data.username != "undefined" && typeof data.password != "undefined") {
  11.       pool.getConnection(function(err, connection) {
  12.         connection.query('' +
  13.           'SELECT id, username, password ' +
  14.           'FROM users ' +
  15.           'WHERE username = ? AND activated = 1' +
  16.           '', [data.username], function(err, rows) {
  17.           //
  18.           if (!err) {
  19.             if (typeof rows[0] != 'undefined') {
  20.               var user = rows[0];
  21.               var validate_hash = bcrypt.compareSync(data.password, user.password);
  22.  
  23.               if (validate_hash) {
  24.                 var salt = bcrypt.genSaltSync(10);
  25.                 var session_id = data.username + data.password + Date.now();
  26.                 session_id = bcrypt.hashSync(session_id, salt);
  27.  
  28.                 connection.query('' +
  29.                   'INSERT INTO sessions ' +
  30.                   'SET id = ?, user_id = ?, expire = ?' +
  31.                   '', [session_id, user.id, (Date.now() + (60*60*1000))], function(err, rows) {
  32.                   if (!err) {
  33.                     response = {
  34.                       'status': 1,
  35.                       'data': session_id
  36.                     };
  37.                     res.json(response);
  38.                     return null;
  39.                   }
  40.                   else {
  41.                     response = {
  42.                       'status': 0,
  43.                       'message': JSON.stringify(err)
  44.                     };
  45.                     res.json(response);
  46.                     return null;
  47.                   }
  48.                 });
  49.               }
  50.               else {
  51.                 response = {
  52.                   'status': 0,
  53.                   'message': 'User not found..'
  54.                 };
  55.                 res.json(response);
  56.                 return null;
  57.               }
  58.             }
  59.             else {
  60.               response = {
  61.                 'status': 0,
  62.                 'message': 'User not found'
  63.               };
  64.               res.json(response);
  65.               return null;
  66.             }
  67.           }
  68.           else {
  69.             response = {
  70.               'status': 0,
  71.               'message': JSON.stringify(err)
  72.             };
  73.             res.json(response);
  74.             return null;
  75.           }
  76.         });
  77.       });
  78.     }
  79.   },
  80.   create: function(req, res) {
  81.     var response = {};
  82.     var data = req.body;
  83.     if (typeof data.username != 'undefined' && typeof data.password != 'undefined') {
  84.       pool.getConnection(function (err, connection) {
  85.         var salt = bcrypt.genSaltSync(10);
  86.         var hashed_password = bcrypt.hashSync(data.password, salt);
  87.         connection.query('INSERT INTO users SET username = ?, password = ?, activated = 0', [data.username, hashed_password], function (err, rows) {
  88.           if (!err) {
  89.             response = {
  90.               'status': 1
  91.             }
  92.           }
  93.           else {
  94.             response = {
  95.               'status': 0,
  96.               'message': JSON.stringify(err)
  97.             }
  98.           }
  99.  
  100.           res.json(response);
  101.         });
  102.       });
  103.     }
  104.   },
  105.   delete: function(req, res) {
  106.   }
  107. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement