From 6ee360df0bd8beccd2c1d23fbe0cc2c507f59a89 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 27 Jul 2016 12:22:04 +0200 Subject: [PATCH] Registration and login are now done with JavaScript from index.html --- pages/.idea/workspace.xml | 142 +++++++++++------- pages/index.html | 57 ++++++- pages/js/login.js | 17 +-- pages/js/register.js | 17 +++ pages/js/utils.js | 13 ++ pages/register.html | 39 ----- .../norbipeti/chat/server/IOHelper.java | 4 +- src/io/github/norbipeti/chat/server/Main.java | 6 +- .../chat/server/db/DataProvider.java | 2 - .../norbipeti/chat/server/page/LoginPage.java | 6 +- .../chat/server/page/RegisterPage.java | 27 ++-- 11 files changed, 193 insertions(+), 137 deletions(-) create mode 100644 pages/js/register.js create mode 100644 pages/js/utils.js delete mode 100644 pages/register.html diff --git a/pages/.idea/workspace.xml b/pages/.idea/workspace.xml index a357d56..21f5b5b 100644 --- a/pages/.idea/workspace.xml +++ b/pages/.idea/workspace.xml @@ -2,10 +2,17 @@ + + + - + + + + + @@ -29,8 +36,8 @@ - - + + @@ -41,18 +48,28 @@ + + + + + + + + + + - + - - + + - - + + @@ -70,12 +87,14 @@ - - + + - - - + + + + + @@ -120,7 +139,10 @@ @@ -236,7 +258,7 @@ - + @@ -347,12 +369,12 @@ 1469522327377 - + - @@ -360,8 +382,8 @@ - - + + @@ -405,7 +427,7 @@ - + @@ -467,7 +489,9 @@ - + + + @@ -475,16 +499,6 @@ - - - - - - - - - - @@ -493,6 +507,14 @@ + + + + + + + + @@ -501,33 +523,51 @@ - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pages/index.html b/pages/index.html index f3b1e4e..73b4019 100644 --- a/pages/index.html +++ b/pages/index.html @@ -2,8 +2,10 @@ ChatServer + + @@ -15,10 +17,57 @@
-
- E-mail: - Password: - + + + + + + + + + + + +
E-mail: + +
Password: + +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + +
Name: + +
E-mail: + +
Password + +
Password confirm + +
+ +
diff --git a/pages/js/login.js b/pages/js/login.js index 29885a1..55045fd 100644 --- a/pages/js/login.js +++ b/pages/js/login.js @@ -1,18 +1,7 @@ /** - * Created by Norbert_Szatmari on 2016-07-26. + * Created by Norbi on 2016-07-26. */ -function getFormData($form) { - var unindexed_array = $form.serializeArray(); - var indexed_array = {}; - - $.map(unindexed_array, function (n, i) { - indexed_array[n['name']] = n['value']; - }); - - return indexed_array; -} - -function check(form) { +function login(form) { var json = JSON.stringify(getFormData($(form))); $.ajax({ url: "/login", data: json, method: "POST", success: function (result) { @@ -21,6 +10,8 @@ function check(form) { errormsg.innerHTML = result; errormsg.style = "display: block"; } + else + location.reload(true); } }); } diff --git a/pages/js/register.js b/pages/js/register.js new file mode 100644 index 0000000..eac6cc5 --- /dev/null +++ b/pages/js/register.js @@ -0,0 +1,17 @@ +/** + * Created by Norbi on 2016-07-27. + */ +function register(form) { + var json = JSON.stringify(getFormData($(form))); + $.ajax({ + url: "/register", data: json, method: "POST", success: function (result) { + if (result != "Success") { + var errormsg = document.getElementById("errormsg"); + errormsg.innerHTML = result; + errormsg.style = "display: block"; + } + else + location.reload(true); + } + }); +} diff --git a/pages/js/utils.js b/pages/js/utils.js new file mode 100644 index 0000000..6f4af7b --- /dev/null +++ b/pages/js/utils.js @@ -0,0 +1,13 @@ +/** + * Created by Norbi on 2016-07-27. + */ +function getFormData($form) { + var unindexed_array = $form.serializeArray(); + var indexed_array = {}; + + $.map(unindexed_array, function (n, i) { + indexed_array[n['name']] = n['value']; + }); + + return indexed_array; +} diff --git a/pages/register.html b/pages/register.html deleted file mode 100644 index b8df795..0000000 --- a/pages/register.html +++ /dev/null @@ -1,39 +0,0 @@ - - -

Register

-
-
- - - - - - - - - - - - - - - - - - - - - - -
Name: - -
E-mail: - -
Password - -
Password confirm - -
- -
- diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index cdb33be..5c2f97a 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -10,8 +10,6 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; -import java.util.List; -import java.util.Map.Entry; import java.util.UUID; import java.util.function.Function; @@ -56,6 +54,7 @@ public class IOHelper { return content; } + @Deprecated public static HashMap GetPOST(HttpExchange exchange) throws IOException { if (exchange.getRequestBody().available() == 0) return new HashMap<>(); @@ -81,7 +80,6 @@ public class IOHelper { return null; try { String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1); - HashMap vars = new HashMap<>(); JSONObject obj = new JSONObject(content); return obj; } catch (Exception e) { diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 7a7ef99..b3de86a 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -7,10 +7,8 @@ import java.util.Set; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.core.config.LoggerConfig; import org.reflections.Reflections; import org.reflections.scanners.SubTypesScanner; @@ -18,8 +16,6 @@ import org.reflections.util.ClasspathHelper; import org.reflections.util.ConfigurationBuilder; import com.sun.net.httpserver.HttpServer; -import com.sun.org.apache.xerces.internal.parsers.BasicParserConfiguration; - import io.github.norbipeti.chat.server.db.*; import io.github.norbipeti.chat.server.db.domain.*; import io.github.norbipeti.chat.server.page.*; @@ -36,7 +32,7 @@ public class Main { LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); - loggerConfig.setLevel(Level.DEBUG); + loggerConfig.setLevel(Level.WARN); ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig. System.out.println("Loading database..."); try (DataProvider provider = new DataProvider()) { diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index a8c2c46..ddb518b 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -1,8 +1,6 @@ package io.github.norbipeti.chat.server.db; import java.util.List; -import java.util.function.Consumer; - import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index e682d69..3dc48f0 100644 --- a/src/io/github/norbipeti/chat/server/page/LoginPage.java +++ b/src/io/github/norbipeti/chat/server/page/LoginPage.java @@ -1,10 +1,7 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; -import java.util.HashMap; - import org.json.JSONObject; -import org.jsoup.nodes.Element; import org.mindrot.jbcrypt.BCrypt; import com.sun.net.httpserver.HttpExchange; @@ -33,13 +30,12 @@ public class LoginPage extends Page { if (loginuser == null || !BCrypt.checkpw(post.getString("pass"), loginuser.getPassword())) { IOHelper.SendResponse(200, (doc) -> { doc.appendElement("p").text("The username or password is invalid."); - doc.attr("style", "display: block"); return doc; }, exchange); return; } IOHelper.LoginUser(exchange, loginuser, provider); - IOHelper.Redirect("/", exchange); + IOHelper.SendResponse(200, "Success", exchange); } catch (Exception e) { throw e; } diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index 65fa27c..c563e77 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -1,8 +1,7 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; -import java.util.HashMap; - +import org.json.JSONObject; import org.mindrot.jbcrypt.BCrypt; import com.sun.net.httpserver.HttpExchange; @@ -14,13 +13,12 @@ import io.github.norbipeti.chat.server.db.domain.User; public class RegisterPage extends Page { @Override public void handlePage(HttpExchange exchange) throws IOException { - HashMap post = IOHelper.GetPOST(exchange); - if (post.size() > 0) { + JSONObject post = IOHelper.GetPOSTJSON(exchange); + if (post != null) { String errormsg = CheckValues(post, "name", "email", "pass", "pass2"); if (errormsg.length() > 0) { final String msg = errormsg; - IOHelper.SendModifiedPage(200, this, (doc) -> doc.getElementById("errormsg").text(msg).ownerDocument(), - exchange); + IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange); return; // TODO: Use JavaScript too, for error checks } try (DataProvider provider = new DataProvider()) { @@ -34,31 +32,30 @@ public class RegisterPage extends Page { errormsg += "

The passwords don't match

"; if (errormsg.length() > 0) { final String msg = errormsg; - IOHelper.SendModifiedPage(200, this, - (doc) -> doc.getElementById("errormsg").text(msg).ownerDocument(), exchange); + IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange); return; } User user = new User(); - user.setName(post.get("name")); - user.setEmail(post.get("email")); + user.setName(post.getString("name")); + user.setEmail(post.getString("email")); user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/ - user.setPassword(BCrypt.hashpw(post.get("pass"), user.getSalt())); + user.setPassword(BCrypt.hashpw(post.getString("pass"), user.getSalt())); provider.addUser(user); User managedUser = provider.getUser(user.getId()); IOHelper.LoginUser(exchange, managedUser, provider); - IOHelper.Redirect("/", exchange); + IOHelper.SendResponse(200, "Success", exchange); } catch (Exception e) { throw e; } return; } - IOHelper.SendPage(200, this, exchange); + IOHelper.Redirect("/", exchange); } - private String CheckValues(HashMap post, String... values) { + private String CheckValues(JSONObject post, String... values) { String errormsg = ""; for (String value : values) - if (!CheckValue(post.get(value))) + if (!CheckValue(post.getString(value))) errormsg += "

" + value + " can't be empty

"; return errormsg; }