简体   繁体   English

AngularFire2 v5 角度认证

[英]AngularFire2 v5 Angular Authentication

I am having a few problems with an Angular project im working on.我在处理一个 Angular 项目时遇到了一些问题。

I decided to use Angular7 with Firebase (AngularFire v5)我决定将 Angular7 与 Firebase (AngularFire v5) 一起使用

In my AuthService在我的 AuthService

export class AuthService {
private authState: Observable<firebase.User>;
public currentUser: firebase.User;
private profileDoc: AngularFirestoreDocument<Item>;
private profile$: Observable<Item>;

export class AuthService {
private authState: Observable<firebase.User>;
public currentUser: firebase.User;
private profileDoc: AngularFirestoreDocument<Item>;
private profile$: Observable<Item>;

constructor(public afAuth: AngularFireAuth, private afs: AngularFirestore) {
    this.authState = this.afAuth.authState;
    this.afAuth.authState.subscribe((user: firebase.User) => {
        this.currentUser = user;
        if(user !== null && user.uid) {
            this.profileDoc = this.afs.doc<Item>('users/' + user.uid);
            this.profile$ = this.profileDoc.valueChanges();
        }
    });
}

get profile() : Observable<Item> {
    return this.profile$;
}
}

I am trying to get a firestore reference to the users profile when they have logged in, and on the frontend in a component show the firstname and lastname of the user.我试图在用户登录时获取对用户配置文件的 firestore 引用,并在组件的前端显示用户的名字和姓氏。

But when i try and access the profile to subscribe to it:但是当我尝试访问个人资料以订阅它时:

 this._auth.profile.subscribe((profile) => {
        console.log("Profile", profile);
 });

It gives me errors.它给了我错误。

ERROR TypeError: Cannot read property 'subscribe' of undefined错误类型错误:无法读取未定义的属性“订阅”

You can create a function where in you use the AngularFireAuth service to directly get the uid of the user and then make a query to the firestore something like this您可以创建一个函数,在该函数中使用 AngularFireAuth 服务直接获取用户的 uid,然后像这样对 Firestore 进行查询

constructor(private afAuth: AngularFireAuth, private afs: AngularFireStore) {}

getProfile() {
   const userId = this.afAuth.auth.currentUser.uid;
   const doc = this.afs.doc('path' + userId );
   return doc.valueChanges();
   // then subscribe to this in the component
}

hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM