1
1
import { NgModule , Inject } from '@angular/core'
2
2
import { AngularFireAuth } from 'angularfire2/auth'
3
- import { flatMap } from 'rxjs/operators'
4
- import { ICookieGetSet } from './app.common'
5
3
import {
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'
9
11
import { of } from 'rxjs'
12
+ import { DOCUMENT } from '@angular/common'
13
+ import { CookieService } from '../../cookies/browser'
10
14
11
15
// tslint:disable:no-this
12
16
13
17
function toExtractIdTokenFromUser ( user : firebase . User | null ) {
14
18
return user ? user . getIdToken ( ) : of ( undefined ) . toPromise ( )
15
19
}
16
20
17
- function storeJwtInCookies ( cs : ICookieGetSet , storageKey : string ) {
21
+ function storeJwtInCookies ( cs : CookieService , storageKey : string ) {
18
22
return ( jwt ?: string ) => {
19
23
jwt ? cs . set ( storageKey , jwt ) : cs . remove ( storageKey )
20
24
}
@@ -23,13 +27,44 @@ function storeJwtInCookies(cs: ICookieGetSet, storageKey: string) {
23
27
// tslint:disable-next-line:no-class
24
28
@NgModule ( )
25
29
export 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
+
26
40
constructor (
27
41
public auth : AngularFireAuth ,
42
+ private cs : CookieService ,
28
43
@Inject ( FIREBASE_AUTH_COOKIE_STO_KEY ) stoKey : string ,
29
- @Inject ( FIREBASE_AUTH_COOKIE_FACTORY ) getSetFactory : ICookieGetSet
44
+ @Inject ( DOCUMENT ) private doc : HTMLDocument
30
45
) {
31
46
auth . user
32
47
. 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
+ } )
34
69
}
35
70
}
0 commit comments