Skip to content

Commit 73a3e26

Browse files
committed
feat(module): Add a custom FirebaseApp name
1 parent 8df96d1 commit 73a3e26

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

docs/1-install-and-setup.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You should see a message that says *App works!*
5252
npm install angularfire2 firebase --save
5353
```
5454

55-
Now that you have a new project setup, install AngularFire 2 and Firebase from npm.
55+
Now that you have a new project setup, install AngularFire2 and Firebase from npm.
5656

5757
### 4. Setup @NgModule
5858

@@ -86,6 +86,22 @@ export class AppModule {}
8686

8787
```
8888

89+
### Custom FirebaseApp Names
90+
You can optionally provide a custom FirebaseApp name with `initializeApp`.
91+
92+
```ts
93+
@NgModule({
94+
imports: [
95+
BrowserModule,
96+
AngularFireModule.initializeApp(firebaseConfig, authConfig, 'my-app-name')
97+
],
98+
declarations: [ AppComponent ],
99+
bootstrap: [ AppComponent ]
100+
})
101+
export class AppModule {}
102+
```
103+
104+
89105
### 5. Inject AngularFire
90106

91107
Open `/src/app/app.component.ts`, and make sure to modify/delete any tests to get the sample working (tests are still important, you know):

docs/5-user-authentication.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ const myFirebaseConfig = {
2222
databaseURL: '<your-database-URL>',
2323
storageBucket: '<your-storage-bucket>',
2424
messagingSenderId: '<your-messaging-sender-id>'
25-
}
25+
};
2626

2727
const myFirebaseAuthConfig = {
2828
provider: AuthProviders.Google,
2929
method: AuthMethods.Redirect
30-
}
30+
};
3131

