Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cookieParser = require('cookie-parser');
- var bodyParser = require('body-parser');
- var path = require('path');
- var http = require('http');
- var express = require('express');
- var fs = require('fs');
- var app = express();
- var mysql = require('mysql');
- var after = require("after");
- var bcrypt = require('bcrypt');
- var https = require('https');
- var privateKey = fs.readFileSync('key.pem');
- var certificate = fs.readFileSync('cert.pem');
- var credentials = {key: privateKey, cert: certificate};
- var app = express();
- var httpsServer = https.createServer(credentials, app);
- var httpServer = http.createServer(app);
- httpsServer.listen(3000);
- httpServer.listen(3001);
- var connection = mysql.createConnection({
- host : 'localhost',
- port: '/var/run/mysqld/mysqld.sock',
- user : 'root',
- password : 'YourBigM@m@n4815162342',
- database : 'LLDB',
- charset: "utf8_general_ci"
- });
- const saltRounds = 10;
- connection.connect();
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(bodyParser.json());
- app.use(cookieParser());
- app.use(express.static(__dirname + '/public'));
- app.get('/', function (req, res) {
- //console.log("Cookies: ", req.cookies);
- res.sendFile(__dirname+ '/private/home.html');
- });
- app.all('/signup', function (req, res) {
- var bannedEmailDomains = [ "mvrht", "noicd", "10minuteemail" , "10minutemail", "20email", "dropmail"];
- var signupStatus = {};
- var usernameErrors = [];
- var emailErrors = [];
- var passwordErrors = [];
- var finished = after(3, sendSignupStatus);
- //Username
- if(!req.body.username || req.body.username.length < 4) {
- usernameErrors.push("Must be at least 4 characters long");
- finished();
- }
- else {
- if(!isASCII(req.body.username, false))
- usernameErrors.push("Invalid symbols");
- if(req.body.username.length > 30)
- usernameErrors.push("Must be less than 30 characters long");
- if(usernameErrors.length === 0) {
- connection.query("SELECT * FROM user_main_info WHERE username = '"+req.body.username + "';", function(error, result, field) {
- if(result.length > 0 ) {
- usernameErrors.push("Account already exists");
- }
- finished();
- });
- }
- else
- finished();
- }
- //Email
- if(!req.body.email) {
- emailErrors.push("Email not specified");
- finished();
- }
- else {
- if(!isASCII(req.body.email,true))
- emailErrors.push("Invalid symbols");
- if(req.body.email.length > 30)
- emailErrors.push("Must be less than 30 characters long");
- for(var i = 0; i < bannedEmailDomains.length; i++) {
- if(req.body.email.indexOf(bannedEmailDomains[i]) !== -1) {
- emailErrors.push("Invalid email domain");
- break;
- }
- }
- if(req.body.email.indexOf("@") === -1 || req.body.email.indexOf(" ") !== -1)
- emailErrors.push("Invalid email");
- if(emailErrors.length === 0) {
- connection.query("SELECT * FROM user_main_info WHERE email = '"+req.body.email + "';", function(error, result, field) {
- if(result.length > 0 ) {
- emailErrors.push("This email is taken");
- }
- finished();
- });
- }
- else
- finished();
- }
- //Password
- if(!req.body.password || req.body.password.length < 10) {
- passwordErrors.push("Must be at least 10 characters long");
- finished();
- }
- else
- finished();
- function sendSignupStatus() {
- signupStatus["usernameErrors"] = usernameErrors;
- signupStatus["emailErrors"] = emailErrors;
- signupStatus["passwordErrors"] = passwordErrors;
- if(signupStatus["usernameErrors"].length !== 0 ||
- signupStatus["emailErrors"].length !== 0 ||
- signupStatus["emailErrors"].length !== 0) {
- res.send(JSON.stringify(signupStatus));
- }
- else {
- bcrypt.genSalt(10, function(err, salt) {
- bcrypt.hash(req.body.password, salt, function(err, hash) {
- connection.query("INSERT INTO user_main_info (username, email, password, salt) VALUES ('"+ req.body.username+"', '"+req.body.email +"', '"+ hash + "', '"+salt+"');");
- res.send("1");
- });
- });
- }
- }
- });
- app.get('/:id', function (req, res) {
- if(req.params.id !== "profile")
- {
- res.status(404);
- res.send('Not found!');
- }
- res.sendFile(__dirname+ '/private/profile.html');
- });
- app.listen(1337);
- //Checks if string contains only ASCII characters
- function isASCII(str, acceptSpecial) {
- if(acceptSpecial)
- return /^[\x00-\x7F]*$/.test(str);
- else
- return /^[0-9a-zA-Z]+$/.test(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement