[READ-ONLY] Mirror of https://github.com/probablykasper/ferrum. Music library app for Mac, Linux and Windows ferrum.kasper.space
electron linux macos music music-library music-player napi windows
0

Configure Feed

Select the types of activity you want to include in your feed.

Revert "Removed username, just emails"

This reverts commit 23795ed7b3bf9bfc0c7c6527c4de961d1781cbc0.

KH (Sep 26, 2017, 11:20 PM +0200) 8e3befd8 398558d6

+60 -39
+4 -4
web/src/modules/passport.js
··· 4 4 5 5 module.exports = function(passport) { 6 6 // local strategy 7 - passport.use(new LocalStrategy(function(email, password, done) { 8 - // match email 9 - db.query("SELECT * FROM users WHERE email = ?", email, function(err, result) { 7 + passport.use(new LocalStrategy(function(username, password, done) { 8 + // match username 9 + db.query("SELECT * FROM users WHERE username = ?", [username], function(err, result) { 10 10 if (err) console.log(err); 11 - if (!result[0]) return done(null, false, {email: "exist"}); 11 + if (!result[0]) return done(null, false, {username: "exist"}); 12 12 13 13 // match password 14 14 bcrypt.compare(password, result[0].password, function(err, isMatch) {
+36 -26
web/src/modules/routes.js
··· 181 181 // ---------- account ---------- 182 182 183 183 module.exports.login = (req, res) => { 184 - let email = req.body.email; 184 + let username = req.body.username; 185 185 let password = req.body.password; 186 186 let errors = {}; 187 - if (!validator.isEmail(email)) errors.email = "invalid"; 188 - if (validator.isEmpty(email)) errors.email = "empty"; 189 - if (!validator.isLength(email, {max: 60})) errors.email = "long"; 187 + if (validator.isEmpty(username)) errors.username = "empty"; 188 + if (!validator.isLength(username, {max: 30})) errors.username = "long"; 190 189 if (!validator.isLength(password, {min: 8})) errors.password = "short"; 191 190 if (!validator.isLength(password, {max: 100})) errors.password = "long"; 192 191 193 - if (!errors.email && !errors.password) { 192 + if (!errors.username && !errors.password) { 194 193 // auth 195 194 passport.authenticate("local", function(err, user, info) { 196 195 if (err) return console.log(err); 197 196 if (!user) { 198 - if (info.email) errors.email = info.email; 197 + if (info.username) errors.username = info.username; 199 198 if (info.password) errors.password = info.password; 200 199 res.json({ "errors": errors }); 201 200 } else { ··· 220 219 // console.log("-----err"); 221 220 // console.log(error); 222 221 // }); 223 - // client.indices.putMapping({ 224 - // index: "users", 225 - // type: "cooltype", 226 - // body: { 227 - // "properties": { 228 - // "full_name": { 229 - // "type": "text" 230 - // } 231 - // } 232 - // } 233 - // }).then((body) => { 234 - // console.log("-----suc"); 235 - // console.log(body); 236 - // }, (error) => { 237 - // console.log("-----err"); 238 - // console.log(error); 239 - // }); 222 + client.indices.putMapping({ 223 + index: "users", 224 + type: "user", 225 + body: { 226 + properties: { 227 + username: "text", 228 + email: "text", 229 + password: "text" 230 + } 231 + } 232 + }).then((body) => { 233 + console.log("-----suc"); 234 + console.log(body); 235 + }, (error) => { 236 + console.log("-----err"); 237 + console.log(error); 238 + }); 240 239 241 240 module.exports.register = (req, res) => { 241 + let username = req.body.username; 242 242 let email = req.body.email; 243 243 let password = req.body.password; 244 244 let password2 = req.body.password2; 245 245 let errors = {}; 246 + if (validator.isEmpty(username)) errors.username = "empty"; 247 + if (!validator.isLength(username, {max: 30})) errors.username = "long"; 246 248 if (!validator.isEmail(email)) errors.email = "invalid"; 247 249 if (validator.isEmpty(email)) errors.email = "empty"; 248 250 if (!validator.isLength(email, {max: 60})) errors.email = "long"; 249 251 if (!validator.isLength(password, {min: 8})) errors.password = "short"; 250 252 if (!validator.isLength(password, {max: 100})) errors.password = "long"; 251 253 if (!validator.equals(password2, password)) errors.password2 = "match"; 252 - db.query("SELECT * FROM users WHERE email = ?", email, function(err, result) { 254 + db.query("SELECT * FROM users WHERE username = ? OR email = ?", [username, email], function(err, result) { 253 255 if (err) console.log(err); 256 + if (result[0] && result[0].username == username) errors.username = "exist"; 254 257 if (result[0] && result[0].email == email) errors.email = "exist"; 258 + if (result[1] && result[1].username == username) errors.username = "exist"; 255 259 if (result[1] && result[1].email == email) errors.email = "exist"; 256 - if (!errors.email && !errors.password && !errors.password2) { 260 + if (!errors.username && !errors.email && !errors.password && !errors.password2) { 257 261 bcrypt.genSalt(10, function(err, salt) { 258 262 if (err) { 259 263 jsonResErr(res, {unknown: 55529}); ··· 262 266 if (err) { 263 267 jsonResErr(res, {unknown: 55222}); 264 268 } else { 269 + let values = { 270 + userId: b32(6), 271 + username: username, 272 + email: email, 273 + password: hashedPassword 274 + }; 265 275 client.index({ 266 276 index: "users", 267 277 type: "user", 268 278 body: { 269 - userId: b32(6), 279 + username: username, 270 280 email: email, 271 281 password: hashedPassword 272 282 }
+3 -1
web/src/pug/login.pug
··· 3 3 .form 4 4 .fields 5 5 .success 6 + .error.username 7 + input.for-login.for-register(type="text", name="username", placeholder="Username", tabindex="-1") 6 8 .error.email 7 - input.for-login.for-register(type="text", name="email", placeholder="Email", tabindex="-1") 9 + input.for-register(type="text", name="email", placeholder="Email", tabindex="-1") 8 10 .error.password 9 11 input.for-login.for-register(type="password", name="password", placeholder="Password", tabindex="-1") 10 12 .error.password2
+17 -8
web/src/static/global.js
··· 1303 1303 } 1304 1304 function resetMsgs() { 1305 1305 success.classList.remove("visible"); 1306 + username. previousElementSibling.classList.remove("visible"); 1306 1307 email. previousElementSibling.classList.remove("visible"); 1307 1308 password. previousElementSibling.classList.remove("visible"); 1308 1309 password2.previousElementSibling.classList.remove("visible"); 1309 1310 } 1310 1311 function displayErr(field, msg) { 1312 + if (field == "username") field = username; 1311 1313 if (field == "email") field = email; 1312 1314 if (field == "password") field = password; 1313 1315 if (field == "password2") field = password2; ··· 1318 1320 var register = document.querySelector("button.register"); 1319 1321 var form = document.querySelector(".form"); 1320 1322 1323 + var username = document.querySelector("input[name='username']"); 1321 1324 var email = document.querySelector("input[name='email']"); 1322 1325 var password = document.querySelector("input[name='password']"); 1323 1326 var password2 = document.querySelector("input[name='password2']"); ··· 1327 1330 if (!form.classList.contains("login")) { 1328 1331 email.setAttribute("tabindex", "-1"); 1329 1332 password2.setAttribute("tabindex", "-1"); 1333 + username.setAttribute("tabindex", "0"); 1330 1334 password.setAttribute("tabindex", "0"); 1331 1335 login.setAttribute("tabindex", "0"); 1332 1336 register.setAttribute("tabindex", "0"); ··· 1337 1341 form.classList.add("has-been-expanded"); 1338 1342 } else { 1339 1343 var a = false, b = false; 1340 - if (!b) b = validate("empty", "We need your email", email ); 1341 - if (!b) b = validate("longerThan", "Maximum 60 characters :/", email, 60 ); 1342 - if (!b) b = validate("notEmail", "That's email isn't valid", email ); 1344 + if (!a) a = validate("empty", "You should fill this in", username ); 1345 + if (!a) a = validate("longerThan", "Keep it below 30", username, 30 ); 1343 1346 if (!b) b = validate("shorterThan", "That's a bit short... Try 8 characters", password, 8 ); 1344 1347 if (!b) b = validate("longerThan", "You crossed the 100 characters line", password, 100 ); 1345 1348 1346 1349 if (!a && !b) { 1347 1350 var req = 1348 1351 "type=login"+ 1349 - "&email="+email.value+ 1352 + "&username="+username.value+ 1350 1353 "&password="+password.value; 1351 1354 xhr(req, "/login", function(res) { 1352 1355 var errors = JSON.parse(res).errors; 1353 1356 if (errors) { 1354 - if (errors.email == "invalid") displayErr("email", "That email isn't valid"); 1355 - if (errors.email == "empty") displayErr("email", "We need your email"); 1356 - if (errors.email == "long") displayErr("email", "Maximum 60 characters :/"); 1357 - if (errors.email == "exist") displayErr("email", "Email already exists"); 1357 + if (errors.username == "empty") displayErr("username", "You should fill this in"); 1358 + if (errors.username == "long") displayErr("username", "Keep it below 30"); 1359 + if (errors.username == "exist") displayErr("username", "Had trouble finding that user"); 1358 1360 if (errors.password == "short") displayErr("password", "That's a bit short... Try 8 characters"); 1359 1361 if (errors.password == "long") displayErr("password", "You crossed the 100 characters line"); 1360 1362 if (errors.password == "incorrect") displayErr("password", "You guessed the wrong password"); ··· 1369 1371 function clickRegister() { 1370 1372 resetMsgs(); 1371 1373 if (!form.classList.contains("register")) { 1374 + username.setAttribute("tabindex", "0"); 1372 1375 email.setAttribute("tabindex", "0"); 1373 1376 password.setAttribute("tabindex", "0"); 1374 1377 password2.setAttribute("tabindex", "0"); ··· 1381 1384 form.classList.add("has-been-expanded"); 1382 1385 } else { 1383 1386 var a = false, b = false, c = false, d = false; 1387 + if (!a) a = validate("empty", "You should fill this in", username ); 1388 + if (!a) a = validate("longerThan", "Keep it below 30", username, 30 ); 1384 1389 if (!b) b = validate("empty", "We need your email", email ); 1385 1390 if (!b) b = validate("longerThan", "Maximum 60 characters :/", email, 60 ); 1386 1391 if (!b) b = validate("notEmail", "That's email isn't valid", email ); ··· 1390 1395 if (!a && !b && !c && !d) { 1391 1396 var req = 1392 1397 "type=register"+ 1398 + "&username="+username.value+ 1393 1399 "&email="+email.value+ 1394 1400 "&password="+password.value+ 1395 1401 "&password2="+password2.value; ··· 1397 1403 xhr(req, "/register", function(res) { 1398 1404 var errors = JSON.parse(res).errors; 1399 1405 if (errors) { 1406 + if (errors.username == "empty") displayErr("username", "You should fill this in"); 1407 + if (errors.username == "long") displayErr("username", "Keep it below 30"); 1408 + if (errors.username == "exist") displayErr("username", "Unavailable username"); 1400 1409 if (errors.email == "invalid") displayErr("email", "That email isn't valid"); 1401 1410 if (errors.email == "empty") displayErr("email", "We need your email"); 1402 1411 if (errors.email == "long") displayErr("email", "Maximum 60 characters :/");