add configuration error popups

This commit is contained in:
lukas-heiligenbrunner 2020-04-15 15:49:04 +02:00
parent b9058dcdba
commit cf9069e489
4 changed files with 50 additions and 43 deletions

View File

@ -7,7 +7,7 @@ import java.io.IOException
import java.util.* import java.util.*
/** /**
* Storeage of user information * Storage of user information
* * database infos * * database infos
* * mqtt infos * * mqtt infos
* *
@ -99,6 +99,7 @@ class Storage {
/** /**
* check if all needed properties are set up correctly * check if all needed properties are set up correctly
* todo real check if connections can be established
*/ */
fun isEveryThingDefined(): Boolean { fun isEveryThingDefined(): Boolean {
return (mqttServer != "" && return (mqttServer != "" &&

View File

@ -23,14 +23,8 @@ class MainPage : HttpHandler {
debug("looking for: $path") debug("looking for: $path")
if (path.contains(".html")) { if (path.contains(".html")) {
// check if db and mqtt is configured properly // if not logged in allow only register and index page!
// todo check real connection / not only if filled in if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") {
if (!Storage.getInstance().isEveryThingDefined()) {
sendPage("/config.html", t)
return
}
if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page
sendPage(path, t) sendPage(path, t)
} }
else { else {
@ -38,7 +32,8 @@ class MainPage : HttpHandler {
sendPage("/index.html", t) sendPage("/index.html", t)
} }
} }
else { //only detect login state on html pages else {
//only detect login state on html pages
sendPage(path, t) sendPage(path, t)
} }
} }

View File

@ -3,6 +3,7 @@ package com.wasteinformationserver.website.datarequests.login
import com.wasteinformationserver.basicutils.Log.Log.debug import com.wasteinformationserver.basicutils.Log.Log.debug
import com.wasteinformationserver.basicutils.Log.Log.error import com.wasteinformationserver.basicutils.Log.Log.error
import com.wasteinformationserver.basicutils.Log.Log.message import com.wasteinformationserver.basicutils.Log.Log.message
import com.wasteinformationserver.basicutils.Storage
import com.wasteinformationserver.db.JDBC import com.wasteinformationserver.db.JDBC
import com.wasteinformationserver.website.HttpTools.Companion.StringToMD5 import com.wasteinformationserver.website.HttpTools.Companion.StringToMD5
import com.wasteinformationserver.website.basicrequest.PostRequest import com.wasteinformationserver.website.basicrequest.PostRequest
@ -11,9 +12,11 @@ import java.sql.SQLException
import java.util.* import java.util.*
/** /**
* request handler of new login request of user * request handler of new user login requests
* - checks the truth of username and password * - checks the truth of username and password
* - replies right error messages or the success login * - replies right error messages or the success login
*
* @author Lukas Heiligenbrunner
*/ */
class LoginRequest : PostRequest() { class LoginRequest : PostRequest() {
override fun request(params: HashMap<String, String>): String { override fun request(params: HashMap<String, String>): String {
@ -26,24 +29,36 @@ class LoginRequest : PostRequest() {
error("no connection to db") error("no connection to db")
return "{\"status\" : \"nodbconn\"}" return "{\"status\" : \"nodbconn\"}"
} }
if (!Storage.getInstance().isEveryThingDefined()) {
error("config not configured correctly")
return "{\"status\" : \"conferror\"}"
}
val s = jdbc.executeQuery("select * from user where username ='$username'") val s = jdbc.executeQuery("select * from user where username ='$username'")
debug("successfully logged in to db") debug("successfully logged in to db")
var response = "{\"accept\": false}" var response = "{\"accept\": false}"
try { try {
s.last() s.last()
if (s.row == 1) { //success if (s.row == 1) {
//success
if (StringToMD5(password!!) == s.getString("password")) { if (StringToMD5(password!!) == s.getString("password")) {
debug("login success") debug("login success")
LoginState.getObject().logIn() LoginState.getObject().logIn()
LoginState.getObject().setAccountData(username, s.getString("firstName"), s.getString("secondName"), s.getString("email"), s.getInt("permission")) LoginState.getObject().setAccountData(username, s.getString("firstName"), s.getString("secondName"), s.getString("email"), s.getInt("permission"))
response = "{\"accept\": true}" response = "{\"accept\": true}"
} else { }
else {
debug("wrong password") debug("wrong password")
} }
} else if (s.row == 0) { //user not found }
else if (s.row == 0) {
//user not found
debug("user not found") debug("user not found")
} else { //internal error two users with same name...? }
else {
//internal error two users with same name...?
error("there seem to be two users with same name") error("there seem to be two users with same name")
} }
debug("rowcount: " + s.row) debug("rowcount: " + s.row)

View File

@ -2,43 +2,39 @@ $(document).ready(function () {
$('#loginbtn').click(function (e) { $('#loginbtn').click(function (e) {
e.preventDefault(); e.preventDefault();
console.log("clicked login button"); console.log("clicked login button");
var username = $("#userfield")[0].value; const username = $("#userfield")[0].value;
var password = $("#passfield")[0].value; const password = $("#passfield")[0].value;
$.post('/senddata/loginget', 'username=' + username + '&password=' + password, function (data) { $.post('/senddata/loginget', 'username=' + username + '&password=' + password, function (data) {
console.log(data); console.log(data);
if (data.status == "nodbconn"){ // todo parse different errors here with popups
Swal.fire({
type: "error",
title: 'No connection to Database',
html: 'Setup DB here --> <a href="index.html">click<a/>.',
}).then((result) => {
console.log('Popup closed. ')
});
}
if (data.accept == true) { if (data.accept == true) {
console.log("successfully logged in!"); console.log("successfully logged in!");
document.cookie = "username=" + username;
window.location = 'dashboard.html'; window.location = 'dashboard.html';
} else {
if (data.status == "nodbconn") {
Swal.fire({
icon: "error",
title: 'No connection to Database',
html: 'Setup DB in config file!.',
});
} else if (data.status == "conferror") {
Swal.fire({
icon: "error",
title: 'Not configured correctly',
html: 'Please edit settings.prop and restart the server!',
});
} else {
Swal.fire({
icon: "error",
title: 'Wrong login data',
html: 'Maybe a typo in your password?',
});
}
} }
}, 'json'); }, 'json');
}); });
//register pwa
async function registerSW() {
console.log("registering service worker!");
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('/sw.js');
} catch (e) {
console.log(`SW registration failed`);
}
}
}
registerSW();
}); });