Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var bodyParser = require('body-parser');
- var app = express();
- var path = require('path');
- var database = require('mongodb').MongoClient;
- const jwt = require('jsonwebtoken');
- const uuid = require('uuid/v4');
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true } ));
- app.use(express.static(path.join(__dirname,'dist')));
- app.use((req,res,next) => {
- res.setHeader('Acess-Control-Allow-Origin', '*');
- res.setHeader('Acess-Control-Allow-Methods', 'GET, POST');
- res.setHeader('Acess-Control-Allow-Headers', 'X-Request-With,content-type');
- res.setHeader('Acess-Control-Allow-Credentials', true);
- next();
- });
- app.get('*', (req,res) => {
- res.sendFile(path.join(__dirname,'dist/index.html'));
- });
- app.post("/data", (req,res) => {
- fullCookie = req.headers.cookie;
- cookies = fullCookie.split(';');
- for (i = 0; i < cookies.length; i++)
- {
- cookie = cookies[i];
- cookieKeyValue = cookie.split('=');
- if (cookieKeyValue[0] == 'token')
- {
- var isValid = jwt.verify(cookieKeyValue[1],'stupid', function(err,token)
- {
- if (err)
- {
- response.set('Content-Type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Invalid Token!\"}"));
- } else {
- //send data
- }
- });
- break;
- }
- }
- });
- app.post("/login",(request, response) => {
- if (request.body.Username == "")
- {
- response.set('Content-Type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter your username\"}"));
- }
- if (request.body.Password == "")
- {
- response.set('Content-Type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter you password\"}"));
- }
- database.connect("mongodb://localhost:27017/integrated_test",function(err,db)
- {
- mydb = db.db('accounts');
- var findUser = {
- "Username" : request.body.Username,
- "Password" : request.body.Password
- };
- usersArray = mydb.collection('user').find(findUser).toArray(function(err,result)
- {
- mydb = db.db('accounts');
- if (result.length != 0)
- {
- var token;
- var secretKey = 'stupid';
- var UserInfo =
- {
- "Username" : "" + request.body.Username,
- "Password" : "" + request.body.Password,
- "EMail" : "" + request.body.EMail
- };
- token = jwt.sign(UserInfo,secretKey);
- response.cookie('token','' + token);
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"Succeded\": true, \"Token\":" + "\"" + token + "\"}"));
- } else {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Error: Username or Password is incorrect.\"}"));
- }
- }
- );
- });
- });
- app.post("/register",(request, response) => {
- var succeded = true;
- console.log(request.body.EMail);
- 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,}))$/;
- if (request.body.Username == "")
- {
- response.set('Content-Type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please choose a username\"}"));
- succeded = false;
- } else
- if (request.body.EMail == "")
- {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter an e-mail\"}"));
- succeded = false;
- } else
- if (request.body.Password == "")
- {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter a password\"}"));
- succeded = false;
- } else
- if (regExForMail.test(request.body.EMail) == false)
- {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Please enter a valid e-mail address\"}"));
- succeded = false;
- } else
- if (request.body.Password.length < 5)
- {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"Your password is too short.\"}"));
- succeded = false;
- }
- if (succeded)
- {
- database.connect("mongodb://localhost:27017/integrated_test",function(err,db)
- {
- mydb = db.db('accounts');
- var findUser = {
- "Username" : request.body.Username,
- "Password" : request.body.Password
- };
- usersArray = mydb.collection('user').find(findUser).toArray(function(err,result) {
- if (result.length != 0)
- {
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"ErrorMsg\":\"This e-mail has already been register.\"}"));
- } else {
- var token;
- var secretKey = 'stupid';
- var UserInfo =
- {
- "Username" : "" + request.body.Username,
- "Password" : "" + request.body.Password,
- "EMail" : "" + request.body.EMail
- };
- mydb.collection('user').insert( UserInfo );
- token = jwt.sign(UserInfo,secretKey);
- response.cookie('token','' + token);
- response.set('content-type', 'text/plain');
- response.send(JSON.stringify("{ \"Succeded\": true, \"Token\":" + "\"" + token + "\"}"));
- }
- }
- );
- });
- }
- });
- var server = app.listen(4000,(req,res)=>
- {
- console.log('listening on port 4000');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement