remember login state, and check if user is logged in on page
This commit is contained in:
@ -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();
|
||||
|
||||
|
22
src/website/CheckLoginState.java
Normal file
22
src/website/CheckLoginState.java
Normal 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}";
|
||||
}
|
||||
}
|
@ -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");
|
||||
|
53
src/website/LoginState.java
Normal file
53
src/website/LoginState.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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}";
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user