Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pool = require('./mysql-pool');
- var bcrypt = require('bcrypt');
- module.exports = {
- get: function(req, res) {
- },
- validate: function(req, res) {
- var response = {};
- var data = req.query;
- if (typeof data.username != "undefined" && typeof data.password != "undefined") {
- pool.getConnection(function(err, connection) {
- connection.query('' +
- 'SELECT id, username, password ' +
- 'FROM users ' +
- 'WHERE username = ? AND activated = 1' +
- '', [data.username], function(err, rows) {
- //
- if (!err) {
- if (typeof rows[0] != 'undefined') {
- var user = rows[0];
- var validate_hash = bcrypt.compareSync(data.password, user.password);
- if (validate_hash) {
- var salt = bcrypt.genSaltSync(10);
- var session_id = data.username + data.password + Date.now();
- session_id = bcrypt.hashSync(session_id, salt);
- connection.query('' +
- 'INSERT INTO sessions ' +
- 'SET id = ?, user_id = ?, expire = ?' +
- '', [session_id, user.id, (Date.now() + (60*60*1000))], function(err, rows) {
- if (!err) {
- response = {
- 'status': 1,
- 'data': session_id
- };
- res.json(response);
- return null;
- }
- else {
- response = {
- 'status': 0,
- 'message': JSON.stringify(err)
- };
- res.json(response);
- return null;
- }
- });
- }
- else {
- response = {
- 'status': 0,
- 'message': 'User not found..'
- };
- res.json(response);
- return null;
- }
- }
- else {
- response = {
- 'status': 0,
- 'message': 'User not found'
- };
- res.json(response);
- return null;
- }
- }
- else {
- response = {
- 'status': 0,
- 'message': JSON.stringify(err)
- };
- res.json(response);
- return null;
- }
- });
- });
- }
- },
- create: function(req, res) {
- var response = {};
- var data = req.body;
- if (typeof data.username != 'undefined' && typeof data.password != 'undefined') {
- pool.getConnection(function (err, connection) {
- var salt = bcrypt.genSaltSync(10);
- var hashed_password = bcrypt.hashSync(data.password, salt);
- connection.query('INSERT INTO users SET username = ?, password = ?, activated = 0', [data.username, hashed_password], function (err, rows) {
- if (!err) {
- response = {
- 'status': 1
- }
- }
- else {
- response = {
- 'status': 0,
- 'message': JSON.stringify(err)
- }
- }
- res.json(response);
- });
- });
- }
- },
- delete: function(req, res) {
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement