Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { NgModule, NgZone } from '@angular/core';
import { FirebaseApp, AngularFireModule } from 'angularfire2';
import { NgModule } from '@angular/core';
import { AngularFireAuth } from './auth';
import '@firebase/auth';

export function _getAngularFireAuth(app: FirebaseApp) {
return new AngularFireAuth(app);
}

export const AngularFireAuthProvider = {
provide: AngularFireAuth,
useFactory: _getAngularFireAuth,
deps: [ FirebaseApp ]
};

export const AUTH_PROVIDERS = [
AngularFireAuthProvider,
];
import { FirebaseApp } from '@firebase/app-types'

@NgModule({
imports: [ AngularFireModule ],
providers: [ AUTH_PROVIDERS ]
providers: [ AngularFireAuth ]
})
export class AngularFireAuthModule { }
4 changes: 2 additions & 2 deletions src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TestBed, inject } from '@angular/core/testing';
import { _do } from 'rxjs/operator/do';
import { take } from 'rxjs/operator/take';
import { skip } from 'rxjs/operator/skip';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { AngularFireAuth, AngularFireAuthModule } from 'angularfire2/auth';
import { COMMON_CONFIG } from './test-config';

Expand Down Expand Up @@ -51,7 +51,7 @@ describe('AngularFireAuth', () => {
});

afterEach(done => {
app.delete().then(done, done.fail);
afAuth.auth.app.delete().then(done, done.fail);
});

