如何获取当前经过身份验证的用户 angularfire2 (angular) 以便它可以及时用于 class 方法?
How to get current authenticated user angularfire2 (angular) so that it is usable in time for class methods?
我有很多方法需要 currentUserId。
我一直在像这样注入我的 AuthService 获取和存储用户:
// 授权服务代码
getAuthorizationState() {
return this.afAuth.authState;
}
// Feed 组件代码
constructor(...auth: AuthService...) {
this.user = this.auth.getAthorizationState();
this.user.subscribe(
(value) => {
console.log('value', value);
this.currentUser = value;
}
);
}
// This tends to throw an error can not get property uid of undefined
getUserFeed(uri: string): Obervable<any> {
return this.getFeed(`feed/${this.currentUser.uid})`)
}
// So have been resorting to this
getUserFeed(uri: string): Obervable<any> {
this.user.map((user) => {return user.uid})
.flatMap((currentUserId) => {
return this.getFeed(`feed/${currentUserId})`)
});
}
问题:必须用各种方法来解决这个问题。在 angularjs 我记得我相信使用 resolve 方法来帮助解决这个问题。
问题:是否有'app global'方法来获取和存储当前经过身份验证的用户的信息,以便在调用组件或注入器服务中的方法时它已经存在?
继续 angular gitter 并被告知我正在做的可观察链确实是要使用的模式。我稍微修改了它,并将重构以在服务与组件中使用它。
getHomeFeedPostsWrapper(): Observable<any> {
return getUserId().map((user) => {
return user.uid;
}).switchMap((userId) => {
return this.friendly.getHomeFeedPosts(userId);
});
}
/**
* Helper methods
*/
getUserId() {
return this.auth.getAuthState().map((user) => {
return user.uid;
});
}
我有很多方法需要 currentUserId。 我一直在像这样注入我的 AuthService 获取和存储用户:
// 授权服务代码
getAuthorizationState() {
return this.afAuth.authState;
}
// Feed 组件代码
constructor(...auth: AuthService...) {
this.user = this.auth.getAthorizationState();
this.user.subscribe(
(value) => {
console.log('value', value);
this.currentUser = value;
}
);
}
// This tends to throw an error can not get property uid of undefined
getUserFeed(uri: string): Obervable<any> {
return this.getFeed(`feed/${this.currentUser.uid})`)
}
// So have been resorting to this
getUserFeed(uri: string): Obervable<any> {
this.user.map((user) => {return user.uid})
.flatMap((currentUserId) => {
return this.getFeed(`feed/${currentUserId})`)
});
}
问题:必须用各种方法来解决这个问题。在 angularjs 我记得我相信使用 resolve 方法来帮助解决这个问题。
问题:是否有'app global'方法来获取和存储当前经过身份验证的用户的信息,以便在调用组件或注入器服务中的方法时它已经存在?
继续 angular gitter 并被告知我正在做的可观察链确实是要使用的模式。我稍微修改了它,并将重构以在服务与组件中使用它。
getHomeFeedPostsWrapper(): Observable<any> {
return getUserId().map((user) => {
return user.uid;
}).switchMap((userId) => {
return this.friendly.getHomeFeedPosts(userId);
});
}
/**
* Helper methods
*/
getUserId() {
return this.auth.getAuthState().map((user) => {
return user.uid;
});
}