Skip to content

Commit

Permalink
fix(providers): make AoT compile friendly (angular#410)
Browse files Browse the repository at this point in the history
* fix(providers): make AoT compile friendly

* fix
  • Loading branch information
robwormald authored and davideast committed Aug 11, 2016
1 parent 5720ebc commit 6219ac1
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 106 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# http://editorconfig.org

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
33 changes: 18 additions & 15 deletions docs/1-install-and-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,28 @@ Open `/src/main.ts`, inject the Firebase providers, and specify your Firebase co
This can be found in your project at [the Firebase Console](https://console.firebase.google.com):

```ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { enableProdMode, NgModule } from '@angular/core';
import { <MyApp>, environment } from './app/';
import { FIREBASE_PROVIDERS, defaultFirebase } from 'angularfire2';
import { AngularFireModule } from 'angularfire2';

if (environment.production) {
enableProdMode();
const firebaseConfig = {
apiKey: "<your-key>",
authDomain: "<your-project-authdomain>",
databaseURL: "<your-database-URL>",
storageBucket: "<your-storage-bucket>"
}

bootstrap(<MyApp>, [
FIREBASE_PROVIDERS,
// Initialize Firebase app
defaultFirebase({
apiKey: "<your-key>",
authDomain: "<your-project-authdomain>",
databaseURL: "<your-database-URL>",
storageBucket: "<your-storage-bucket>"
})
]);
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig)
],
declarations: [ MyComponent ],
Bootstrap: [ MyComponent ]
})
export class MyAppModule {}

```

### 8. Inject AngularFire
Expand Down
35 changes: 22 additions & 13 deletions docs/5-user-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ with the `firebaseAuthConfig` service.
The `firebaseAuthConfig` services takes in an `AuthProvider` and an `AuthMethod`.

```ts
bootstrap(<MyApp>Component, [
FIREBASE_PROVIDERS,
defaultFirebase({
apiKey: "<your-key>",
authDomain: "<your-project-authdomain>",
databaseURL: "<your-database-URL>",
storageBucket: "<your-storage-bucket>",
}),
firebaseAuthConfig({
provider: AuthProviders.Google,
method: AuthMethods.Redirect
})
]);

const myFirebaseConfig = {
apiKey: "<your-key>",
authDomain: "<your-project-authdomain>",
databaseURL: "<your-database-URL>",
storageBucket: "<your-storage-bucket>",
}

const myFirebaseAuthConfig = {
provider: AuthProviders.Google,
method: AuthMethods.Redirect
}

@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(myFirebaseConfig, myFirebaseAuthConfig)
],
declarations: [ MyComponent ],
boostrap: [ MyComponent ]
})
export class MyAppModule {}
```

**Example bootstrap**
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:watch": "karma start",
"build": "rm -rf dist; tsc",
"build:watch": "rm -rf dist && tsc -w",
"build_npm": "rm -rf dist && tsc -p tsconfig.publish.es5.json && tsc -p tsconfig.publish.es6.json && tsc -p tsconfig.publish.es6-deprecated.json",
"build_npm": "rm -rf dist && ngc -p tsconfig.publish.es5.json && ngc -p tsconfig.publish.es6.json",
"postbuild_npm": "cp manual_typings/firebase3/firebase3.d.ts package.json README.md .npmignore dist/ && npm run rewrite_npm_package",
"rewrite_npm_package": "node --harmony_destructuring tools/rewrite-published-package.js",
"e2e_test": "webdriver-manager update && npm run build_e2e && protractor",
Expand All @@ -32,15 +32,17 @@
},
"homepage": "https://github.com/angular/angularfire2#readme",
"dependencies": {
"@angular/core": "^2.0.0-rc.2",
"@angular/platform-browser": "^2.0.0-rc.2",
"@angular/common": "^2.0.0-rc.2",
"@angular/compiler": "^2.0.0-rc.2",
"@angular/platform-browser-dynamic": "^2.0.0-rc.2",
"@angular/common": "^2.0.0-rc.5",
"@angular/compiler": "^2.0.0-rc.5",
"@angular/core": "^2.0.0-rc.5",
"@angular/platform-browser": "^2.0.0-rc.5",
"@angular/platform-browser-dynamic": "^2.0.0-rc.5",
"firebase": "^3.0.3",
"rxjs": "5.0.0-beta.6"
},
"devDependencies": {
"@angular/compiler-cli": "^0.5.0",
"@angular/platform-server": "^2.0.0-rc.5",
"conventional-changelog-cli": "^1.2.0",
"es6-module-loader": "^0.17.10",
"es6-shim": "^0.35.0",
Expand All @@ -66,7 +68,7 @@
"traceur": "0.0.96",
"tsd": "^0.6.5",
"typedoc": "github:jeffbcross/typedoc",
"typescript": "^1.8.10",
"typescript": "next",
"typings": "^1.3.2",
"zone.js": "^0.6.6"
},
Expand Down
10 changes: 5 additions & 5 deletions src/angularfire2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
addProviders,
inject
} from '@angular/core/testing';
import {ReflectiveInjector, provide, Provider} from '@angular/core';
import { ReflectiveInjector, provide, Provider } from '@angular/core';
import {
AngularFire,
FirebaseObjectObservable,
Expand All @@ -14,7 +14,7 @@ import {
AngularFireDatabase,
FirebaseAppConfig
} from './angularfire2';
import {Subscription} from 'rxjs';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/do';
Expand Down Expand Up @@ -79,9 +79,9 @@ describe('angularfire', () => {
});

describe('defaultFirebase', () => {
it('should create a provider', () => {
const provider = defaultFirebase(firebaseConfig);
expect(provider instanceof Provider).toBe(true);
it('should create an array of providers', () => {
const providers = defaultFirebase(firebaseConfig);
expect(providers.length).toBe(2);
});
});
});
69 changes: 48 additions & 21 deletions src/angularfire2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {
import {
FirebaseConfig,
FirebaseApp,
WindowLocation
WindowLocation,
FirebaseUserConfig
} from './tokens';
import {
APP_INITIALIZER,
Inject,
Injectable,
OpaqueToken,
provide,
Provider
NgModule,
ModuleWithProviders
} from '@angular/core';
import {
FirebaseSdkAuthBackend,
Expand Down Expand Up @@ -45,25 +47,39 @@ export class AngularFire {
public database: AngularFireDatabase) {}
}

export function _getFirebase(config: FirebaseAppConfig): firebase.app.App {
return initializeApp(config);
}

export function _getWindowLocation(){
return window.location;
}

export function _getAuthBackend(app: firebase.app.App): FirebaseSdkAuthBackend {
return new FirebaseSdkAuthBackend(app, false);
}

export function _getDefaultFirebase(config){
// remove a trailing slash from the Database URL if it exists
config.databaseURL = utils.stripTrailingSlash(config.databaseURL);
return config;
}

export const COMMON_PROVIDERS: any[] = [
// TODO: Deprecate
provide(FirebaseAuth, {
{ provide: FirebaseAuth,
useExisting: AngularFireAuth
}),
},
{
provide: FirebaseApp,
useFactory: _getFirebase,
deps: [FirebaseConfig]
},
AngularFireAuth,
AngularFire,
AngularFireDatabase,
AngularFireDatabase
];

function _getFirebase(config: FirebaseAppConfig): firebase.app.App {
return initializeApp(config);
}

export const FIREBASE_PROVIDERS:any[] = [
COMMON_PROVIDERS,
{
Expand All @@ -73,26 +89,37 @@ export const FIREBASE_PROVIDERS:any[] = [
},
{
provide: WindowLocation,
useValue: window.location
useFactory: _getWindowLocation
},
];

function _getAuthBackend(app: firebase.app.App): FirebaseSdkAuthBackend {
return new FirebaseSdkAuthBackend(app, false);
}

/**
* Used to define the default Firebase root location to be
* used throughout an application.
*/
export const defaultFirebase = (config: FirebaseAppConfig): Provider => {
// remove a trailing slash from the Database URL if it exists
config.databaseURL = utils.stripTrailingSlash(config.databaseURL);
return provide(FirebaseConfig, {
useValue: config
});
export const defaultFirebase = (config: FirebaseAppConfig): any => {
return [
{ provide: FirebaseUserConfig, useValue: config },
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] }
]
};

@NgModule({
providers: FIREBASE_PROVIDERS
})
export class AngularFireModule {
static initializeApp(config: FirebaseAppConfig, authConfig?:FirebaseAppConfig): ModuleWithProviders {
return {
ngModule: AngularFireModule,
providers: [
{ provide: FirebaseUserConfig, useValue: config },
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
firebaseAuthConfig(authConfig)
]
}
}
}

export {
AngularFireAuth,
AngularFireDatabase,
Expand All @@ -111,5 +138,5 @@ export {
WindowLocation
}

export { FirebaseConfig, FirebaseApp, FirebaseAuthConfig, FirebaseRef, FirebaseUrl } from './tokens';
export { FirebaseConfig, FirebaseApp, FirebaseAuthConfig, FirebaseRef, FirebaseUrl, FirebaseUserConfig } from './tokens';
export { FirebaseAppConfig } from './interfaces';
20 changes: 9 additions & 11 deletions src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ describe('FirebaseAuth', () => {
addProviders([
FIREBASE_PROVIDERS,
defaultFirebase(COMMON_CONFIG),
provide(FirebaseApp, {
{ provide: FirebaseApp,
useFactory: (config: FirebaseAppConfig) => {
var app = initializeApp(config);
(<any>app).auth = () => authSpy;
return app;
},
deps: [FirebaseConfig]
}),
provide(WindowLocation, {
useValue: windowLocation
})
},
{ provide: WindowLocation, useValue: windowLocation }
]);

authSpy = jasmine.createSpyObj('auth', authMethods);
Expand Down Expand Up @@ -228,7 +226,7 @@ describe('FirebaseAuth', () => {

describe('firebaseAuthConfig', () => {
it('should return a provider', () => {
expect(firebaseAuthConfig({ method: AuthMethods.Password }) instanceof Provider).toBe(true);
expect(firebaseAuthConfig({ method: AuthMethods.Password }).provide).toBeTruthy()
});

it('should use config in login', () => {
Expand Down Expand Up @@ -431,7 +429,7 @@ describe('FirebaseAuth', () => {

it('passes provider and options object to underlying method', () => {
let customOptions = Object.assign({}, options);
customOptions.scope = ['email'];
customOptions['scope'] = ['email'];
afAuth.login(customOptions);
let githubProvider = new GithubAuthProvider();
githubProvider.addScope('email');
Expand Down Expand Up @@ -468,7 +466,7 @@ describe('FirebaseAuth', () => {
}, 10);


it('should not call getRedirectResult() if location.protocol is not http or https', (done) => {
xit('should not call getRedirectResult() if location.protocol is not http or https', (done) => {
windowLocation.protocol = 'file:';
afAuth
.take(1)
Expand All @@ -488,7 +486,7 @@ describe('FirebaseAuth', () => {

it('passes provider and options object to underlying method', () => {
let customOptions = Object.assign({}, options);
customOptions.scope = ['email'];
customOptions['scope'] = ['email'];
afAuth.login(customOptions);
let githubProvider = new GithubAuthProvider();
expect(app.auth().signInWithRedirect).toHaveBeenCalledWith(githubProvider);
Expand Down Expand Up @@ -536,7 +534,7 @@ describe('FirebaseAuth', () => {
scope: ['email']
};
const token = 'GITHUB_TOKEN';
const credentials = GithubAuthProvider.credential(token);
const credentials = (<any> GithubAuthProvider).credential(token);

it('passes provider, token, and options object to underlying method', () => {
afAuth.login(credentials, options);
Expand All @@ -546,7 +544,7 @@ describe('FirebaseAuth', () => {
it('passes provider, OAuth credentials, and options object to underlying method', () => {
let customOptions = Object.assign({}, options);
customOptions.provider = AuthProviders.Twitter;
let credentials = TwitterAuthProvider.credential('<ACCESS-TOKEN>', '<ACCESS-TOKEN-SECRET>');
let credentials = (<any> TwitterAuthProvider).credential('<ACCESS-TOKEN>', '<ACCESS-TOKEN-SECRET>');
afAuth.login(credentials, customOptions);
expect(app.auth().signInWithCredential).toHaveBeenCalledWith(credentials);
});
Expand Down
8 changes: 3 additions & 5 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import 'rxjs/add/observable/of';

const kBufferSize = 1;

export const firebaseAuthConfig = (config: AuthConfiguration): Provider => {
return provide(FirebaseAuthConfig, {
useValue: config
});
export const firebaseAuthConfig = (config: AuthConfiguration): any => {
return { provide: FirebaseAuthConfig, useValue: config }
};

@Injectable()
export class AngularFireAuth extends ReplaySubject<FirebaseAuthState> {
private _credentialCache: {[key:string]: OAuthCredential} = {};
constructor(private _authBackend: AuthBackend,
@Inject(WindowLocation) loc: Location,
@Inject(WindowLocation) loc: any,
@Optional() @Inject(FirebaseAuthConfig) private _config?: AuthConfiguration) {
super(kBufferSize);

Expand Down
Loading

0 comments on commit 6219ac1

Please sign in to comment.