Advertisement
Guest User

Untitled

a guest
Mar 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const db = require('./db');
  3. const bodyParser = require('body-parser');
  4.  
  5. const app = express();
  6. const jsonParser = bodyParser.json({ extended: false });
  7. const PORT = 4000;
  8.  
  9. //Generate a key within stupid&simple algorithm
  10. function keyGen() {
  11.     return Math.floor(Math.random() * (4 - 1)) + 1; //returning a random (расширить генерацию с применением MD5)
  12.                                                     // value from 1 to 3
  13. }
  14.  
  15. //killing a key after 1 minute pass(setting it to NULL)
  16. function killKey() {
  17.     return db.models.users.findAll().then((rows) => {
  18.         let promises = [];
  19.         for (let i in rows) {
  20.             console.log('hello');
  21.  
  22.             let difference = new Date().getTime() - rows[i].dataValues.updatedAt.getTime(); // unixtime
  23.  
  24.             if(difference > 60 * 1000) { //if diff more than 1 min setting key to NULL
  25.                 promises.push(rows[i].update({
  26.                     key: null
  27.                 }));
  28.             }
  29.         }
  30.  
  31.         return Promise.all(promises); //  ждём завершения всех промисов
  32.     });
  33. }
  34.  
  35. app.use('/auth',jsonParser,function (req, res, next) {
  36.     killKey(); // killing a keys
  37.  
  38.     let clientKey;
  39.    
  40.     if (!req.headers.cookie) {
  41.         clientKey = null;
  42.     } else {
  43.         clientKey = req.headers.cookie; // TODO: fix it (parse cookies)
  44.     }
  45.    
  46.     return db.models.users.findOne({
  47.         where: {
  48.             login: req.body.login,
  49.             password: req.body.password,
  50.             key: clientKey
  51.         }
  52.     }).then(row => {
  53.         if (row === null) {
  54.             res.send('session expired relog in please');
  55.         } else {
  56.             next();
  57.         }
  58.     });
  59. });
  60.  
  61. app.route('/auth')
  62.     .post(jsonParser, (req,res) => {
  63.         return db.models.users.findUser(req.body.login, req.body.password)
  64.             .then(user => {
  65.                 if (user !== null) { // logged correct
  66.                     if (!req.headers.cookie) { // if the req has no key server send it within the object
  67.                         let key;
  68.                         key = keyGen();
  69.                         return user.update({
  70.                             key // fixed (shorter)
  71.                         });
  72.                     } else {
  73.                         throw new Error('key is valid, u re authorized')
  74.                     }
  75.                 } else { //no comb like this
  76.                     throw new Error('No such user with login and password');
  77.                 }
  78.             }).then(() => {
  79.                 return db.models.users.findUser(req.body.login, req.body.password);
  80.             }).then(user => {
  81.                 res.json(user.dataValues);//sending the obj with the new key to the client
  82.             }).catch(e => res.status(500).send(e.msg));
  83.     });
  84.  
  85. app.route('/reg')
  86.     .post(jsonParser, (req,res) => {
  87.         return db.models.users.findUser(req.body.login, req.body.password).then(user => {
  88.             if (user === null) {
  89.                 return db.models.users.regUser(req.body.login, req.body.login).then(() => { // fix promise chaining
  90.                    res.send('registred successfully');
  91.                 });
  92.             } else {
  93.                 res.send('this log&pass combination is already exist');
  94.             }
  95.         });
  96.     });
  97.  
  98. app.listen(PORT, '127.0.0.1', (err) => {
  99.     if(err) {
  100.         console.error(err.message);
  101.     } else {
  102.         console.log('listening to port ' + PORT);
  103.     }
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement