Advertisement
Guest User

Untitled

a guest
Sep 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var bodyParser = require('body-parser');
  3. var app = express();
  4. var path = require('path');
  5. var database = require('mongodb').MongoClient;
  6. const jwt = require('jsonwebtoken');
  7. const uuid = require('uuid/v4');
  8.  
  9. app.use(bodyParser.json());
  10. app.use(bodyParser.urlencoded({ extended: true } ));
  11. app.use(express.static(path.join(__dirname,'dist')));
  12.  
  13. app.use((req,res,next) => {
  14.     res.setHeader('Acess-Control-Allow-Origin', '*');
  15.     res.setHeader('Acess-Control-Allow-Methods', 'GET, POST');
  16.     res.setHeader('Acess-Control-Allow-Headers', 'X-Request-With,content-type');
  17.     res.setHeader('Acess-Control-Allow-Credentials', true);
  18.     next();
  19. });
  20.  
  21. app.get('*', (req,res) => {
  22.     res.sendFile(path.join(__dirname,'dist/index.html'));
  23. });
  24.  
  25. app.post("/data", (req,res)  => {
  26.  
  27.     fullCookie = req.headers.cookie;
  28.     cookies = fullCookie.split(';');
  29.  
  30.     for (i = 0; i < cookies.length; i++)
  31.     {
  32.         cookie = cookies[i];
  33.         cookieKeyValue = cookie.split('=');
  34.  
  35.         if (cookieKeyValue[0] == 'token')
  36.         {
  37.             var isValid = jwt.verify(cookieKeyValue[1],'stupid', function(err,token)
  38.             {
  39.                 if (err)
  40.                 {
  41.                     response.set('Content-Type', 'text/plain');
  42.                     response.send(JSON.stringify("{ \"ErrorMsg\":\"Invalid Token!\"}"));
  43.                 } else {
  44.                     //send data
  45.                 }
  46.             });
  47.             break;
  48.         }
  49.  
  50.     }
  51.  
  52.    
  53.  
  54. });
  55.  
  56. app.post("/login",(request, response) => {
  57.  
  58.     if (request.body.Username == "")
  59.     {
  60.         response.set('Content-Type', 'text/plain');
  61.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter your username\"}"));
  62.     }
  63.     if (request.body.Password == "")
  64.     {
  65.         response.set('Content-Type', 'text/plain');
  66.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter you password\"}"));
  67.     }
  68.     database.connect("mongodb://localhost:27017/integrated_test",function(err,db)
  69.     {
  70.  
  71.         mydb = db.db('accounts');
  72.  
  73.         var findUser = {
  74.             "Username" : request.body.Username,
  75.             "Password" : request.body.Password
  76.         };
  77.  
  78.  
  79.         usersArray = mydb.collection('user').find(findUser).toArray(function(err,result)
  80.         {
  81.  
  82.             mydb = db.db('accounts');
  83.             if (result.length != 0)
  84.             {
  85.                 var token;
  86.                 var secretKey = 'stupid';
  87.                 var UserInfo =
  88.                 {
  89.                     "Username" : "" + request.body.Username,
  90.                     "Password" : "" + request.body.Password,
  91.                     "EMail" : "" +  request.body.EMail
  92.                 };
  93.  
  94.                 token = jwt.sign(UserInfo,secretKey);
  95.                 response.cookie('token','' + token);
  96.                 response.set('content-type', 'text/plain');
  97.                 response.send(JSON.stringify("{ \"Succeded\": true, \"Token\":" + "\"" + token + "\"}"));
  98.  
  99.             } else {
  100.                 response.set('content-type', 'text/plain');
  101.                 response.send(JSON.stringify("{ \"ErrorMsg\":\"Error: Username or Password is incorrect.\"}"));
  102.             }
  103.         }
  104.         );
  105.     });
  106.  
  107.  
  108.  
  109. });
  110.  
  111.  
  112. app.post("/register",(request, response) => {
  113.  
  114.    
  115.     var succeded = true;
  116.     console.log(request.body.EMail);
  117.  
  118.     var regExForMail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  119.  
  120.     if (request.body.Username == "")
  121.     {
  122.         response.set('Content-Type', 'text/plain');
  123.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please choose a username\"}"));
  124.  
  125.         succeded = false;
  126.     } else
  127.     if (request.body.EMail == "")
  128.     {
  129.         response.set('content-type', 'text/plain');
  130.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter an e-mail\"}"));
  131.         succeded = false;
  132.     } else
  133.     if (request.body.Password == "")
  134.     {
  135.         response.set('content-type', 'text/plain');
  136.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter a password\"}"));
  137.         succeded = false;
  138.     } else
  139.     if (regExForMail.test(request.body.EMail) == false)
  140.     {
  141.         response.set('content-type', 'text/plain');
  142.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter a valid e-mail address\"}"));
  143.         succeded = false;
  144.     } else
  145.     if (request.body.Password.length < 5)
  146.     {
  147.         response.set('content-type', 'text/plain');
  148.         response.send(JSON.stringify("{ \"ErrorMsg\":\"Your password is too short.\"}"));
  149.         succeded = false;
  150.     }
  151.        
  152.     if (succeded)
  153.     {
  154.         database.connect("mongodb://localhost:27017/integrated_test",function(err,db)
  155.         {
  156.  
  157.             mydb = db.db('accounts');
  158.  
  159.             var findUser = {
  160.                 "Username" : request.body.Username,
  161.                 "Password" : request.body.Password
  162.             };
  163.  
  164.             usersArray = mydb.collection('user').find(findUser).toArray(function(err,result) {
  165.             if (result.length != 0)
  166.             {
  167.                 response.set('content-type', 'text/plain');
  168.                 response.send(JSON.stringify("{ \"ErrorMsg\":\"This e-mail has already been register.\"}"));
  169.  
  170.             } else {
  171.                 var token;
  172.                 var secretKey = 'stupid';
  173.                 var UserInfo =
  174.                 {
  175.                     "Username" : "" + request.body.Username,
  176.                     "Password" : "" + request.body.Password,
  177.                     "EMail" : "" +  request.body.EMail
  178.                 };
  179.  
  180.                
  181.                 mydb.collection('user').insert( UserInfo );
  182.  
  183.                 token = jwt.sign(UserInfo,secretKey);
  184.                 response.cookie('token','' + token);
  185.                 response.set('content-type', 'text/plain');
  186.                 response.send(JSON.stringify("{ \"Succeded\": true, \"Token\":" + "\"" + token + "\"}"));
  187.  
  188.             }
  189.  
  190.    
  191.         }
  192.         );
  193.  
  194.                 });
  195.     }
  196.  
  197. });
  198.  
  199. var server = app.listen(4000,(req,res)=>
  200. {
  201.     console.log('listening on port 4000');
  202. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement