Repository navigation
Expand file tree
/
Copy pathauth.module.ts
More file actions
68 lines (61 loc) · 1.99 KB
/
Copy pathauth.module.ts
File metadata and controls
68 lines (61 loc) · 1.99 KB
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
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders, Injector } from '@angular/core';
import { Auth as FirebaseAuth } from 'firebase/auth';
import { ɵgetDefaultInstanceOf, ɵAngularFireSchedulers, VERSION } from '@angular/fire';
import { Auth, AuthInstances, AUTH_PROVIDER_NAME } from './auth';
import { FirebaseApps, FirebaseApp } from '@angular/fire/app';
import { registerVersion } from 'firebase/app';
import { AppCheckInstances } from '@angular/fire/app-check';
export const PROVIDED_AUTH_INSTANCES = new InjectionToken<Auth[]>('angularfire2.auth-instances');
export function defaultAuthInstanceFactory(provided: FirebaseAuth[]|undefined, defaultApp: FirebaseApp) {
const defaultAuth = ɵgetDefaultInstanceOf<FirebaseAuth>(AUTH_PROVIDER_NAME, provided, defaultApp);
return defaultAuth && new Auth(defaultAuth);
}
export function authInstanceFactory(fn: (injector: Injector) => FirebaseAuth) {
return (zone: NgZone, injector: Injector) => {
const auth = zone.runOutsideAngular(() => fn(injector));
return new Auth(auth);
};
}
const AUTH_INSTANCES_PROVIDER = {
provide: AuthInstances,
deps: [
[new Optional(), PROVIDED_AUTH_INSTANCES ],
]
};
const DEFAULT_AUTH_INSTANCE_PROVIDER = {
provide: Auth,
useFactory: defaultAuthInstanceFactory,
deps: [
[new Optional(), PROVIDED_AUTH_INSTANCES ],
FirebaseApp,
]
};
@NgModule({
providers: [
DEFAULT_AUTH_INSTANCE_PROVIDER,
AUTH_INSTANCES_PROVIDER,
]
})
export class AuthModule {
constructor() {
registerVersion('angularfire', VERSION.full, 'auth');
}
}
export function provideAuth(fn: () => FirebaseAuth, ...deps: any[]): ModuleWithProviders<AuthModule> {
return {
ngModule: AuthModule,
providers: [{
provide: PROVIDED_AUTH_INSTANCES,
useFactory: authInstanceFactory(fn),
multi: true,
deps: [
NgZone,
Injector,
ɵAngularFireSchedulers,
FirebaseApps,
[new Optional(), AppCheckInstances ],
...deps,
]
}]
};
}