From 73ecdf5e6d0572de2c79b01831386a29103f3c20 Mon Sep 17 00:00:00 2001 From: KH Date: Tue, 26 Sep 2017 21:20:20 +0200 Subject: [PATCH] Registering uses elasticsearch --- web/src/modules/routes.js | 72 ++++++++++++++++++++++++++------------- web/src/static/global.js | 3 +- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/web/src/modules/routes.js b/web/src/modules/routes.js index a7db8ad..d6d686c 100644 --- a/web/src/modules/routes.js +++ b/web/src/modules/routes.js @@ -220,23 +220,6 @@ 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); -// }); module.exports.register = (req, res) => { let email = req.body.email; @@ -249,11 +232,13 @@ module.exports.register = (req, res) => { 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) { - if (err) console.log(err); - if (result[0] && result[0].email == email) errors.email = "exist"; - if (result[1] && result[1].email == email) errors.email = "exist"; - if (!errors.email && !errors.password && !errors.password2) { + client.search({ + index: "users", + type: "user", + q: "email:"+email, + size: 1 + }).then((body) => { + if (body.hits.total == 0) { bcrypt.genSalt(10, function(err, salt) { if (err) { jsonResErr(res, {unknown: 55529}); @@ -273,14 +258,53 @@ module.exports.register = (req, res) => { }).then((body) => { jsonResErr(res, null); }, (error) => { - jsonResErr(res, 29132); + jsonResErr(res, {unknown: 50132}); }); } }); } }); - } else res.json({ "errors": errors }); + } else if (body.hits.total == 1) { + jsonResErr(res, {email: "exist"}); + } else { + jsonResErr(res, {unknown: 59283}); + } + + }, (error) => { + jsonResErr(res, {unknown: 52334}); }); + // db.query("SELECT * FROM users WHERE email = ?", email, function(err, result) { + // if (err) console.log(err); + // if (result[0] && result[0].email == email) errors.email = "exist"; + // if (result[1] && result[1].email == email) errors.email = "exist"; + // if (!errors.email && !errors.password && !errors.password2) { + // bcrypt.genSalt(10, function(err, salt) { + // if (err) { + // jsonResErr(res, {unknown: 55529}); + // } else { + // bcrypt.hash(password, salt, function(err, hashedPassword) { + // if (err) { + // jsonResErr(res, {unknown: 55222}); + // } else { + // client.index({ + // index: "users", + // type: "user", + // body: { + // userId: b32(6), + // email: email, + // password: hashedPassword + // } + // }).then((body) => { + // jsonResErr(res, null); + // }, (error) => { + // jsonResErr(res, 29132); + // }); + // } + // }); + // } + // }); + // } else res.json({ "errors": errors }); + // }); }; module.exports.logout = (req, res) => { diff --git a/web/src/static/global.js b/web/src/static/global.js index 9785451..8477743 100644 --- a/web/src/static/global.js +++ b/web/src/static/global.js @@ -1389,8 +1389,7 @@ function clickRegister() { if (!d) d = validate("noMatch", "The passwords do not match", password2, password ); if (!a && !b && !c && !d) { var req = - "type=register"+ - "&email="+email.value+ + "email="+email.value+ "&password="+password.value+ "&password2="+password2.value; -- 2.51.2