Skip to content

Commit df31f28

Browse files
fix(firebase): additional auth module config
1 parent 58cc8a1 commit df31f28

File tree

7 files changed

+96
-11
lines changed

7 files changed

+96
-11
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NgModule } from '@angular/core'
2+
import { AngularFireAuthModule } from 'angularfire2/auth'
3+
import {
4+
FIREBASE_AUTH_COOKIE_STO_KEY,
5+
FIREBASE_AUTH_COOKIE_FACTORY
6+
} from './tokens'
7+
import { CookieService } from '../../cookies/browser'
8+
9+
// tslint:disable:no-this
10+
// tslint:disable-next-line:no-class
11+
@NgModule({
12+
imports: [AngularFireAuthModule],
13+
exports: [AngularFireAuthModule],
14+
providers: [
15+
{ provide: FIREBASE_AUTH_COOKIE_FACTORY, useClass: CookieService },
16+
{ provide: FIREBASE_AUTH_COOKIE_STO_KEY, useValue: 'firebaseJWT' }
17+
]
18+
})
19+
export class FirebaseAuthAppModule {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { FirebaseAuthAppModule } from './app.auth.module'
2+
3+
export interface ICookieGetSet {
4+
readonly set: (name: string, value: string) => string
5+
readonly remove: (name: string) => void
6+
}
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1-
import { NgModule } from '@angular/core'
2-
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth'
1+
import { NgModule, Inject } from '@angular/core'
2+
import { AngularFireAuth } from 'angularfire2/auth'
3+
import { flatMap } from 'rxjs/operators'
4+
import { ICookieGetSet } from './app.common'
5+
import {
6+
FIREBASE_AUTH_COOKIE_FACTORY,
7+
FIREBASE_AUTH_COOKIE_STO_KEY
8+
} from './tokens'
9+
import { of } from 'rxjs'
10+
11+
// tslint:disable:no-this
12+
13+
function toExtractIdTokenFromUser(user: firebase.User | null) {
14+
return user ? user.getIdToken() : of(undefined).toPromise()
15+
}
16+
17+
function storeJwtInCookies(cs: ICookieGetSet, storageKey: string) {
18+
return (jwt?: string) => {
19+
jwt ? cs.set(storageKey, jwt) : cs.remove(storageKey)
20+
}
21+
}
322

423
// tslint:disable-next-line:no-class
5-
@NgModule({
6-
imports: [AngularFireAuthModule],
7-
exports: [AngularFireAuthModule]
8-
})
24+
@NgModule()
925
export class FirebaseAuthBrowserModule {
10-
constructor(public auth: AngularFireAuth) {
11-
// TODO
26+
constructor(
27+
public auth: AngularFireAuth,
28+
@Inject(FIREBASE_AUTH_COOKIE_STO_KEY) stoKey: string,
29+
@Inject(FIREBASE_AUTH_COOKIE_FACTORY) getSetFactory: ICookieGetSet
30+
) {
31+
auth.user
32+
.pipe(flatMap(toExtractIdTokenFromUser))
33+
.subscribe(storeJwtInCookies(getSetFactory, stoKey))
1234
}
1335
}

src/modules/firebase/auth/browser.common.ts

Whitespace-only changes.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { NgModule } from '@angular/core'
2-
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth'
2+
import { AngularFireAuth } from 'angularfire2/auth'
33
import { FirebaseServerAuth } from './server.auth.service'
44

55
// tslint:disable-next-line:no-class
66
@NgModule({
7-
imports: [AngularFireAuthModule],
8-
exports: [AngularFireAuthModule],
97
providers: [{ provide: AngularFireAuth, useClass: FirebaseServerAuth }]
108
})
119
export class FirebaseAuthServerModule {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// admin.initializeApp({
2+
// credential: admin.credential.cert({
3+
// projectId: 'angular-universal-consulting',
4+
// clientEmail: 'firebase-adminsdk-5dap9@angular-universal-consulting.iam.gserviceaccount.com',
5+
// privateKey: '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjbmYPKtRgkOOv\n4IVPJGw2I1h2Gf7wSdT9Ohn7mkjag659LEo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5IFKJbxBpzkrCL1\nlrf+/hDwe3aMDgC3qJIzRPnwzXpZNWl+hhC19CzGFp6t6mpgk+y+SCPjN7os/0DP\nbW3NHcytTnGITxHRzUXROmmb9M6JycA4gq1+0mGvITV3eEhg52530/0YFQKBgQC/\nVBYj1sl471zYys2FZ9x8YKThzMzzabV18/2Pi2VMXrzgJV/O7KdhIKm8QsCT7T3J\nlzCwDqiKHqt661Cyd44hBFjV0tZhnqotQZUbMJT9cyYGF3Oa2f17vkW0ntgsIg5l\njsjtgRciiOx39EDCnywTK9CP4j3lxiSxz4EvZ/1rLQKBgAbtfcy1ypdyO9bnODVn\nzzP1ldNJSXowTpqRjtCyqelGZMX+85IXb4Yg3pB+8MGbWj0pTNN5Dot3fgL+flTu\nhQJ0+OMgVXsYWHU00WyZpA0nmpXsDrZDRN6S9V1Iyyhuff3oxCP7RQvEiTKG76y5\nTr1hsyrNNkELTGx0wJioUKO1\n-----END PRIVATE KEY-----\n'
6+
// }),
7+
// databaseURL: 'https://angular-universal-consulting.firebaseio.com'
8+
// })
9+
10+
// providers: [{
11+
// provide: AngularFireAuth,
12+
// useFactory: (http: HttpClient, cs: CookieService) => {
13+
// const firebaseIdToken = cs.get('firebaseJwt')
14+
// // verify a token symmetric
15+
// // return admin.auth().verifyIdToken(cs.get('firebaseJwt'))
16+
// // .then(function (decodedToken) {
17+
// // return {
18+
// // user: of(decodedToken)
19+
// // }
20+
// // }).catch(function (error) {
21+
// // return {
22+
// // user: of(undefined)
23+
// // }
24+
// // })
25+
// return {
26+
// user: firebaseIdToken ? of(admin.auth().verifyIdToken(firebaseIdToken)).pipe(flatMap(a => a)) : of(null),
27+
// idToken: firebaseIdToken ? of(firebaseIdToken) : of(null),
28+
// }
29+
// },
30+
// deps: [HttpClient, CookieService]
31+
// }]

src/modules/firebase/auth/tokens.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ICookieGetSet } from './app.common'
2+
import { InjectionToken } from '@angular/core'
3+
4+
export const FIREBASE_AUTH_COOKIE_FACTORY = new InjectionToken<ICookieGetSet>(
5+
'fng.auth.ck.get.set'
6+
)
7+
export const FIREBASE_AUTH_COOKIE_STO_KEY = new InjectionToken<string>(
8+
'fng.auth.ck.sto'
9+
)

0 commit comments

Comments
 (0)