-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathangularfire2.ts
143 lines (132 loc) · 3.26 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
import { initializeApp } from 'firebase';
import * as utils from './utils';
import { FirebaseAppConfig } from './interfaces';
import {
FirebaseListFactoryOpts,
FirebaseObjectFactoryOpts,
} from './interfaces';
import {
FirebaseConfig,
FirebaseApp,
WindowLocation,
FirebaseUserConfig,
FirebaseAuthConfig
} from './tokens';
import {
APP_INITIALIZER,
Inject,
Injectable,
OpaqueToken,
provide,
NgModule,
ModuleWithProviders
} from '@angular/core';
import {
FirebaseSdkAuthBackend,
AngularFireAuth,
firebaseAuthConfig,
FirebaseAuth,
AuthBackend,
AuthMethods,
AuthProviders,
FirebaseAuthState
} from './auth/index';
import {
FirebaseListObservable,
FirebaseObjectObservable,
FirebaseListFactory,
FirebaseObjectFactory,
AngularFireDatabase,
FirebaseDatabase
} from './database/index';
@Injectable()
export class AngularFire {
constructor(
@Inject(FirebaseConfig) private fbUrl:string,
public auth: AngularFireAuth,
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,
useExisting: AngularFireAuth
},
{
provide: FirebaseApp,
useFactory: _getFirebase,
deps: [FirebaseConfig]
},
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?:FirebaseAppConfig): ModuleWithProviders {
return {
ngModule: AngularFireModule,
providers: [
{ provide: FirebaseUserConfig, useValue: config },
{ provide: FirebaseConfig, useFactory: _getDefaultFirebase, deps: [FirebaseUserConfig] },
{ provide: FirebaseAuthConfig, useValue: authConfig }
]
}
}
}
export {
AngularFireAuth,
AngularFireDatabase,
// TODO: Deprecate
FirebaseAuth,
// TODO: Deprecate
FirebaseDatabase,
FirebaseListObservable,
FirebaseObjectObservable,
FirebaseListFactory,
FirebaseObjectFactory,
firebaseAuthConfig,
FirebaseAuthState,
AuthMethods,
AuthProviders,
WindowLocation
}
export { FirebaseConfig, FirebaseApp, FirebaseAuthConfig, FirebaseRef, FirebaseUrl, FirebaseUserConfig } from './tokens';
export { FirebaseAppConfig } from './interfaces';