离子2检查用户是否已使用angularfire2登录

时间:2017-01-05 08:48:07

标签: facebook ionic-framework ionic2 angularfire2

我做了facebook登录到我的应用程序,我正在尝试检查用户是否登录然后转到HomePage,如果没有转到loginPage。 我用firebase做了它,但我正试图用angularfire2做到这一点 这是我的代码与firebase(我想在angularfire2中)

 firebase.auth().onAuthStateChanged((user) => {
   var that=this;
  if (user) {
    if(this.reg_boolean=="true"){
      console.log("regCompleted");  
       this.nav.setRoot(TabsPage,{
       dep:this.department,year:this.year,semester:this.semester

    })
      this.rootPage=TabsPage;
    }
    else{
    this.rootPage = SignupPage;
    }
    console.log("I'm here! HomePage");
  } else {
    this.rootPage = LoginPage;
    console.log("I'm here! LoginPage");
  }
});

1 个答案:

答案 0 :(得分:2)

我让它在app.component.ts文件中工作。

import { ViewChild } from '@angular/core';
import { Nav } from 'ionic-angular';
import { AngularFire } from 'angularfire2'; // import this, duh

import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';

export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any;
  pages: Array<{ title: string, component: any }>;
  userName:string;

  constructor(public platform: Platform,
              public af: AngularFire,
              public authService: AuthenticationService) { // used for logout

     this.initializeApp();

     // Listen for auth sub
     af.auth.subscribe(user => {
        this.rootPage = user ? HomePage : LoginPage;
     });
     ....

所以基本上听取订阅并根据传递给var rootPage的值更改user。如果您希望更具体地使用重定向,可以在console.log()上执行user