Skip to content

Commit d0c1287

Browse files
fix(firebase): add FirebaseUniversalAuthService
1 parent e2909ed commit d0c1287

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

src/modules/firebase/auth/app.auth.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { NgModule } from '@angular/core'
22
import { AngularFireAuthModule } from 'angularfire2/auth'
3+
import { CookieService } from '../../cookies/browser'
4+
import { FirebaseUniversalAuthService } from './browser.auth.service'
35
import {
46
FIREBASE_AUTH_COOKIE_STO_KEY,
57
FIREBASE_AUTH_COOKIE_FACTORY
68
} from './tokens'
7-
import { CookieService } from '../../cookies/browser'
89

910
// tslint:disable:no-this
1011
// tslint:disable-next-line:no-class
1112
@NgModule({
1213
imports: [AngularFireAuthModule],
1314
exports: [AngularFireAuthModule],
1415
providers: [
16+
FirebaseUniversalAuthService,
1517
{ provide: FIREBASE_AUTH_COOKIE_FACTORY, useClass: CookieService },
1618
{ provide: FIREBASE_AUTH_COOKIE_STO_KEY, useValue: 'firebaseJWT' }
1719
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { AngularFireAuth } from 'angularfire2/auth'
2+
import { Injectable, Inject } from '@angular/core'
3+
import { startWith } from 'rxjs/operators'
4+
import { TransferState, StateKey } from '@angular/platform-browser'
5+
import { FIREBASE_AUTH_OBJ_TS } from './tokens'
6+
7+
// tslint:disable:no-this
8+
// tslint:disable-next-line:no-class
9+
@Injectable()
10+
export class FirebaseUniversalAuthService {
11+
constructor(
12+
private auth: AngularFireAuth,
13+
private ts: TransferState,
14+
@Inject(FIREBASE_AUTH_OBJ_TS) private tsKey: StateKey<any>
15+
) {}
16+
17+
readonly fbAuth = this.auth
18+
readonly user = this.auth.user.pipe(
19+
startWith(this.ts.get(this.tsKey, undefined))
20+
)
21+
}

src/modules/firebase/auth/server.auth.module.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule } from '@angular/core'
1+
import { NgModule, APP_INITIALIZER } from '@angular/core'
22
import { AngularFireAuth } from 'angularfire2/auth'
33
import { FirebaseServerAuth } from './server.auth.service'
44
import {
@@ -7,8 +7,11 @@ import {
77
} from './server.common'
88
import { initializeApp, credential, auth, apps } from 'firebase-admin'
99
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'
1111
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'
1215

1316
function firebaseAdminAppAlreadyExists() {
1417
return apps.length ? true : false
@@ -35,10 +38,30 @@ export function getUserJwt(cs: CookieService, key: string) {
3538
return cs.get(key)
3639
}
3740

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+
3856
// tslint:disable-next-line:no-class
3957
@NgModule({
4058
providers: [
41-
{ provide: AngularFireAuth, useClass: FirebaseServerAuth },
59+
FirebaseServerAuth,
60+
{ provide: AngularFireAuth, useExisting: FirebaseServerAuth },
61+
{
62+
provide: FirebaseUniversalAuthService,
63+
useExisting: FirebaseServerAuth
64+
},
4265
{
4366
provide: FIREBASE_AUTH_SERVER_ADMIN_APP,
4467
useFactory: fbAdminFactory,
@@ -48,6 +71,12 @@ export function getUserJwt(cs: CookieService, key: string) {
4871
provide: FIREBASE_AUTH_SERVER_USER_JWT,
4972
useFactory: getUserJwt,
5073
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
5180
}
5281
]
5382
})

src/modules/firebase/auth/server.auth.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Injectable, Inject } from '@angular/core'
22
import {
33
FIREBASE_AUTH_SERVER_ADMIN_APP,
4-
FIREBASE_AUTH_SERVER_USER_JWT,
5-
FIREBASE_AUTH_OBJ_TS
4+
FIREBASE_AUTH_SERVER_USER_JWT
65
} from './server.common'
76
import { auth } from 'firebase-admin'
87
import { of } from 'rxjs'
98
import { flatMap, catchError, map, tap } from 'rxjs/operators'
109
import { TransferState } from '@angular/platform-browser'
10+
import { FIREBASE_AUTH_OBJ_TS } from './tokens'
1111

1212
function validateToken(auth: auth.Auth, jwt: string) {
1313
return of(auth.verifyIdToken(jwt))
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { InjectionToken } from '@angular/core'
22
import { auth } from 'firebase-admin'
3-
import { makeStateKey } from '@angular/platform-browser'
43

54
export const FIREBASE_AUTH_SERVER_ADMIN_APP = new InjectionToken<auth.Auth>(
65
'fng.fb.svr.auth.admin'
76
)
87
export const FIREBASE_AUTH_SERVER_USER_JWT = new InjectionToken<string>(
98
'fng.fb.svr.auth.jwt'
109
)
11-
12-
export const FIREBASE_AUTH_OBJ_TS = makeStateKey('fng.fb.auth.ts')

src/modules/firebase/auth/tokens.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { ICookieGetSet } from './app.common'
22
import { InjectionToken } from '@angular/core'
3+
import { makeStateKey } from '@angular/platform-browser'
34

45
export const FIREBASE_AUTH_COOKIE_FACTORY = new InjectionToken<ICookieGetSet>(
56
'fng.auth.ck.get.set'
67
)
78
export const FIREBASE_AUTH_COOKIE_STO_KEY = new InjectionToken<string>(
89
'fng.auth.ck.sto'
910
)
11+
export const FIREBASE_AUTH_OBJ_TS = makeStateKey('fng.fb.auth.ts')

0 commit comments

Comments
 (0)