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

This commit is contained in:
Lukas-Heiligenbrunner 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 db.jdcb;
import java.io.IOException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -11,6 +12,17 @@ public class main {
D.printList(); 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()); Thread mythread = new Thread(() -> new website.Webserver().startserver());
mythread.start(); 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 //success
if (HttpTools.StringToMD5(password).equals(s.getString("password"))) { if (HttpTools.StringToMD5(password).equals(s.getString("password"))) {
System.out.println("login success"); System.out.println("login success");
LoginState.getObject().logIn();
LoginState.getObject().setAccountData(username,"","","");
response = "{\"accept\": true}"; response = "{\"accept\": true}";
} else { } else {
System.out.println("wrong password"); 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"); 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());"); 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}"; return "{\"accept\": true}";
} }
} }

View File

@ -19,6 +19,7 @@ public class Webserver {
server.createContext("/", new MainPage()); server.createContext("/", new MainPage());
server.createContext("/senddata/loginget", new LoginRequest()); server.createContext("/senddata/loginget", new LoginRequest());
server.createContext("/senddata/registerpost",new RegisterRequest()); server.createContext("/senddata/registerpost",new RegisterRequest());
server.createContext("/senddata/checkloginstate",new CheckLoginState());
server.setExecutor(null); // creates a default executor server.setExecutor(null); // creates a default executor
server.start(); server.start();

7
wwwroot/css/profile.css Normal file
View File

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

2
wwwroot/js/profile.js Normal file
View File

@ -0,0 +1,2 @@
$(document).ready(function() {
});

View File

@ -1,3 +1,20 @@
$(document).ready(function() { $(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');
});
}); });

121
wwwroot/profile.html Normal file
View File

@ -0,0 +1,121 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="lib/bootstrap.min.css">
<!--Fontawesome CDN-->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<!-- jQuery library -->
<script src="lib/jquery.min.js"></script>
<!-- Popper JS -->
<script src="lib/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="lib/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/profile.css">
<script type="text/javascript" src="js/profile.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="settings.html">Settings</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="profile.html">Profile</a>
</li>
</ul>
</div>
<div class="mx-auto order-0">
<a class="navbar-brand mx-auto" href="#">Profile</a>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ml-auto">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"> lheilige</i></span>
</div>
<button type="button" class="btn btn-primary">Logout</button>
</ul>
</div>
</nav>
<div class="row">
<div class="col-lg-3">
</div>
<div class="col-lg-6">
<!-- .card -->
<div class="card card-fluid">
<h6 class="card-header"> Account Info</h6>
<!-- .card-body -->
<div class="card-body">
<!-- form -->
<form method="post">
<!-- form row -->
<div class="form-row">
<!-- form column -->
<div class="col-md-6 mb-3">
<label for="input01">First Name</label>
<input type="text" class="form-control" id="input01" value="Max" required=""></div>
<!-- /form column -->
<!-- form column -->
<div class="col-md-6 mb-3">
<label for="input02">Last Name</label>
<input type="text" class="form-control" id="input02" value="Mustermann" required=""></div>
<!-- /form column -->
</div>
<!-- /form row -->
<!-- .form-group -->
<div class="form-group">
<label for="input03">Email</label>
<input type="email" class="form-control" id="input03" value="Max@Musterman.com" required=""></div>
<!-- /.form-group -->
<!-- .form-group -->
<div class="form-group">
<label for="input04">New Password</label>
<input type="password" class="form-control" id="input04" value="secret" required=""></div>
<!-- /.form-group -->
<!-- .form-group -->
<div class="form-group">
<label for="input05">Username</label>
<input type="text" class="form-control" id="input05" value="bent10" required="">
</div>
<!-- /.form-group -->
<hr>
<!-- .form-actions -->
<div class="form-actions">
<!-- enable submit btn when user type their current password -->
<input type="password" class="form-control ml-auto mr-3" id="input06"
placeholder="Enter Current Password" required="">
<button type="submit" class="btn btn-primary" disabled="">Update Account</button>
</div>
<!-- /.form-actions -->
</form>
<!-- /form -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<div class="col-lg-3">
</div>
</div>
</body>
</html>

View File

@ -7,6 +7,10 @@
<!-- Latest compiled and minified CSS --> <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="lib/bootstrap.min.css"> <link rel="stylesheet" href="lib/bootstrap.min.css">
<!--Fontawesome CDN-->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<!-- jQuery library --> <!-- jQuery library -->
<script src="lib/jquery.min.js"></script> <script src="lib/jquery.min.js"></script>
@ -21,23 +25,30 @@
<script type="text/javascript" src="js/settings.js"> </script> <script type="text/javascript" src="js/settings.js"> </script>
</head> </head>
<body> <body>
<!-- Grey with black text --> <nav class="navbar navbar-expand-md navbar-dark bg-dark">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark"> <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
<ul class="navbar-nav"> <ul class="navbar-nav mr-auto">
<li class="nav-item active"> <li class="nav-item active">
<a class="nav-link" href="#">Active</a> <a class="nav-link" href="settings.html">Settings</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="#">Link</a> <a class="nav-link" href="profile.html">Profile</a>
</li> </li>
<li class="nav-item"> </ul>
<a class="nav-link" href="#">Link</a> </div>
</li> <div class="mx-auto order-0">
<li class="nav-item"> <a class="navbar-brand mx-auto" href="#">Profile</a>
<a class="nav-link disabled" href="#">Disabled</a> </div>
</li> <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
</ul> <ul class="navbar-nav ml-auto">
</nav> <div class="input-group-prepend">
<span class="input-group-text"><i id="userlabel" class="fas fa-user"> lheilige</i></span>
</div>
<button type="button" id="logoutbtn" class="btn btn-primary">Logout</button>
</ul>
</div>
</nav>
<div class="optionsarray"> <div class="optionsarray">
<div class="custom-control custom-switch"> <div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1"> <input type="checkbox" class="custom-control-input" id="customSwitch1">