describe('Zones', () => {
Expand Down
42 changes: 27 additions & 15 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FirebaseAuth, User } from '@firebase/auth-types';
import { Injectable, NgZone } from '@angular/core';
import { FirebaseOptions } from '@firebase/app-types';
import { Injectable, Inject, Optional, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { observeOn } from 'rxjs/operator/observeOn';
import { FirebaseApp, ZoneScheduler } from 'angularfire2';

import { FirebaseAppConfig, FirebaseAppName, firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';

import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/of';
Expand All @@ -26,22 +28,32 @@ export class AngularFireAuth {
*/
public readonly idToken: Observable<string|null>;

constructor(public app: FirebaseApp) {
this.auth = app.auth();

const authState$ = new Observable(subscriber => {
const unsubscribe = this.auth.onAuthStateChanged(subscriber);
return { unsubscribe };
constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string
) {
const app = firebaseAppFactory(config, name);
this.auth = app.auth!();

const authStateZone = new NgZone({});
this.authState = authStateZone.runOutsideAngular(() => {
const authState$ = new Observable(subscriber => {
const unsubscribe = this.auth.onAuthStateChanged(subscriber);
return { unsubscribe };
});
return observeOn.call(authState$, new FirebaseZoneScheduler(authStateZone));
});
this.authState = observeOn.call(authState$, new ZoneScheduler(Zone.current));

const idToken$ = new Observable<User|null>(subscriber => {
const unsubscribe = this.auth.onIdTokenChanged(subscriber);
return { unsubscribe };
}).switchMap(user => {
return user ? Observable.fromPromise(user.getIdToken()) : Observable.of(null)
const idTokenZone = new NgZone({});
this.idToken = idTokenZone.runOutsideAngular(() => {
const idToken$ = new Observable(subscriber => {
const unsubscribe = this.auth.onIdTokenChanged(subscriber);
return { unsubscribe };
}).switchMap((user:User|null) => {
return user ? Observable.fromPromise(user.getIdToken()) : Observable.of(null)
});
return observeOn.call(idToken$, new FirebaseZoneScheduler(idTokenZone));
});
this.idToken = observeOn.call(idToken$, new ZoneScheduler(Zone.current));
}

}
58 changes: 16 additions & 42 deletions src/core/angularfire2.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
import { FirebaseAppConfigToken, FirebaseApp, _firebaseAppFactory } from './firebase.app.module';
import { Injectable, InjectionToken, NgModule } from '@angular/core';
import { InjectionToken, NgZone } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { Scheduler } from 'rxjs/Scheduler';
import { queue } from 'rxjs/scheduler/queue';

export interface FirebaseAppConfig {
apiKey?: string;
authDomain?: string;
databaseURL?: string;
storageBucket?: string;
messagingSenderId?: string;
projectId?: string;
}

const FirebaseAppName = new InjectionToken<string>('FirebaseAppName');
import firebase from '@firebase/app';
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';

export const FirebaseAppProvider = {
provide: FirebaseApp,
useFactory: _firebaseAppFactory,
deps: [ FirebaseAppConfigToken, FirebaseAppName ]
};

@NgModule({
providers: [ FirebaseAppProvider ],
})
export class AngularFireModule {
static initializeApp(config: FirebaseAppConfig, appName?: string) {
return {
ngModule: AngularFireModule,
providers: [
{ provide: FirebaseAppConfigToken, useValue: config },
{ provide: FirebaseAppName, useValue: appName }
]
}
}
export function firebaseAppFactory(config: FirebaseOptions, name?: string): FirebaseApp {
const appName = name || '[DEFAULT]';
const existingApp = firebase.apps.filter(app => app.name == appName)[0];
return existingApp || firebase.initializeApp(config, appName);
}

/**
* TODO: remove this scheduler once Rx has a more robust story for working
* with zones.
*/
export class ZoneScheduler {
export const FirebaseAppName = new InjectionToken<string>('angularfire2.appName');
export const FirebaseAppConfig = new InjectionToken<FirebaseOptions>('angularfire2.config');

// TODO: Correctly add ambient zone typings instead of using any.
constructor(public zone: any) {}
// Put in database.ts when we dropped depreciated
export const RealtimeDatabaseURL = new InjectionToken<string>('angularfire2.realtimeDatabaseURL');


export class FirebaseZoneScheduler {
constructor(public zone: NgZone) {}
schedule(...args: any[]): Subscription {
return <Subscription>this.zone.run(() => queue.schedule.apply(queue, args));
return <Subscription>this.zone.runGuarded(function() { return queue.schedule.apply(queue, args)});
}
}

export { FirebaseApp, FirebaseAppName, FirebaseAppConfigToken };
}
40 changes: 0 additions & 40 deletions src/core/firebase.app.module.ts

This file was deleted.

19 changes: 2 additions & 17 deletions src/database-deprecated/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { NgModule } from '@angular/core';
import { AngularFireModule, FirebaseApp } from 'angularfire2';
import { FirebaseApp } from '@firebase/app-types';
import { AngularFireDatabase } from './database';
import '@firebase/database';

export function _getAngularFireDatabase(app: FirebaseApp) {
return new AngularFireDatabase(app);
}

export const AngularFireDatabaseProvider = {
provide: AngularFireDatabase,
useFactory: _getAngularFireDatabase,
deps: [ FirebaseApp ]
};

export const DATABASE_PROVIDERS = [
AngularFireDatabaseProvider,
];

@NgModule({
imports: [ AngularFireModule ],
providers: [ DATABASE_PROVIDERS ]
providers: [ AngularFireDatabase ]
})
export class AngularFireDatabaseModule { }
19 changes: 13 additions & 6 deletions src/database-deprecated/database.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { FirebaseDatabase } from '@firebase/database-types';
import { Inject, Injectable } from '@angular/core';
import { FirebaseAppConfigToken, FirebaseAppConfig, FirebaseApp } from 'angularfire2';
import { Inject, Injectable, Optional } from '@angular/core';
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseListFactory } from './firebase_list_factory';
import { FirebaseListObservable } from './firebase_list_observable';
import { FirebaseListFactoryOpts, FirebaseObjectFactoryOpts, PathReference } from './interfaces';
import { FirebaseObjectFactory } from './firebase_object_factory';
import { FirebaseObjectObservable } from './firebase_object_observable';
import * as utils from './utils';
import { FirebaseOptions } from '@firebase/app-types';
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, firebaseAppFactory } from 'angularfire2';

@Injectable()
export class AngularFireDatabase {
Expand All @@ -16,17 +18,22 @@ export class AngularFireDatabase {
*/
database: FirebaseDatabase;

constructor(public app: FirebaseApp) {
this.database = app.database();
constructor(
@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string
) {
const app = firebaseAppFactory(config, name);
this.database = app.database!(databaseURL);
}

list(pathOrRef: PathReference, opts?:FirebaseListFactoryOpts):FirebaseListObservable<any[]> {
const ref = utils.getRef(this.app, pathOrRef);
const ref = utils.getRef(this.database, pathOrRef);
return FirebaseListFactory(ref, opts);
}

object(pathOrRef: PathReference, opts?:FirebaseObjectFactoryOpts):FirebaseObjectObservable<any> {
const ref = utils.getRef(this.app, pathOrRef);
const ref = utils.getRef(this.database, pathOrRef);
return FirebaseObjectFactory(ref, opts);
}

Expand Down
Loading