3232
@NgModule({
3333
imports: [
@@ -46,7 +46,7 @@ export class AppModule {}
4646
If you have setup authentication in bootstrap like above, then all you need to do
4747
is call login on `af.auth.login()`
4848

49-
The long exception is if you're using username and password, then you'll have
49+
The lone exception is if you're using username and password, then you'll have
5050
to call `af.auth.login()` with the user's credentials.
5151

5252
```ts
@@ -102,7 +102,7 @@ authentication in the bootstrap phase, you can still override the configuration.
102102
af.auth.login({
103103
provider: AuthProviders.Anonymous,
104104
method: AuthMethods.Anonymous,
105-
})
105+
});
106106

107107
// Email and password
108108
af.auth.login({
@@ -112,19 +112,19 @@ af.auth.login({
112112
{
113113
provider: AuthProviders.Password,
114114
method: AuthMethods.Password,
115-
})
115+
});
116116

117117
// Social provider redirect
118118
af.auth.login({
119119
provider: AuthProviders.Twitter,
120120
method: AuthMethods.Redirect,
121-
})
121+
});
122122

123123
// Social provider popup
124124
af.auth.login({
125125
provider: AuthProviders.Github,
126126
method: AuthMethods.Popup,
127-
})
127+
});
128128
```
129129

130130
**Example app:**

src/angularfire2.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ import { Subscription } from 'rxjs/Subscription';
2020
import { COMMON_CONFIG, ANON_AUTH_CONFIG } from './test-config';
2121

2222
describe('angularfire', () => {
23-
var subscription:Subscription;
24-
var app: firebase.app.App;
25-
var rootRef: firebase.database.Reference;
26-
var questionsRef: firebase.database.Reference;
27-
var listOfQuestionsRef: firebase.database.Reference;
28-
var angularFire2: AngularFire;
23+
let subscription:Subscription;
24+
let app: firebase.app.App;
25+
let rootRef: firebase.database.Reference;
26+
let questionsRef: firebase.database.Reference;
27+
let listOfQuestionsRef: firebase.database.Reference;
28+
let angularFire2: AngularFire;
29+
const APP_NAME = 'super-awesome-test-firebase-app-name';
2930

3031
beforeEach(() => {
3132

3233
TestBed.configureTestingModule({
33-
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG)]
34+
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, APP_NAME)]
3435
});
3536

3637
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
@@ -71,6 +72,9 @@ describe('angularfire', () => {
7172
describe('FirebaseApp', () => {
7273
it('should provide a FirebaseApp for the FirebaseApp binding', () => {
7374
expect(typeof app.delete).toBe('function');
75+
});
76+
it('should have the provided name', () => {
77+
expect(app.name).toBe(APP_NAME);
7478
})
7579
});
7680

src/angularfire2.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
FirebaseApp,
1212
WindowLocation,
1313
FirebaseUserConfig,
14-
FirebaseAuthConfig
14+
FirebaseAuthConfig,
15+
FirebaseAppName
1516
} from './tokens';
1617
import {
1718
APP_INITIALIZER,
@@ -43,14 +44,14 @@ import {
4344
@Injectable()
4445
export class AngularFire {
4546
constructor(
46-
@Inject(FirebaseConfig) private firebaseConfig:string,
47+
@Inject(FirebaseConfig) private firebaseConfig:FirebaseAppConfig,
4748
public auth: AngularFireAuth,
4849
public database: AngularFireDatabase) {}
4950
}
5051

51-
export function _getFirebase(config: FirebaseAppConfig): firebase.app.App {
52+
export function _getFirebase(config: FirebaseAppConfig, appName: string): firebase.app.App {
5253
try {
53-
return firebase.initializeApp(config);
54+
return firebase.initializeApp(config, appName);
5455
}
5556
catch (e) {
5657
return firebase.app(null);
@@ -79,7 +80,7 @@ export const COMMON_PROVIDERS: any[] = [
7980
{
8081
provide: FirebaseApp,
8182
useFactory: _getFirebase,
82-
deps: [FirebaseConfig]
83+
deps: [FirebaseConfig, FirebaseAppName]
8384
},
8485
AngularFireAuth,
8586
AngularFire,
@@ -106,23 +107,24 @@ export const FIREBASE_PROVIDERS:any[] = [
106107
export const defaultFirebase = (config: FirebaseAppConfig): any => {
107108
return [
108109
{ provide: FirebaseUserConfig, useValue: config },
109-
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] }
110+
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] }
110111
]
111112
};
112113

113114
@NgModule({
114-
providers: FIREBASE_PROVIDERS
115+
providers: FIREBASE_PROVIDERS
115116
})
116117
export class AngularFireModule {
117-
static initializeApp(config: FirebaseAppConfig, authConfig?:AuthConfiguration): ModuleWithProviders {
118+
static initializeApp(config: FirebaseAppConfig, authConfig?: AuthConfiguration, appName?: string): ModuleWithProviders {
118119
return {
119-
ngModule: AngularFireModule,
120-
providers: [
121-
{ provide: FirebaseUserConfig, useValue: config },
122-
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
123-
{ provide: FirebaseAuthConfig, useValue: authConfig }
124-
]
125-
}
120+
ngModule: AngularFireModule,
121+
providers: [
122+
{ provide: FirebaseUserConfig, useValue: config },
123+
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
124+
{ provide: FirebaseAuthConfig, useValue: authConfig },
125+
{ provide: FirebaseAppName, useValue: appName }
126+
]
127+
}
126128
}
127129
}
128130

src/tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const FirebaseConfig = new OpaqueToken('FirebaseUrl');
44
export const FirebaseApp = new OpaqueToken('FirebaseApp')
55
export const FirebaseAuthConfig = new OpaqueToken('FirebaseAuthConfig');
66
export const FirebaseUserConfig = new OpaqueToken('FirebaseUserConfig');
7+
export const FirebaseAppName = new OpaqueToken('FirebaseAppName');
78
export const WindowLocation = new OpaqueToken('WindowLocation');
89
// TODO: Deprecate
910
export const FirebaseRef = FirebaseApp;

0 commit comments

Comments
 (0)