diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index c5af0da..6fb57a3 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -5,6 +5,8 @@ import java.util.HashMap; import com.sun.net.httpserver.HttpExchange; import io.github.norbipeti.chat.server.IOHelper; +import io.github.norbipeti.chat.server.db.DataProvider; +import io.github.norbipeti.chat.server.db.domain.User; public class RegisterPage extends Page { @Override @@ -12,14 +14,27 @@ public class RegisterPage extends Page { HashMap post = IOHelper.GetPOST(exchange); if (post.size() > 0) { String errormsg = CheckValues(post, "name", "email", "pass", "pass2"); - if (errormsg.length() == 0) { - // Process register - String successmsg = ""; - IOHelper.SendModifiedPage(200, this, "", successmsg, exchange); - return; // TODO: Only show tag when needed - } else + if (errormsg.length() > 0) { IOHelper.SendModifiedPage(200, this, "", errormsg, exchange); - return; + return; // TODO: Use JavaScript too, for error checks + } + String successmsg = ""; + try (DataProvider provider = new DataProvider()) { + for (User user : provider.getUsers()) { + if (post.get("email").equals(user.getEmail())) { + errormsg += "

An user with this name already exists

"; + break; + } + } + if (!post.get("pass").equals(post.get("pass2"))) + errormsg += "

The passwords don't match

"; + } + if (errormsg.length() > 0) { + IOHelper.SendModifiedPage(200, this, "", errormsg, exchange); + return; + } + IOHelper.SendModifiedPage(200, this, "", successmsg, exchange); + return; // TODO: Only show tag when needed } IOHelper.SendPage(200, this, exchange); }