无法注销 firebase 用户
can't sign out firebase user
我正在尝试退出我的 angular 应用中当前登录的用户。那是我的客户服务:
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) {}
}
和 Firebase 服务:
@Injectable({
providedIn: 'root',
})
export class FirebaseService {
public app = initializeApp(firebaseConfig);
public auth = getAuth();
}
但是当我调用 logOut 函数时出现这个错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot assign to
read only property 'currentUser' of object '[object Object]'
TypeError: Cannot assign to read only property 'currentUser' of object '[object Object]'
at index-8593558d.js:2473
at Generator.next ()
at asyncGeneratorStep (asyncToGenerator.js:3)
at _next (asyncToGenerator.js:25)
at asyncToGenerator.js:32
at new ZoneAwarePromise (zone.js:1387)
at asyncToGenerator.js:21
at AuthImpl.directlySetCurrentUser (index-8593558d.js:2466)
at index-8593558d.js:2329
at Generator.next ()
at resolvePromise (zone.js:1213)
at zone.js:1120
at zone.js:1136
at ZoneDelegate.invoke (zone.js:372)
at Object.onInvoke (core.js:28692)
at ZoneDelegate.invoke (zone.js:371)
at Zone.run (zone.js:134)
at zone.js:1276
at ZoneDelegate.invokeTask (zone.js:406)
at Object.onInvokeTask (core.js:28679)
应用程序正在从 ngrx 效果调用它,但我认为这不是问题,因为我尝试在没有 ngrx 的情况下调用它,但仍然存在此错误。
所以经过一些调查我找到了解决方案,我不确定这个问题的原因,但这可能对某人有帮助。首先,我将用户身份验证逻辑从 ngrx 效果移动到 Angular 像这样触发身份验证观察者:
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']);
}
});
}
我正在尝试退出我的 angular 应用中当前登录的用户。那是我的客户服务:
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) {}
}
和 Firebase 服务:
@Injectable({
providedIn: 'root',
})
export class FirebaseService {
public app = initializeApp(firebaseConfig);
public auth = getAuth();
}
但是当我调用 logOut 函数时出现这个错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot assign to read only property 'currentUser' of object '[object Object]' TypeError: Cannot assign to read only property 'currentUser' of object '[object Object]' at index-8593558d.js:2473 at Generator.next () at asyncGeneratorStep (asyncToGenerator.js:3) at _next (asyncToGenerator.js:25) at asyncToGenerator.js:32 at new ZoneAwarePromise (zone.js:1387) at asyncToGenerator.js:21 at AuthImpl.directlySetCurrentUser (index-8593558d.js:2466) at index-8593558d.js:2329 at Generator.next () at resolvePromise (zone.js:1213) at zone.js:1120 at zone.js:1136 at ZoneDelegate.invoke (zone.js:372) at Object.onInvoke (core.js:28692) at ZoneDelegate.invoke (zone.js:371) at Zone.run (zone.js:134) at zone.js:1276 at ZoneDelegate.invokeTask (zone.js:406) at Object.onInvokeTask (core.js:28679)
应用程序正在从 ngrx 效果调用它,但我认为这不是问题,因为我尝试在没有 ngrx 的情况下调用它,但仍然存在此错误。
所以经过一些调查我找到了解决方案,我不确定这个问题的原因,但这可能对某人有帮助。首先,我将用户身份验证逻辑从 ngrx 效果移动到 Angular 像这样触发身份验证观察者:
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']);
}
});
}