Since onAuthStateChanged
takes an observer as input, and returns the teardown function, we can simply wrap it with:
Rx.Observable.create(obs => firebase.auth().onAuthStateChanged(obs))
Actually for strange reasons this might not work, and we can do:
var onAuthStateChanged$ = Rx.Observable.create(obs => {
return firebase.auth().onAuthStateChanged(
user => obs.next(user),
err => obs.error(err),
() => obs.complete());
})
Now if you are unfamiliar with the Observable.create
function, let me explain: create
takes a onSubscribe
function that hands in an observer and returns the teardown function. Doesnt that sounds very familiar with now onAuthStateChanged
is build up? You hand in nextOrObserver
and it returns the teardown!
(Now for strange reasons nextOrObserver
did not accept an observer
for me, so i switched to giving it a next
function instead. Hench the code above.)
With the onAuthStateChanged$
set up, we can transform the stream using operators. All operators do is transform one observable into another, and RxJs has several dozen of these. In your case, it might look like this:
canActivate(): Observable<boolean> {
onAuthStateChanged$
.do(user => {if (!user) { this.router.navigate(['/login']); } })
.map(user => !!user)
.do(user => console.log('Authenticated?', user))
}