2017-10-19 50 views
2

我被困在這個問題上與來自firebase的「firestore」服務在ionic2項目上工作。ionic2 - angularfire2 - firestore:登出缺少或不足的權限

我有一個osservable可以使用異步管道從模板中的firestore獲取一些數據。 對此EndPoint的規則僅對已登錄的用戶授予讀取和寫入權限。 當我註銷時,我把重定向到登錄頁面。

..和現在就這個問題..

當我在登錄頁面登陸,幾秒鐘後,跳出IonicErrorHandler通知我有沒有足夠的權限。

so; 我如何告訴firestore osservable; 「嘿夥計,阻止它,我打電話ü後,如果有人登錄再次」

(生病嘗試befour的signOut退訂(),但不工作,也
它持久不)

重新蓋上:

當我註銷

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) 

(準確地,我recive它的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 }; 
     }); 
     }); 
    } 

} 

TKS提前向所有幫助我理解它 :)

+0

你是否設法解決這個問題?我也遇到過這個問題 –

+0

我被困在這個問題上一個星期了。希望任何人都可以幫助我! – Sifeng

+0

是否在您的登錄頁面中調用了任何集合? – Hareesh

回答

1

是,

正常情況下,這是通過取消訂閱您正在導航的組件的ngOnDestroy()中的訂閱來解決的。 這就是我這樣做的方式。

那麼對你來說,這將是:

ngOnDestroy() { 
     this.attivita$.unsubscribe(); 
} 

然而,這是非常難以確定,你應該取消的錯誤並不會給任何指示。

我添加了一個問題angularFire的開發者對您的問題:

https://github.com/angular/angularfire2/issues/1459.

這將是很好的幫助,使異常點,你在正確的方向,例如,路徑未取消訂閱或最後一個路徑段。

此外,還有用於執行此操作在這篇文章中列出的替代方法: http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/

希望有所幫助。

1

我建議您從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()), 
); 
相關問題