连接到 Firebase 数据库后如何处理用户注销?
How do you handle a user logout after connection to Firebase db?
我正在使用 Firebase 和 AngularFire2 库构建 Angular2 应用程序。
授权连接后用户退出怎么处理?例如,具有有效帐户的用户登录,连接到我的 Firebase 数据库的 "orders" 节点,然后用户注销。
我在控制台中收到以下错误,这很合理。但是我应该如何捕获此错误或以其他方式防止它呢?
错误:
FIREBASE WARNING: Exception was thrown by user callback. Error: permission_denied at /orders: Client doesn't have permission to access the desired data.
相关代码(我认为):
@Injectable()
export class OrderService {
private orders$: FirebaseListObservable<any>;
private _pendingOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
private _activeOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
constructor(
private af: AngularFire,
private auth: AuthService) {
this.auth.isAuthed
.subscribe((value: boolean) => {
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
// Somehow unsubscribe here, perhaps?
}
});
}
_subscribeToUserOrders(userId) {
const query = {
orderByChild: 'userId',
equalTo: userId
};
this.orders$ = this.af.database
.list(`orders`, query);
this.orders$.subscribe((orders) => {
// Load pending orders
this._pendingOrders$.next(orders.filter(o => o.status === 'PENDING'));
// Load active orders
this._activeOrders$.next(orders.filter(o => o.status === 'ACTIVE'));
});
}
get pendingOrders() {
return this._pendingOrders$.asObservable();
}
get activeOrders() {
return this._activeOrders$.asObservable();
}
}
对 this.orders$.subscribe
的调用将 return 一个 RxJS Subscription
:
import { Subscription } from 'rxjs/Subscription';
private ordersSubscription: Subscription;
...
this.ordersSubscription = this.orders$.subscribe(...);
你可以用它来取消订阅(你可能也想从你的主题中发出 null
):
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
this._unsubscribeFromUserOrders();
}
...
_unsubscribeFromUserOrders() {
this.ordersSubscription.unsubscribe();
this.orders$ = null;
this._pendingOrders$.next(null);
this._activeOrders$.next(null);
}
我正在使用 Firebase 和 AngularFire2 库构建 Angular2 应用程序。
授权连接后用户退出怎么处理?例如,具有有效帐户的用户登录,连接到我的 Firebase 数据库的 "orders" 节点,然后用户注销。
我在控制台中收到以下错误,这很合理。但是我应该如何捕获此错误或以其他方式防止它呢?
错误:
FIREBASE WARNING: Exception was thrown by user callback. Error: permission_denied at /orders: Client doesn't have permission to access the desired data.
相关代码(我认为):
@Injectable()
export class OrderService {
private orders$: FirebaseListObservable<any>;
private _pendingOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
private _activeOrders$: BehaviorSubject<any> = new BehaviorSubject(null);
constructor(
private af: AngularFire,
private auth: AuthService) {
this.auth.isAuthed
.subscribe((value: boolean) => {
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
// Somehow unsubscribe here, perhaps?
}
});
}
_subscribeToUserOrders(userId) {
const query = {
orderByChild: 'userId',
equalTo: userId
};
this.orders$ = this.af.database
.list(`orders`, query);
this.orders$.subscribe((orders) => {
// Load pending orders
this._pendingOrders$.next(orders.filter(o => o.status === 'PENDING'));
// Load active orders
this._activeOrders$.next(orders.filter(o => o.status === 'ACTIVE'));
});
}
get pendingOrders() {
return this._pendingOrders$.asObservable();
}
get activeOrders() {
return this._activeOrders$.asObservable();
}
}
对 this.orders$.subscribe
的调用将 return 一个 RxJS Subscription
:
import { Subscription } from 'rxjs/Subscription';
private ordersSubscription: Subscription;
...
this.ordersSubscription = this.orders$.subscribe(...);
你可以用它来取消订阅(你可能也想从你的主题中发出 null
):
if (this.auth.isAuthed.value) {
const userId = this.auth.getUserId();
this._subscribeToUserOrders(userId);
} else {
this._unsubscribeFromUserOrders();
}
...
_unsubscribeFromUserOrders() {
this.ordersSubscription.unsubscribe();
this.orders$ = null;
this._pendingOrders$.next(null);
this._activeOrders$.next(null);
}