我正在尝试在我的角度应用程序中注销当前登录的用户。那是我的客户服务:
export class AuthClientService {
public register(email: string, password: string): Observable<Object> {
return from(
createUserWithEmailAndPassword(this.firebaseService.auth, email, password)
);
}
public logIn(email: string, password: string): Observable<Object> {
return from(
signInWithEmailAndPassword(this.firebaseService.auth, email, password)
);
}
public logOut(): Observable<void> {
return from(signOut(this.firebaseService.auth));
}
constructor(private firebaseService: FirebaseService) {}
}
和火力基地服务:
@Injectable({
providedIn: 'root',
})
export class FirebaseService {
public app = initializeApp(firebaseConfig);
public auth = getAuth();
}
但是当我调用logOut函数时有这个错误:
错误:未捕获(在promise): TypeError:无法分配到只读属性'当前用户'的对象'[object Object]'TypeError:无法分配到只读属性'当前用户'的对象'[object Object]'在index-8593558d.js:2473在Generator.next()在asyncGeneratorStep(asyncToGenerator.js:3)在_next(asyncToGenerator.js:25)在asyncToGenerator.js:32在新的ZoneAware Promise(zone.js:1387)在asyncToGenerator.js:21在AuthImpl.DirectlySetCurrentUser(index-8593558d.js:2466)在index-8593558d.js:2329在Generator.next()在resvePromise(zone.js:1213)在zone.js:1120在zone.js:1136在ZoneRemisate.invoke(zone.js
应用程序是从ngrx效果调用它的,但我认为这不是问题,因为我试图在没有ngrx的情况下调用它,但仍然存在这个错误。
所以经过一些调查我找到了解决方案,我不确定这个问题的原因,但这可能对某人有帮助。首先,我将用户身份验证逻辑从ngrx效果移动到Angular fire auth观察者,如下所示:
public authObserver(): void {
this.angularFireAuth.user.subscribe((user) => {
if (user) {
this.store.dispatch(readCredentials({ userCredential: user }));
this.router.navigate(['/dashboard']);
}
});
}
之后我意识到错误的原因是将用户对象传递给ngrx操作函数。当我创建对象用户的深层副本并将其传递给这样的函数时,它可以正常工作。
public authObserver(): void {
this.angularFireAuth.user.subscribe((user) => {
if (user) {
let copy = JSON.parse(JSON.stringify(user));
this.store.dispatch(readCredentials({ userCredential: copy }));
this.router.navigate(['/dashboard']);
}
});
}