diff --git a/web/src/modules/passport.js b/web/src/modules/passport.js index 977d10c..5b06e75 100644 --- a/web/src/modules/passport.js +++ b/web/src/modules/passport.js @@ -4,11 +4,11 @@ const db = require("./db"); module.exports = function(passport) { // local strategy - passport.use(new LocalStrategy(function(email, password, done) { - // match email - db.query("SELECT * FROM users WHERE email = ?", email, function(err, result) { + passport.use(new LocalStrategy(function(username, password, done) { + // match username + db.query("SELECT * FROM users WHERE username = ?", [username], function(err, result) { if (err) console.log(err); - if (!result[0]) return done(null, false, {email: "exist"}); + if (!result[0]) return done(null, false, {username: "exist"}); // match password bcrypt.compare(password, result[0].password, function(err, isMatch) { diff --git a/web/src/modules/routes.js b/web/src/modules/routes.js index a7db8ad..2b4ddd7 100644 --- a/web/src/modules/routes.js +++ b/web/src/modules/routes.js @@ -181,21 +181,20 @@ module.exports.playlist = (req, res) => { // ---------- account ---------- module.exports.login = (req, res) => { - let email = req.body.email; + let username = req.body.username; let password = req.body.password; let errors = {}; - if (!validator.isEmail(email)) errors.email = "invalid"; - if (validator.isEmpty(email)) errors.email = "empty"; - if (!validator.isLength(email, {max: 60})) errors.email = "long"; + if (validator.isEmpty(username)) errors.username = "empty"; + if (!validator.isLength(username, {max: 30})) errors.username = "long"; if (!validator.isLength(password, {min: 8})) errors.password = "short"; if (!validator.isLength(password, {max: 100})) errors.password = "long"; - if (!errors.email && !errors.password) { + if (!errors.username && !errors.password) { // auth passport.authenticate("local", function(err, user, info) { if (err) return console.log(err); if (!user) { - if (info.email) errors.email = info.email; + if (info.username) errors.username = info.username; if (info.password) errors.password = info.password; res.json({ "errors": errors }); } else { @@ -220,40 +219,45 @@ module.exports.login = (req, res) => { // console.log("-----err"); // console.log(error); // }); -// client.indices.putMapping({ -// index: "users", -// type: "cooltype", -// body: { -// "properties": { -// "full_name": { -// "type": "text" -// } -// } -// } -// }).then((body) => { -// console.log("-----suc"); -// console.log(body); -// }, (error) => { -// console.log("-----err"); -// console.log(error); -// }); +client.indices.putMapping({ + index: "users", + type: "user", + body: { + properties: { + username: "text", + email: "text", + password: "text" + } + } +}).then((body) => { + console.log("-----suc"); + console.log(body); +}, (error) => { + console.log("-----err"); + console.log(error); +}); module.exports.register = (req, res) => { + let username = req.body.username; let email = req.body.email; let password = req.body.password; let password2 = req.body.password2; let errors = {}; + if (validator.isEmpty(username)) errors.username = "empty"; + if (!validator.isLength(username, {max: 30})) errors.username = "long"; if (!validator.isEmail(email)) errors.email = "invalid"; if (validator.isEmpty(email)) errors.email = "empty"; if (!validator.isLength(email, {max: 60})) errors.email = "long"; if (!validator.isLength(password, {min: 8})) errors.password = "short"; if (!validator.isLength(password, {max: 100})) errors.password = "long"; if (!validator.equals(password2, password)) errors.password2 = "match"; - db.query("SELECT * FROM users WHERE email = ?", email, function(err, result) { + db.query("SELECT * FROM users WHERE username = ? OR email = ?", [username, email], function(err, result) { if (err) console.log(err); + if (result[0] && result[0].username == username) errors.username = "exist"; if (result[0] && result[0].email == email) errors.email = "exist"; + if (result[1] && result[1].username == username) errors.username = "exist"; if (result[1] && result[1].email == email) errors.email = "exist"; - if (!errors.email && !errors.password && !errors.password2) { + if (!errors.username && !errors.email && !errors.password && !errors.password2) { bcrypt.genSalt(10, function(err, salt) { if (err) { jsonResErr(res, {unknown: 55529}); @@ -262,11 +266,17 @@ module.exports.register = (req, res) => { if (err) { jsonResErr(res, {unknown: 55222}); } else { + let values = { + userId: b32(6), + username: username, + email: email, + password: hashedPassword + }; client.index({ index: "users", type: "user", body: { - userId: b32(6), + username: username, email: email, password: hashedPassword } diff --git a/web/src/pug/login.pug b/web/src/pug/login.pug index 49ff5b5..baf1158 100644 --- a/web/src/pug/login.pug +++ b/web/src/pug/login.pug @@ -3,8 +3,10 @@ main.home-page-logged-out .form .fields .success + .error.username + input.for-login.for-register(type="text", name="username", placeholder="Username", tabindex="-1") .error.email - input.for-login.for-register(type="text", name="email", placeholder="Email", tabindex="-1") + input.for-register(type="text", name="email", placeholder="Email", tabindex="-1") .error.password input.for-login.for-register(type="password", name="password", placeholder="Password", tabindex="-1") .error.password2 diff --git a/web/src/static/global.js b/web/src/static/global.js index 9785451..5212566 100644 --- a/web/src/static/global.js +++ b/web/src/static/global.js @@ -1303,11 +1303,13 @@ function validate(type, msg, el, third) { } function resetMsgs() { success.classList.remove("visible"); + username. previousElementSibling.classList.remove("visible"); email. previousElementSibling.classList.remove("visible"); password. previousElementSibling.classList.remove("visible"); password2.previousElementSibling.classList.remove("visible"); } function displayErr(field, msg) { + if (field == "username") field = username; if (field == "email") field = email; if (field == "password") field = password; if (field == "password2") field = password2; @@ -1318,6 +1320,7 @@ var login = document.querySelector("button.login"); var register = document.querySelector("button.register"); var form = document.querySelector(".form"); +var username = document.querySelector("input[name='username']"); var email = document.querySelector("input[name='email']"); var password = document.querySelector("input[name='password']"); var password2 = document.querySelector("input[name='password2']"); @@ -1327,6 +1330,7 @@ function clickLogin() { if (!form.classList.contains("login")) { email.setAttribute("tabindex", "-1"); password2.setAttribute("tabindex", "-1"); + username.setAttribute("tabindex", "0"); password.setAttribute("tabindex", "0"); login.setAttribute("tabindex", "0"); register.setAttribute("tabindex", "0"); @@ -1337,24 +1341,22 @@ function clickLogin() { form.classList.add("has-been-expanded"); } else { var a = false, b = false; - if (!b) b = validate("empty", "We need your email", email ); - if (!b) b = validate("longerThan", "Maximum 60 characters :/", email, 60 ); - if (!b) b = validate("notEmail", "That's email isn't valid", email ); + if (!a) a = validate("empty", "You should fill this in", username ); + if (!a) a = validate("longerThan", "Keep it below 30", username, 30 ); if (!b) b = validate("shorterThan", "That's a bit short... Try 8 characters", password, 8 ); if (!b) b = validate("longerThan", "You crossed the 100 characters line", password, 100 ); if (!a && !b) { var req = "type=login"+ - "&email="+email.value+ + "&username="+username.value+ "&password="+password.value; xhr(req, "/login", function(res) { var errors = JSON.parse(res).errors; if (errors) { - if (errors.email == "invalid") displayErr("email", "That email isn't valid"); - if (errors.email == "empty") displayErr("email", "We need your email"); - if (errors.email == "long") displayErr("email", "Maximum 60 characters :/"); - if (errors.email == "exist") displayErr("email", "Email already exists"); + if (errors.username == "empty") displayErr("username", "You should fill this in"); + if (errors.username == "long") displayErr("username", "Keep it below 30"); + if (errors.username == "exist") displayErr("username", "Had trouble finding that user"); if (errors.password == "short") displayErr("password", "That's a bit short... Try 8 characters"); if (errors.password == "long") displayErr("password", "You crossed the 100 characters line"); if (errors.password == "incorrect") displayErr("password", "You guessed the wrong password"); @@ -1369,6 +1371,7 @@ function clickLogin() { function clickRegister() { resetMsgs(); if (!form.classList.contains("register")) { + username.setAttribute("tabindex", "0"); email.setAttribute("tabindex", "0"); password.setAttribute("tabindex", "0"); password2.setAttribute("tabindex", "0"); @@ -1381,6 +1384,8 @@ function clickRegister() { form.classList.add("has-been-expanded"); } else { var a = false, b = false, c = false, d = false; + if (!a) a = validate("empty", "You should fill this in", username ); + if (!a) a = validate("longerThan", "Keep it below 30", username, 30 ); if (!b) b = validate("empty", "We need your email", email ); if (!b) b = validate("longerThan", "Maximum 60 characters :/", email, 60 ); if (!b) b = validate("notEmail", "That's email isn't valid", email ); @@ -1390,6 +1395,7 @@ function clickRegister() { if (!a && !b && !c && !d) { var req = "type=register"+ + "&username="+username.value+ "&email="+email.value+ "&password="+password.value+ "&password2="+password2.value; @@ -1397,6 +1403,9 @@ function clickRegister() { xhr(req, "/register", function(res) { var errors = JSON.parse(res).errors; if (errors) { + if (errors.username == "empty") displayErr("username", "You should fill this in"); + if (errors.username == "long") displayErr("username", "Keep it below 30"); + if (errors.username == "exist") displayErr("username", "Unavailable username"); if (errors.email == "invalid") displayErr("email", "That email isn't valid"); if (errors.email == "empty") displayErr("email", "We need your email"); if (errors.email == "long") displayErr("email", "Maximum 60 characters :/");