我在使用 firebase 的“firestore”服务处理 ionic2 项目时遇到了这个问题。
我有一个 osservable 可以使用异步管道从模板中的 firestore 获取一些数据。 此 EndPoint 上的规则仅向登录用户授予读写访问权限。 当我注销时,我将重定向到登录页面。
..现在问题来了..
当我登陆登录页面时,几秒钟后,跳出 IonicErrorHandler 通知我没有足够的权限。
所以; 我如何告诉 firestore osservable; “嘿伙计,停下来,如果有人再次登录,我稍后会调用你”
(我会在注销前尝试取消订阅()但不起作用,而且
它不是来自持久性)
回顾:
当我注销时
this.afAuth.auth.signOut();
错误:
core.es5.js:1020 ERROR Error: Missing or insufficient permissions.
at new FirestoreError (error.js:164)
at JsonProtoSerializer.fromRpcStatus (serializer.js:126)
at JsonProtoSerializer.fromWatchChange (serializer.js:517)
at PersistentListenStream.onMessage (persistent_stream.js:334)
at persistent_stream.js:270
at persistent_stream.js:247
at async_queue.js:81
at t.invoke (polyfills.js:3)
at Object.onInvoke (core.es5.js:3890)
at t.invoke (polyfills.js:3)
(准确的说,我收到了3次。准确的是集合中文档的数量)
我调用 firestore 端点的服务:
export interface Attivita {
id: string;
committente: string;
durata: number;
nome: string;
progetto: string;
userId: string;
}
@Injectable()
export class FirebaseService {
attivitaCollectionRef: AngularFirestoreCollection<Attivita>;
attivita$: Observable<Attivita[]>;
constructor(private afs: AngularFirestore,
public afAuth: AngularFireAuth ) {
}
setOsservableAttivita(uId){
this.attivitaCollectionRef = this.afs.collection('attivita', ref => {
return ref.where("userId", "==", uId)
});
this.attivita$ = this.attivitaCollectionRef.snapshotChanges().map(actions => {
return actions.map(action => {
console.log(action)
const data = action.payload.doc.data() as Attivita;
const id = action.payload.doc.id;
return { id, ...data };
});
});
}
}
提前感谢大家帮助我理解它 :)
最佳答案
我建议您查看来自 Firebase 的 authState
并且仅在您通过身份验证时从 snapshotChanges
获取。 switchMap
运算符允许您根据用户是否经过身份验证等条件在可观察对象之间切换。这是一个可能的解决方案示例。
// Assuming rxjs 5.5.0 with lettable operators
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { empty } from 'rxjs/observable/empty';
import { create } from 'rxjs/observable/create';
const actions$ = this.attivitaCollectionRef.snapshotChanges()
.map(actions => {
return actions.map(action => {
console.log(action)
const data = action.payload.doc.data() as Attivita;
const id = action.payload.doc.id;
return { id, ...data };
});
});
}),
this.attivita$ = create(
// Listen for changes in the authState
subscriber => this.afAuth.onAuthStateChanged(subscriber))
)
.pipe(
// Determine if the user is logged in
map(user => !!user),
// switchMap will unsubscribe from the previous observable
// so when isLoggedIn switches to false actions$ will be unsubscribed from
switchMap(isLoggedIn => isLoggedIn ? actions$ : empty()),
);
关于firebase - ionic2 - angularfire2 - firestore : Missing or insufficient permissions on logout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46831468/