Firebase 身份验证更改侦听器永远不会在 nativescript-angular 应用程序中触发

问题描述

我在我的项目中使用了 nativescript firebase (@nativescript/firebase) 插件版本 11.1.3。我想检查用户是否在应用程序开始时登录重定向到“项目页面”或相应地登录。因此,我在 onAuthStateChanged 中包含了 firebase.init 选项,该选项在 AppComponent 的 ngInit调用

import {Component,OnInit} from "@angular/core";
import {firebase} from "@nativescript/firebase";
import {BackendService} from "./shared/services/backend.service";

@Component({
    selector: "ns-app",templateUrl: "./app.component.html"
})
export class AppComponent implements OnInit {

    ngOnInit() {

        firebase.init({
            // -- persist: true,onAuthStateChanged: (data: any) => {
                console.log(JSON.stringify(data))
                if (data.loggedIn) {
                    BackendService.token = data.user.uid;

                } else {
                    BackendService.token = "";
                }
            }
        }).then(
            () => {
                console.log("firebase.init done");
            },error => {
                console.log(`firebase.init error: ${error}`);
            }
        );
    }
}

BackendService 负责将令牌保存到应用程序设置中。下面的 onLogin() 方法驻留在登录组件中,它对用户进行身份验证并将其重定向到项目视图:

onLogin(): void {

        this.isAuthenticating = true;

        this.firebaseService.login(this.user)
            .then(result => {
                this.isAuthenticating = false;
                this.routerEx.navigate(["/items"],{clearHistory: true});
                console.log(JSON.stringify(result));
            })
            .catch(error => {
                this.isAuthenticating = false;
                alert((error.split(':')[1]))
            });
    }

我在项目组件上有一个 authentication gaurd,如果未设置“令牌”(当前的 UID),它会将用户重定向登录组件。

    canActivate() {
        if (BackendService.isLoggedIn()) {
            return true;
        } else {
            this.routerEx.navigate(["/login"]);
            return false;
        }
    }

从控制台日志中,它清楚地表明我能够成功登录,但该应用程序仍停留在登录屏幕上。事实上,它重定向items 并且 auth guard 将它重定向登录屏幕,因为 onAuthStateChanged 没有被触发并且 token 没有设置。

我已经在 firebase.addAuthStateListener(listener) 外面尝试了 onAuthStateChangedfirebase.init 并且仍然在做同样的事情。如果您认为我的问题太明显了,请多多包涵,因为我是 nativescript 的新手,并且仍在学习大部分概念。

期待中的感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...