如何根据onAuthStateChanged的结果启动Android应用程序?

时间:2016-08-06 21:47:04

标签: android firebase firebase-authentication nativescript

我想在onAuthStateChanged结算后根据firebase.init的结果加载视图。

firebase.init({}).then(
  function (instance) {
    var listener = {
        onAuthStateChanged: function(data) {
            console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
            if (data.loggedIn) {
                console.log("User info", data.user);
                firebase.removeAuthStateListener(listener);
                application.start({ moduleName: "views/home/home" });
            }

            else {
                firebase.removeAuthStateListener(listener);
                application.start({ moduleName: "views/splash/splash" });
            }
        },
        thisArg: this
    };

    firebase.addAuthStateListener(listener);
  }, 
  function (error) {
    console.log("firebase.init error: " + error);
  }
);

但是,我收到此错误:Error: A Frame must be used to navigate to a Page.

当我使用此代码时,应用程序启动正常(尽管没有登录内容):

var firebase = require("nativescript-plugin-firebase");

firebase.init({
 }).then(
    function (instance) {
      console.log("firebase.init done");
    },
    function (error) {
      console.log("firebase.init error: " + error);
    }
);

application.start({ moduleName: "views/splash/splash" });

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

var firebase = require("nativescript-plugin-firebase");

var viewToLoad;

firebase.init({
 }).then(
    function (instance) {
      console.log("firebase.init done");
      viewToLoad = ... ; // set your view on success
    },
    function (error) {
      console.log("firebase.init error: " + error);
      viewToLoad = ... ; // set your view on error
    }
);

application.start({ moduleName: viewToLoad });