add configuration error popups
This commit is contained in:
		@@ -7,7 +7,7 @@ import java.io.IOException
 | 
			
		||||
import java.util.*
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Storeage of user information
 | 
			
		||||
 * Storage of user information
 | 
			
		||||
 * * database infos
 | 
			
		||||
 * * mqtt infos
 | 
			
		||||
 *
 | 
			
		||||
@@ -99,6 +99,7 @@ class Storage {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * check if all needed properties are set up correctly
 | 
			
		||||
     * todo real check if connections can be established
 | 
			
		||||
     */
 | 
			
		||||
    fun isEveryThingDefined(): Boolean {
 | 
			
		||||
        return (mqttServer != "" &&
 | 
			
		||||
 
 | 
			
		||||
@@ -23,14 +23,8 @@ class MainPage : HttpHandler {
 | 
			
		||||
 | 
			
		||||
        debug("looking for: $path")
 | 
			
		||||
        if (path.contains(".html")) {
 | 
			
		||||
            // check if db and mqtt is configured properly
 | 
			
		||||
            // todo check real connection / not only if filled in
 | 
			
		||||
            if (!Storage.getInstance().isEveryThingDefined()) {
 | 
			
		||||
                sendPage("/config.html", t)
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page
 | 
			
		||||
            // if not logged in allow only register and index page!
 | 
			
		||||
            if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") {
 | 
			
		||||
                sendPage(path, t)
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
@@ -38,7 +32,8 @@ class MainPage : HttpHandler {
 | 
			
		||||
                sendPage("/index.html", t)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else { //only detect login state on html pages
 | 
			
		||||
        else {
 | 
			
		||||
            //only detect login state on html pages
 | 
			
		||||
            sendPage(path, t)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package com.wasteinformationserver.website.datarequests.login
 | 
			
		||||
import com.wasteinformationserver.basicutils.Log.Log.debug
 | 
			
		||||
import com.wasteinformationserver.basicutils.Log.Log.error
 | 
			
		||||
import com.wasteinformationserver.basicutils.Log.Log.message
 | 
			
		||||
import com.wasteinformationserver.basicutils.Storage
 | 
			
		||||
import com.wasteinformationserver.db.JDBC
 | 
			
		||||
import com.wasteinformationserver.website.HttpTools.Companion.StringToMD5
 | 
			
		||||
import com.wasteinformationserver.website.basicrequest.PostRequest
 | 
			
		||||
@@ -11,9 +12,11 @@ import java.sql.SQLException
 | 
			
		||||
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
 | 
			
		||||
 * - replies right error messages or the success login
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lukas Heiligenbrunner
 | 
			
		||||
 */
 | 
			
		||||
class LoginRequest : PostRequest() {
 | 
			
		||||
    override fun request(params: HashMap<String, String>): String {
 | 
			
		||||
@@ -26,24 +29,36 @@ class LoginRequest : PostRequest() {
 | 
			
		||||
            error("no connection to db")
 | 
			
		||||
            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'")
 | 
			
		||||
 | 
			
		||||
        debug("successfully logged in to db")
 | 
			
		||||
        var response = "{\"accept\": false}"
 | 
			
		||||
        try {
 | 
			
		||||
            s.last()
 | 
			
		||||
            if (s.row == 1) { //success
 | 
			
		||||
            if (s.row == 1) {
 | 
			
		||||
                //success
 | 
			
		||||
                if (StringToMD5(password!!) == s.getString("password")) {
 | 
			
		||||
                    debug("login success")
 | 
			
		||||
                    LoginState.getObject().logIn()
 | 
			
		||||
                    LoginState.getObject().setAccountData(username, s.getString("firstName"), s.getString("secondName"), s.getString("email"), s.getInt("permission"))
 | 
			
		||||
                    response = "{\"accept\": true}"
 | 
			
		||||
                } else {
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    debug("wrong password")
 | 
			
		||||
                }
 | 
			
		||||
            } else if (s.row == 0) { //user not found
 | 
			
		||||
            }
 | 
			
		||||
            else if (s.row == 0) {
 | 
			
		||||
                //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")
 | 
			
		||||
            }
 | 
			
		||||
            debug("rowcount: " + s.row)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,43 +2,39 @@ $(document).ready(function () {
 | 
			
		||||
    $('#loginbtn').click(function (e) {
 | 
			
		||||
        e.preventDefault();
 | 
			
		||||
        console.log("clicked login button");
 | 
			
		||||
        var username = $("#userfield")[0].value;
 | 
			
		||||
        var password = $("#passfield")[0].value;
 | 
			
		||||
        const username = $("#userfield")[0].value;
 | 
			
		||||
        const password = $("#passfield")[0].value;
 | 
			
		||||
 | 
			
		||||
        $.post('/senddata/loginget', 'username=' + username + '&password=' + password, function (data) {
 | 
			
		||||
 | 
			
		||||
            console.log(data);
 | 
			
		||||
            if (data.status == "nodbconn"){
 | 
			
		||||
                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. ')
 | 
			
		||||
            // todo parse different errors here with popups
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
            if (data.accept == true) {
 | 
			
		||||
                console.log("successfully logged in!");
 | 
			
		||||
                document.cookie = "username=" + username;
 | 
			
		||||
                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');
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //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();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user