我在RxJS示例应用程序:https://github.com/ngrx/example-app中用用户状态处理状态的方式创建了一个商店
注册,登录,注销都很好.减速器和效果毫无例外地发挥作用。
问题来自初始状态(以及来自AngularFire.auth的authstate流)。我想从AngularFireAuth Subject中设置初始状态,就像他们在上一个示例应用程序:https://github.com/angular/angularfire2/blob/master/docs/5-user-authentication.md中所做的那样
在主app.component.ts中,我只是在构造函数中订阅到Af.auth,以便在用户已经登录的情况下更新存储,并且从我的商店分派 authState:
export class AppComponent implements OnInit {
showSidenav$: Observable<boolean>;
currentUser$: Observable<User>;
isLoggedIn$: Observable<boolean>;
constructor(private store: Store<fromRoot.State>,
private router: Router,
private Af: AngularFire,
public dialog: MdDialog,
) {
this.showSidenav$ = this.store.select(fromRoot.getShowSidenav);
this.currentUser$ = this.store.select(fromRoot.getCurrentUser);
this.isLoggedIn$ = this.store.select(fromRoot.getIsLoggedIn);
this.Af.auth.subscribe(authState =>
this.store.dispatch(new UpdateUserStateAction(authState)));
}
但是,当我访问该应用程序时,会显示它,但控制台中会出现以下错误:
reducer.js:22 TypeError: Illegal invocation
at http://localhost:4200/vendor.bundle.js:102121:9
at Array.forEach (native)
at deepFreeze (http://localhost:4200/vendor.bundle.js:102118:33)
at http://localhost:4200/vendor.bundle.js:102124:7
at Array.forEach (native)
at deepFreeze (http://localhost:4200/vendor.bundle.js:102118:33)
at http://localhost:4200/vendor.bundle.js:102124:7
at Array.forEach (native).....
它下面的这个:
client?ffdb:44 [WDS] Disconnected!
如果我尝试登录,它会显示如下:
core.es5.js:1084 ERROR TypeError: Cannot assign to read only property '__zone_symbol__xhrScheduled' of function 'function XMLHttpRequest() { [native code] }'
at ZoneTask.scheduleTask [as scheduleFn] (zone.js:1969)
at ZoneDelegate.webpackJsonp.662.ZoneDelegate.scheduleTask (zone.js:384)
at Object.onScheduleTask (zone.js:274)
at ZoneDelegate.webpackJsonp.662.ZoneDelegate.scheduleTask (zone.js:378)
at Zone.webpackJsonp.662.Zone.scheduleTask (zone.js:209)
at Zone.webpackJsonp.662.Zone.scheduleMacroTask (zone.js:232)
at zone.js:2014
at XMLHttpRequest.send (eval at createNamedFn (zone.js:1476), <anonymous>:3:31)
at F.send (auth.js:71)......
如何从AngularFireAuth设置存储中的初始状态?这意味着每当发生AuthStates事件时,都会将AngularFire.auth事件从AngularFire.auth发送到我的商店。
如果我将Af.auth.subscribe改为另一种方式,则会得到相同的错误:
this.Af.auth.map(authState => new
user.UpdateUserStateAction(authState)).subscribe(store);
编辑:我添加了工作流:
用户使用电子邮件登录,单击按钮会触发onSignInWithEmail(formValue)。
onSignInWithEmail(formValue) {
this.store.dispatch(new user.LogInFromPasswordAction(formValue));
}
效果接收操作,在my数据库中设置用户数据,并触发动作LogInFromPasswordSuccessAction(finalUser)
@Effect()
logInFromPassword$: Observable<Action> = this.actions$
.ofType(user.ActionTypes.LOG_IN_FROM_PASSWORD)
.flatMap((action) => Observable.fromPromise(<Promise<any>>this.userService.loginWithEmail(action.payload))
.flatMap((data) => Observable.of(this.userService.setUserData(data.auth))
.map((finalUser) => new user.LogInFromPasswordSuccessAction(finalUser))));
最后,减速机接受这一行动,并返回新的国家:
case user.ActionTypes.LOG_IN_FROM_PASSWORD_SUCCESS: {
const commingUser = action.payload;
return {
data: commingUser,
isLoggedIn: true
};
}
我存储的是我选择的用户数据,我创建了一个用户模型。
这样做的原因是为了让所有的数据和流都在同一个中央存储中,这样我就可以从"fromRoot“访问任何东西,并在同一个地方进行更新。
发布于 2017-04-20 08:38:37
看起来,来自角火基的auth对象与deepFreeze有一个问题--可能是最简单的调试方法--设置断点并逐步遍历代码。
但是通常来说,将非原始对象(在您的例子中是firebase.User
,名称为authState
)直接保存到您的商店并不是一个好的做法。相反,您应该只将相关和必需的信息保存到您的商店(可能是UserName?)。此外,要重新创建身份验证,无论如何您都需要凭证,因此拥有整个对象在这里没有多大帮助。
此外:既然this.Af.auth
已经是可观察的,为什么您需要将内容保存到您的商店,它已经可以以类似流的方式访问。
https://stackoverflow.com/questions/43501823
复制相似问题