1
- import { NgModule } from '@angular/core'
1
+ import { NgModule , APP_INITIALIZER } from '@angular/core'
2
2
import { AngularFireAuth } from 'angularfire2/auth'
3
3
import { FirebaseServerAuth } from './server.auth.service'
4
4
import {
@@ -7,8 +7,11 @@ import {
7
7
} from './server.common'
8
8
import { initializeApp , credential , auth , apps } from 'firebase-admin'
9
9
import { EnvironmentService } from '../../environment'
10
- import { FIREBASE_AUTH_COOKIE_STO_KEY } from './tokens'
10
+ import { FIREBASE_AUTH_COOKIE_STO_KEY , FIREBASE_AUTH_OBJ_TS } from './tokens'
11
11
import { CookieService } from '../../cookies/browser'
12
+ import { TransferState , StateKey } from '@angular/platform-browser'
13
+ import { take , tap } from 'rxjs/operators'
14
+ import { FirebaseUniversalAuthService } from './browser.auth.service'
12
15
13
16
function firebaseAdminAppAlreadyExists ( ) {
14
17
return apps . length ? true : false
@@ -35,10 +38,30 @@ export function getUserJwt(cs: CookieService, key: string) {
35
38
return cs . get ( key )
36
39
}
37
40
41
+ export function onBootstrap (
42
+ transferState : TransferState ,
43
+ auth : FirebaseServerAuth ,
44
+ tsKey : StateKey < string >
45
+ ) {
46
+ return ( ) => {
47
+ return auth . user
48
+ . pipe (
49
+ take ( 1 ) ,
50
+ tap ( user => transferState . set ( tsKey , user ) )
51
+ )
52
+ . toPromise ( )
53
+ }
54
+ }
55
+
38
56
// tslint:disable-next-line:no-class
39
57
@NgModule ( {
40
58
providers : [
41
- { provide : AngularFireAuth , useClass : FirebaseServerAuth } ,
59
+ FirebaseServerAuth ,
60
+ { provide : AngularFireAuth , useExisting : FirebaseServerAuth } ,
61
+ {
62
+ provide : FirebaseUniversalAuthService ,
63
+ useExisting : FirebaseServerAuth
64
+ } ,
42
65
{
43
66
provide : FIREBASE_AUTH_SERVER_ADMIN_APP ,
44
67
useFactory : fbAdminFactory ,
@@ -48,6 +71,12 @@ export function getUserJwt(cs: CookieService, key: string) {
48
71
provide : FIREBASE_AUTH_SERVER_USER_JWT ,
49
72
useFactory : getUserJwt ,
50
73
deps : [ CookieService , FIREBASE_AUTH_COOKIE_STO_KEY ]
74
+ } ,
75
+ {
76
+ provide : APP_INITIALIZER ,
77
+ useFactory : onBootstrap ,
78
+ deps : [ TransferState , FirebaseServerAuth , FIREBASE_AUTH_OBJ_TS ] ,
79
+ multi : true
51
80
}
52
81
]
53
82
} )
0 commit comments