remember login state, and check if user is logged in on page

This commit is contained in:
2019-09-27 16:37:59 +02:00
parent 06c24185d2
commit 6c3e4cece2
11 changed files with 267 additions and 19 deletions

View File

@ -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();

View File

@ -0,0 +1,22 @@
package website;
import java.util.HashMap;
public class CheckLoginState extends PostRequest {
@Override
public String request(HashMap<String, String> 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}";
}
}

View File

@ -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");

View File

@ -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;
}
}

View File

@ -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}";
}
}

View File

@ -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();