11import { NgModule , Inject } from '@angular/core'
22import { AngularFireAuth } from 'angularfire2/auth'
3- import { flatMap } from 'rxjs/operators'
4- import { ICookieGetSet } from './app.common'
53import {
6- FIREBASE_AUTH_COOKIE_FACTORY ,
7- FIREBASE_AUTH_COOKIE_STO_KEY
8- } from './tokens'
4+ flatMap ,
5+ map ,
6+ startWith ,
7+ distinctUntilChanged ,
8+ share
9+ } from 'rxjs/operators'
10+ import { FIREBASE_AUTH_COOKIE_STO_KEY } from './tokens'
911import { of } from 'rxjs'
12+ import { DOCUMENT } from '@angular/common'
13+ import { CookieService } from '../../cookies/browser'
1014
1115// tslint:disable:no-this
1216
1317function toExtractIdTokenFromUser ( user : firebase . User | null ) {
1418 return user ? user . getIdToken ( ) : of ( undefined ) . toPromise ( )
1519}
1620
17- function storeJwtInCookies ( cs : ICookieGetSet , storageKey : string ) {
21+ function storeJwtInCookies ( cs : CookieService , storageKey : string ) {
1822 return ( jwt ?: string ) => {
1923 jwt ? cs . set ( storageKey , jwt ) : cs . remove ( storageKey )
2024 }
@@ -23,13 +27,44 @@ function storeJwtInCookies(cs: ICookieGetSet, storageKey: string) {
2327// tslint:disable-next-line:no-class
2428@NgModule ( )
2529export class FirebaseAuthBrowserModule {
30+ readonly waitingOnLoginProcess_ = this . cs
31+ . targetValueChange ( 'waitOnAuthResponse' )
32+ . pipe (
33+ map ( a => a . value ) ,
34+ startWith ( this . cs . get ( 'waitOnAuthResponse' ) ) ,
35+ map ( val => ( val && val === true ? true : false ) ) ,
36+ distinctUntilChanged ( ) ,
37+ share ( )
38+ )
39+
2640 constructor (
2741 public auth : AngularFireAuth ,
42+ private cs : CookieService ,
2843 @Inject ( FIREBASE_AUTH_COOKIE_STO_KEY ) stoKey : string ,
29- @Inject ( FIREBASE_AUTH_COOKIE_FACTORY ) getSetFactory : ICookieGetSet
44+ @Inject ( DOCUMENT ) private doc : HTMLDocument
3045 ) {
3146 auth . user
3247 . pipe ( flatMap ( toExtractIdTokenFromUser ) )
33- . subscribe ( storeJwtInCookies ( getSetFactory , stoKey ) )
48+ . subscribe ( storeJwtInCookies ( cs , stoKey ) )
49+
50+ this . waitingOnLoginProcess_ . subscribe ( v => {
51+ setTimeout ( ( ) => {
52+ const container = this . doc . querySelector (
53+ 'fng-firebase-spin-container'
54+ ) as HTMLDivElement | undefined
55+ // tslint:disable:no-if-statement
56+ if ( container ) {
57+ if ( v ) {
58+ container . style . display = 'block'
59+ } else {
60+ container . style . display = 'none'
61+ }
62+ }
63+ } , 0 )
64+ } )
65+
66+ this . auth . auth . getRedirectResult ( ) . then ( ( ) => {
67+ this . cs . remove ( 'waitOnAuthResponse' )
68+ } )
3469 }
3570}
0 commit comments