Added E-mail check and fixes

This commit is contained in:
Norbi Peti 2016-08-19 09:57:00 +02:00
parent e4a9136c68
commit 2cae85e20d
3 changed files with 17 additions and 6 deletions

View file

@ -89,12 +89,14 @@ $(document).ready(function () {
$('#msginput').on("focus", function () { $('#msginput').on("focus", function () {
readTimer == null ? readTimer = setTimeout(function () { readTimer == null ? readTimer = setTimeout(function () {
resetUnread(); resetUnread();
readTimer = null;
}, 3000) : readTimer; }, 3000) : readTimer;
shouldread = true; shouldread = true;
}); });
$('#msginput').on("keydown", resetUnread); $('#msginput').on("keydown", resetUnread);
$('#msginput').on("blur", function () { $('#msginput').on("blur", function () {
readTimer != null ? clearTimeout(readTimer) : readTimer; readTimer != null ? clearTimeout(readTimer) : readTimer;
readTimer = null;
shouldread = false; shouldread = false;
}); });
}); });

View file

@ -64,7 +64,7 @@ public class IndexPage extends Page {
return doc; return doc;
}, exchange); }, exchange);
} // TODO: Validation at registration (no special chars, etc.) }
@Override @Override
public String GetName() { public String GetName() {

View file

@ -1,6 +1,8 @@
package io.github.norbipeti.chat.server.page; package io.github.norbipeti.chat.server.page;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Pattern;
import org.mindrot.jbcrypt.BCrypt; import org.mindrot.jbcrypt.BCrypt;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -12,6 +14,8 @@ import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper; import io.github.norbipeti.chat.server.io.IOHelper;
public class RegisterAjaxPage extends Page { public class RegisterAjaxPage extends Page {
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[\\w.-]+@[\\w.-]+\\.[\\w.-]+$");
@Override @Override
public void handlePage(HttpExchange exchange) throws IOException { public void handlePage(HttpExchange exchange) throws IOException {
JsonObject post = IOHelper.GetPOSTJSON(exchange); JsonObject post = IOHelper.GetPOSTJSON(exchange);
@ -22,10 +26,15 @@ public class RegisterAjaxPage extends Page {
IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange); IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange);
return; // TODO: Use JavaScript too, for error checks return; // TODO: Use JavaScript too, for error checks
} }
for (User user : DataManager.getAll(User.class)) { // TODO: Optimize String email = post.get("email").getAsString();
if (post.get("email").getAsString().equals(user.getEmail())) { if (!EMAIL_PATTERN.matcher(email).matches())
errormsg += "<p>An user with this E-mail already exists</p>"; errormsg += "<p>Invalid E-mail address</p>";
break; else {
for (User user : DataManager.getAll(User.class)) { // TODO: Optimize
if (email.equals(user.getEmail())) {
errormsg += "<p>An user with this E-mail already exists</p>";
break;
}
} }
} }
if (!post.get("pass").getAsString().equals(post.get("pass2").getAsString())) if (!post.get("pass").getAsString().equals(post.get("pass2").getAsString()))
@ -37,7 +46,7 @@ public class RegisterAjaxPage extends Page {
} }
User user = ManagedData.create(User.class); User user = ManagedData.create(User.class);
user.setName(post.get("name").getAsString()); user.setName(post.get("name").getAsString());
user.setEmail(post.get("email").getAsString()); user.setEmail(email);
user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/ user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/
user.setPassword(BCrypt.hashpw(post.get("pass").getAsString(), user.getSalt())); user.setPassword(BCrypt.hashpw(post.get("pass").getAsString(), user.getSalt()));
IOHelper.LoginUser(exchange, user); IOHelper.LoginUser(exchange, user);