From 6c3e4cece21bf5a23e8d345a3698272d7fc1c8e9 Mon Sep 17 00:00:00 2001 From: Lukas-Heiligenbrunner Date: Fri, 27 Sep 2019 16:37:59 +0200 Subject: [PATCH] remember login state, and check if user is logged in on page --- src/main.java | 12 +++ src/website/CheckLoginState.java | 22 ++++++ src/website/LoginRequest.java | 2 + src/website/LoginState.java | 53 ++++++++++++++ src/website/RegisterRequest.java | 2 +- src/website/Webserver.java | 1 + wwwroot/css/profile.css | 7 ++ wwwroot/js/profile.js | 2 + wwwroot/js/settings.js | 19 ++++- wwwroot/profile.html | 121 +++++++++++++++++++++++++++++++ wwwroot/settings.html | 45 +++++++----- 11 files changed, 267 insertions(+), 19 deletions(-) create mode 100644 src/website/CheckLoginState.java create mode 100644 src/website/LoginState.java create mode 100644 wwwroot/css/profile.css create mode 100644 wwwroot/js/profile.js create mode 100644 wwwroot/profile.html diff --git a/src/main.java b/src/main.java index 920774b..b78ecb5 100644 --- a/src/main.java +++ b/src/main.java @@ -1,5 +1,6 @@ import db.jdcb; +import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; @@ -11,6 +12,17 @@ public class main { D.printList(); */ + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + Thread.sleep(200); + System.out.println("Shutting down ..."); + + //shutdown routine + } catch (InterruptedException e) { + e.printStackTrace(); + } + })); + Thread mythread = new Thread(() -> new website.Webserver().startserver()); mythread.start(); diff --git a/src/website/CheckLoginState.java b/src/website/CheckLoginState.java new file mode 100644 index 0000000..8051f2d --- /dev/null +++ b/src/website/CheckLoginState.java @@ -0,0 +1,22 @@ +package website; + +import java.util.HashMap; + +public class CheckLoginState extends PostRequest { + @Override + public String request(HashMap params) { + System.out.println("checkin login state"); + if ((params.get("action")).equals("getloginstate")){ + if (LoginState.getObject().isLoggedIn()){ + return "{\"loggedin\":true, \"username\":\""+LoginState.getObject().getUsername()+"\"}"; + }else { + return "{\"loggedin\":false}"; + } + }else if ((params.get("action")).equals("logout")){ + System.out.println("logging out"); + LoginState.getObject().logOut(); + return "{\"loggedin\":false}"; + } + return "{\"loggedin\":false}"; + } +} diff --git a/src/website/LoginRequest.java b/src/website/LoginRequest.java index 2668066..4aaf6db 100644 --- a/src/website/LoginRequest.java +++ b/src/website/LoginRequest.java @@ -22,6 +22,8 @@ public class LoginRequest extends PostRequest { //success if (HttpTools.StringToMD5(password).equals(s.getString("password"))) { System.out.println("login success"); + LoginState.getObject().logIn(); + LoginState.getObject().setAccountData(username,"","",""); response = "{\"accept\": true}"; } else { System.out.println("wrong password"); diff --git a/src/website/LoginState.java b/src/website/LoginState.java new file mode 100644 index 0000000..7921eca --- /dev/null +++ b/src/website/LoginState.java @@ -0,0 +1,53 @@ +package website; + +public class LoginState { + private LoginState() {} + + private static LoginState mythis=new LoginState(); + + public static LoginState getObject(){ + return mythis; + } + + String username; + String firstname; + String lastname; + String email; + + boolean loggedin = false; + + public void logIn(){ + loggedin=true; + } + + public void logOut(){ + loggedin=false; + } + + public void setAccountData(String username, String firstname, String lastname, String email){ + this.username=username; + this.firstname=firstname; + this.lastname=lastname; + this.email=email; + } + + public boolean isLoggedIn(){ + return loggedin; + } + + public String getUsername() { + return username; + } + + public String getFirstname() { + return firstname; + } + + public String getLastname() { + return lastname; + } + + public String getEmail() { + return email; + } +} diff --git a/src/website/RegisterRequest.java b/src/website/RegisterRequest.java index 3a23f15..652d076 100644 --- a/src/website/RegisterRequest.java +++ b/src/website/RegisterRequest.java @@ -14,7 +14,7 @@ public class RegisterRequest extends PostRequest{ jdcb myjd = new jdcb("users", "kOpaIJUjkgb9ur6S", "wasteinformation"); int s = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('"+params.get("username")+"', '"+params.get("firstname")+"', '"+params.get("lastname")+"', '"+passhash+"', '"+params.get("email")+"', current_timestamp());"); - + // TODO: 27.09.19 detect if register process was successful and reply right json return "{\"accept\": true}"; } } diff --git a/src/website/Webserver.java b/src/website/Webserver.java index 9ba0ce9..a76f5e0 100644 --- a/src/website/Webserver.java +++ b/src/website/Webserver.java @@ -19,6 +19,7 @@ public class Webserver { server.createContext("/", new MainPage()); server.createContext("/senddata/loginget", new LoginRequest()); server.createContext("/senddata/registerpost",new RegisterRequest()); + server.createContext("/senddata/checkloginstate",new CheckLoginState()); server.setExecutor(null); // creates a default executor server.start(); diff --git a/wwwroot/css/profile.css b/wwwroot/css/profile.css new file mode 100644 index 0000000..29e64e9 --- /dev/null +++ b/wwwroot/css/profile.css @@ -0,0 +1,7 @@ +html,body{ + background-image: url('../rsc/login2.jpg'); + background-size: cover; + background-repeat: no-repeat; + height: 100%; + font-family: 'Numans', sans-serif; +} diff --git a/wwwroot/js/profile.js b/wwwroot/js/profile.js new file mode 100644 index 0000000..518cb7f --- /dev/null +++ b/wwwroot/js/profile.js @@ -0,0 +1,2 @@ +$(document).ready(function() { +}); diff --git a/wwwroot/js/settings.js b/wwwroot/js/settings.js index 03c34b5..56c1d04 100644 --- a/wwwroot/js/settings.js +++ b/wwwroot/js/settings.js @@ -1,3 +1,20 @@ $(document).ready(function() { - console.log("hey from js"); + //check login state + console.log("page loaded"); + $.post('/senddata/checkloginstate','action=getloginstate',function(data){ + console.log(data); + if (data.loggedin == true) { + $("#userlabel").html(" "+data.username); + }else{ + $("#userlabel").html(" not logged in!!"); + } + },'json'); + + + + $('#logoutbtn').click(function() { + $.post('/senddata/checkloginstate','action=logout',function(data){ + console.log(data); + },'json'); + }); }); diff --git a/wwwroot/profile.html b/wwwroot/profile.html new file mode 100644 index 0000000..9a3ad9e --- /dev/null +++ b/wwwroot/profile.html @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+ +
+ +
+
Account Info
+ +
+ +
+ +
+ +
+ +
+ + +
+ +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ +
+ +
+ + + +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ + + + + diff --git a/wwwroot/settings.html b/wwwroot/settings.html index 517ad74..3ed9b8e 100644 --- a/wwwroot/settings.html +++ b/wwwroot/settings.html @@ -7,6 +7,10 @@ + + + + @@ -21,23 +25,30 @@ - - + +