在Angular 8单页应用程序中重新渲染FirebaseUI Auth小部件不起作用

问题描述 投票:0回答:1

我使用电子邮件和密码登录创建了一个新的Angular 8 CLI项目,该项目已集成到FirebaseUI Auth中。用户注销后,FirebaseUI身份验证小部件将不会显示。这是错误还是我的文档中缺少什么?

该应用程序是单页应用程序。

我已经根据app.module.ts中的文档注册了Firebase:

// Firebase App (the core Firebase SDK) is always required and must be listed first
import * as firebase from "firebase/app";

// If you enabled Analytics in your project, add the Firebase SDK for Analytics
import "firebase/analytics";

// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";

// Initialize Firebase
firebase.initializeApp(environment.firebaseConfig);

frontpage.component.html]中,我已经根据文档实现了FirebaseUI小部件:

<h1>Welcome - User is logged out!</h1>
<div id="firebaseui-auth-container"></div>
<div id="loader">Loading...</div>

frontpage.component.ts

中,我有:
import { Component, OnInit } from '@angular/core';
import * as firebase from 'firebase/app';
import * as firebaseui from 'firebaseui';
import { Router } from '@angular/router';

@Component({
  selector: 'app-frontpage',
  templateUrl: './frontpage.component.html',
  styleUrls: ['./frontpage.component.less']
})
export class FrontpageComponent implements OnInit {

  ui: firebaseui.auth.AuthUI

  constructor(private router: Router) { 

  }

  ngOnInit() {

    if(firebaseui.auth.AuthUI.getInstance("[DEFAULT]") === null){
      // Initialize the FirebaseUI Widget using Firebase.
      this.ui = new firebaseui.auth.AuthUI(firebase.auth());
    }else{
      this.ui = firebaseui.auth.AuthUI.getInstance("[DEFAULT]");
    }

    var firebaseUIConfig = {
      callbacks: {
        signInSuccessWithAuthResult: function (authResult, redirectUrl) {
          // User successfully signed in.
          // Return type determines whether we continue the redirect automatically
          // or whether we leave that to developer to handle.

          //Manual override to let firebase.auth().onAuthStateChanged handle routing (instead of signInSuccessUrl shown below)
          return false;

          //return true;
        },
        uiShown: function () {
          // The widget is rendered.
          // Hide the loader.
          document.getElementById('loader').style.display = 'none';
        }
      },
      // Will use popup for IDP Providers sign-in flow instead of the default, redirect.
      signInFlow: 'popup',
      signInSuccessUrl: '<url-to-redirect-to-on-success>',
      signInOptions: [
        // Leave the lines as is for the providers you want to offer your users.
        //firebase.auth.GoogleAuthProvider.PROVIDER_ID,
        //firebase.auth.FacebookAuthProvider.PROVIDER_ID,
        //firebase.auth.TwitterAuthProvider.PROVIDER_ID,
        //firebase.auth.GithubAuthProvider.PROVIDER_ID,
        //firebase.auth.EmailAuthProvider.PROVIDER_ID,
        {
          provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
          requireDisplayName: false
        }
        //firebase.auth.PhoneAuthProvider.PROVIDER_ID
      ],
      // Terms of service url.
      tosUrl: '<your-tos-url>',
      // Privacy policy url.
      privacyPolicyUrl: '<your-privacy-policy-url>'
    };

    this.ui.reset(); // according to https://github.com/firebase/firebaseui-web#tips-for-initializing-a-new-ui-instance-with-the-same-auth-instance

    // The start method will wait until the DOM is loaded.
    this.ui.start('#firebaseui-container', firebaseUIConfig);
  }

  ngOnDestroy() {

    // according to https://github.com/firebase/firebaseui-web#050
    this.ui.reset(); // according to https://github.com/firebase/firebaseui-web#tips-for-initializing-a-new-ui-instance-with-the-same-auth-instance
    this.ui.delete();
  }  

}

user-profile.component.html

<h1>Welcome - User is logged in!</h1>
<button mat-raised-button color="primary" (click)="signOut()">Sign out</button>

user-profile.component.ts

import { Component, OnInit } from '@angular/core';
import * as firebase from 'firebase/app';

@Component({
  selector: 'app-user-profile',
  templateUrl: './user-profile.component.html',
  styleUrls: ['./user-profile.component.less']
})
export class UserProfileComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  signOut(){
    firebase.auth().signOut();
  }
}

authentication.service.ts

注入到app.component.ts中,其中,对Auth对象中的更改调用firebase.auth()。onAuthStateChanged-结果是路由:] >
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import * as firebase from 'firebase/app';


@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {

  isLoggedIn = false;

  constructor(private router: Router) {

    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        // User is signed in.

        this.isLoggedIn = true;
        this.router.navigate(['/profile']);
      } else {
        // No user is signed in.

        this.isLoggedIn = false;
        this.router.navigate(['/frontpage']);
      }
    });

  }
}

我使用电子邮件和密码登录创建了一个新的Angular 8 CLI项目,该项目已集成到FirebaseUI Auth中。用户注销后,FirebaseUI身份验证小部件将不会显示。这是错误还是我...

angular firebase firebase-authentication single-page-application firebaseui
1个回答
0
投票

您应该考虑使用AngularFireFirebase集成到Angular应用中。

要安装:yarn add @angular/fire

© www.soinside.com 2019 - 2024. All rights reserved.