使用 firebase 获取 angular 中的当前用户?
Get current user in angular using firebase?
目前正在尝试使用 ionic v2 和 firebase v3 构建应用程序。
不幸的是,我正在努力获取登录用户的详细信息。
在我的 app.ts 中,我正确连接到我的 firebaseapp 并且识别了登录用户并显示了主页:
import {Component} from '@angular/core';
import {Platform, ionicBootstrap, NavController} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HomePage} from './pages/home/home';
declare var firebase:any;
import {Data} from './providers/data/data';
import {LoginPage} from './pages/login/login';
@Component({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [Data]
})
export class MyApp {
rootPage: any = LoginPage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
// Initialize Firebase
var config = {
apiKey: "xxxx",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
storageBucket: "xxxx.appspot.com",
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// If there is a user, take him to home page.
console.log("User is logged in!");
//console.log(user);
this.rootPage = HomePage;
} else {
// If there is no user logged in, send to login page.
this.rootPage = LoginPage;
}
});
});
}
}
ionicBootstrap(MyApp);
然后在我的另一个文件中,我希望使用
检索登录用户的 ID
var authData = firebase.auth().currentUser();
我的控制台出现以下错误:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in :0:0
ORIGINAL EXCEPTION: TypeError: firebase.auth(...).currentUser is not a function
ORIGINAL STACKTRACE:
TypeError: firebase.auth(...).currentUser is not a function
at new AddItemPage (http://localhost:8100/build/js/app.bundle.js:97:40)
at DebugAppView._View_AddItemPage_Host0.createInternal (AddItemPage_Host.template.js:20:27)
at DebugAppView.AppView.create (http://localhost:8100/build/js/app.bundle.js:30016:21)
at DebugAppView.create (http://localhost:8100/build/js/app.bundle.js:30228:44)
at ComponentFactory.create (http://localhost:8100/build/js/app.bundle.js:29245:36)
at ViewContainerRef_.createComponent (http://localhost:8100/build/js/app.bundle.js:30436:45)
at http://localhost:8100/build/js/app.bundle.js:53117:47
at ZoneDelegate.invoke (http://localhost:8100/build/js/zone.js:323:29)
at Object.onInvoke (http://localhost:8100/build/js/app.bundle.js:35470:41)
at ZoneDelegate.invoke (http://localhost:8100/build/js/zone.js:322:35)
ERROR CONTEXT:
[object Object]
我很困惑为什么 currentUser() 不是一个函数,而文档说它是?
https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user
谢谢!
文档说
firebase.auth().currentUser
不是 firebase.auth().currentUser()
是获取用户
的正确方法
目前正在尝试使用 ionic v2 和 firebase v3 构建应用程序。 不幸的是,我正在努力获取登录用户的详细信息。
在我的 app.ts 中,我正确连接到我的 firebaseapp 并且识别了登录用户并显示了主页:
import {Component} from '@angular/core';
import {Platform, ionicBootstrap, NavController} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HomePage} from './pages/home/home';
declare var firebase:any;
import {Data} from './providers/data/data';
import {LoginPage} from './pages/login/login';
@Component({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [Data]
})
export class MyApp {
rootPage: any = LoginPage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
// Initialize Firebase
var config = {
apiKey: "xxxx",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
storageBucket: "xxxx.appspot.com",
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// If there is a user, take him to home page.
console.log("User is logged in!");
//console.log(user);
this.rootPage = HomePage;
} else {
// If there is no user logged in, send to login page.
this.rootPage = LoginPage;
}
});
});
}
}
ionicBootstrap(MyApp);
然后在我的另一个文件中,我希望使用
检索登录用户的 IDvar authData = firebase.auth().currentUser();
我的控制台出现以下错误:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in :0:0
ORIGINAL EXCEPTION: TypeError: firebase.auth(...).currentUser is not a function
ORIGINAL STACKTRACE:
TypeError: firebase.auth(...).currentUser is not a function
at new AddItemPage (http://localhost:8100/build/js/app.bundle.js:97:40)
at DebugAppView._View_AddItemPage_Host0.createInternal (AddItemPage_Host.template.js:20:27)
at DebugAppView.AppView.create (http://localhost:8100/build/js/app.bundle.js:30016:21)
at DebugAppView.create (http://localhost:8100/build/js/app.bundle.js:30228:44)
at ComponentFactory.create (http://localhost:8100/build/js/app.bundle.js:29245:36)
at ViewContainerRef_.createComponent (http://localhost:8100/build/js/app.bundle.js:30436:45)
at http://localhost:8100/build/js/app.bundle.js:53117:47
at ZoneDelegate.invoke (http://localhost:8100/build/js/zone.js:323:29)
at Object.onInvoke (http://localhost:8100/build/js/app.bundle.js:35470:41)
at ZoneDelegate.invoke (http://localhost:8100/build/js/zone.js:322:35)
ERROR CONTEXT:
[object Object]
我很困惑为什么 currentUser() 不是一个函数,而文档说它是? https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user
谢谢!
文档说
firebase.auth().currentUser
不是 firebase.auth().currentUser()
是获取用户