无法将Firebase Auth与JavaFX WebEngine一起使用

时间:2018-02-08 13:39:26

标签: firebase javafx firebase-authentication

我正在使用JavaFX编写自定义Web浏览器以与Firebase身份验证配合使用。登录页面由Firebase Hosting托管,*******。firebaseapp.com域名已获得授权。

当我尝试使用Google Chrome或Safari登录时,登录脚本运行正常,所有代码都可以正常运行。

但是,当我尝试使用我的JavaFX应用程序登录时,我收到此错误,导致我无法登录:

  

网络错误(例如超时,中断连接或   已经发生了无法访问的主机。

通常在未授权域时显示。显然,显示此错误的另一个问题是使用<form>标记但我使用<div>标记作为登录页面。

这是JavaFX代码

/*
    Initally based on https://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm#CEGHBDHF
*/

public class Main extends Application {

    private Scene scene;
    @Override public void start(Stage stage) {
        stage.setTitle("Web View");
        scene = new Scene(new Browser(),750,500, Color.web("#666970"));
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        //apply the styles
        getStyleClass().add("browser");
        // load the web page
        webEngine.load("https://***.firebaseapp.com/login.html?a");
        //add the web view to the scene
        getChildren().add(browser);

        webEngine.setOnAlert((WebEvent<String> wEvent) -> { // From https://stackoverflow.com/a/32682018
            System.out.println("JS alert() message: " + wEvent.getData() );
        });
    }

    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
    }

    @Override protected double computePrefWidth(double height) {
        return 750;
    }

    @Override protected double computePrefHeight(double width) {
        return 500;
    }
}

这是login.js代码

document.addEventListener("DOMContentLoaded", function(event) {
    console.log(window.location.search);

    document.getElementById("loginBtn").addEventListener("click", function(){
        var email = document.getElementById("emailInput").value;
        var password = document.getElementById("passwordInput").value;
        firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { // Login user
            var errorCode = error.code;
            var errorMessage = error.message;
            if (errorCode == "auth/wrong-password") {
                alert("Wrong password.");
            } else {
                alert(errorMessage);
            }
            console.log(error);
        });
    });

    document.getElementById("registerBtn").addEventListener("click", function(){
        var email = document.getElementById("emailInput").value;
        var password = document.getElementById("passwordInput").value;
        firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { // Register new user
            var errorCode = error.code;
            var errorMessage = error.message;
            if (errorCode == "auth/wrong-password") {
                alert("Wrong password.");
            } else {
                alert(errorMessage);
            }
            console.log(error);
        });
    });

    $(document).keypress(function(e){ // From https://stackoverflow.com/questions/6542413/bind-enter-key-to-specific-button-on-page
        if (e.which == 13){
            $("#loginBtn").click();
        }
    });

    firebase.auth().onAuthStateChanged(function(user) {
        if (user && window.location.search != "?a") {
            window.location = "dashboard.html";
        } else if (user) {
            firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
                window.location.href = ("http://*******.cloudfunctions.net/api?type=view&token=" + idToken);
            }).catch(function(error) {
                // Handle error
            });

        }
    });
});

1 个答案:

答案 0 :(得分:0)

我通过改变

解决了这个问题
public Browser() {
    //apply the styles
    getStyleClass().add("browser");
    // load the web page
    webEngine.load("https://***.firebaseapp.com/login.html?a");
    //add the web view to the scene
    getChildren().add(browser);

    webEngine.setOnAlert((WebEvent<String> wEvent) -> { // From https://stackoverflow.com/a/32682018
        System.out.println("JS alert() message: " + wEvent.getData() );
    });
}

public Browser() {
    System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); // From https://stackoverflow.com/a/44906031/9333281

    //apply the styles
    getStyleClass().add("browser");

    // load the web page
    webEngine.load("http://localhost:5004/test1.html?a");
    //add the web view to the scene
    getChildren().add(browser);

    webEngine.setOnAlert((WebEvent<String> wEvent) -> { // From https://stackoverflow.com/a/32682018
        System.out.println("JS alert() message: " + wEvent.getData() );
    });
}

感谢您的帮助!