-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
angularfire2.ts
144 lines (133 loc) · 3.42 KB
/
angularfire2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as firebase from 'firebase';
import * as utils from './utils';
import { FirebaseAppConfig } from './interfaces';
import { AuthConfiguration } from './auth';
import {
FirebaseListFactoryOpts,
FirebaseObjectFactoryOpts,
} from './interfaces';
import {
FirebaseConfig,
FirebaseApp,
WindowLocation,
FirebaseUserConfig,
FirebaseAuthConfig,
FirebaseAppName
} from './tokens';
import {
APP_INITIALIZER,
Inject,
Injectable,
OpaqueToken,
NgModule,
ModuleWithProviders
} from '@angular/core';
import {
FirebaseSdkAuthBackend,
AngularFireAuth,
firebaseAuthConfig,
AuthBackend,
AuthMethods,
AuthProviders,
FirebaseAuthState
} from './auth/index';
import {
FirebaseListObservable,
FirebaseObjectObservable,
FirebaseListFactory,
FirebaseObjectFactory,
AngularFireDatabase
} from './database/index';
@Injectable()
export class AngularFire {
constructor(
@Inject(FirebaseConfig) private firebaseConfig:FirebaseAppConfig,
public auth: AngularFireAuth,
public database: AngularFireDatabase) {}
}
export function _getFirebase(config: FirebaseAppConfig, appName?: string): firebase.app.App {
try {
if (appName) {
return firebase.initializeApp(config, appName);
} else {
return firebase.initializeApp(config);
}
}
catch (e) {
return firebase.app(null);
}
}
export function _getWindowLocation(){
return window.location;
}
export function _getAuthBackend(app: firebase.app.App): FirebaseSdkAuthBackend {
return new FirebaseSdkAuthBackend(app);
}
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[] = [
{
provide: FirebaseApp,
useFactory: _getFirebase,
deps: [FirebaseConfig, FirebaseAppName]
},
AngularFireAuth,
AngularFire,
AngularFireDatabase
];
export const FIREBASE_PROVIDERS:any[] = [
COMMON_PROVIDERS,
{
provide: AuthBackend,
useFactory: _getAuthBackend,
deps: [FirebaseApp]
},
{
provide: WindowLocation,
useFactory: _getWindowLocation
},
];
/**
* Used to define the default Firebase root location to be
* used throughout an application.
*/
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?: AuthConfiguration, appName?: string): ModuleWithProviders {
return {
ngModule: AngularFireModule,
providers: [
{ provide: FirebaseUserConfig, useValue: config },
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
{ provide: FirebaseAuthConfig, useValue: authConfig },
{ provide: FirebaseAppName, useValue: appName }
]
}
}
}
export {
AngularFireAuth,
AngularFireDatabase,
FirebaseListObservable,
FirebaseObjectObservable,
FirebaseListFactory,
FirebaseObjectFactory,
firebaseAuthConfig,
FirebaseAuthState,
AuthMethods,
AuthProviders,
WindowLocation
}
export { FirebaseConfig, FirebaseApp, FirebaseAuthConfig, FirebaseRef, FirebaseUrl, FirebaseUserConfig } from './tokens';
export { FirebaseAppConfig } from './interfaces';