mirror of https://github.com/calzoneman/sync.git
79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
/**
|
|
* Copyright 2013 Calvin 'calzoneman' Montgomery
|
|
*
|
|
* Licensed under Creative Commons Attribution-NonCommercial 3.0
|
|
* See http://creativecommons.org/licenses/by-nc/3.0/
|
|
*
|
|
*/
|
|
|
|
var mysql = require('mysql-libmysqlclient');
|
|
var Config = require('./config.js');
|
|
|
|
// Check if a name is taken
|
|
exports.isRegistered = function(name) {
|
|
var db = mysql.createConnectionSync();
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
if(!db.connectedSync()) {
|
|
console.log("MySQL Connection Failed");
|
|
return true;
|
|
}
|
|
var query = 'SELECT * FROM registrations WHERE uname="{}"'
|
|
.replace(/\{\}/, name);
|
|
var results = db.querySync(query);
|
|
var rows = results.fetchAllSync();
|
|
db.closeSync();
|
|
return rows.length > 0;
|
|
}
|
|
|
|
// Check if a name is valid
|
|
// Valid names are 1-20 characters, alphanumeric and underscores
|
|
exports.validateName = function(name) {
|
|
if(name.length > 20)
|
|
return false;
|
|
const VALID_REGEX = /^[a-zA-Z0-9_]+$/;
|
|
return name.match(VALID_REGEX) != null;
|
|
}
|
|
|
|
// Try to register a new account
|
|
exports.register = function(name, sha256) {
|
|
if(!exports.validateName(name))
|
|
return false;
|
|
if(exports.isRegistered(name))
|
|
return false;
|
|
var db = mysql.createConnectionSync();
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
if(!db.connectedSync()) {
|
|
console.log("MySQL Connection Failed");
|
|
return false;
|
|
}
|
|
var query = 'INSERT INTO registrations VALUES (NULL, "{1}", "{2}", 0)'
|
|
.replace(/\{1\}/, name)
|
|
.replace(/\{2\}/, sha256);
|
|
var results = db.querySync(query);
|
|
db.closeSync();
|
|
return results;
|
|
}
|
|
|
|
// Try to login
|
|
exports.login = function(name, sha256) {
|
|
var db = mysql.createConnectionSync();
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
if(!db.connectedSync()) {
|
|
console.log("MySQL Connection Failed");
|
|
return false;
|
|
}
|
|
var query = 'SELECT * FROM registrations WHERE uname="{1}" AND pw="{2}"'
|
|
.replace(/\{1\}/, name)
|
|
.replace(/\{2\}/, sha256);
|
|
var results = db.querySync(query);
|
|
var rows = results.fetchAllSync();
|
|
db.closeSync();
|
|
if(rows.length > 0) {
|
|
return rows[0];
|
|
}
|
|
return false;
|
|
}
|