Skip to content

Commit aa62620

Browse files
authored
feat(auth): Add Auth
1 parent 110c264 commit aa62620

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/auth/auth.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { TestBed } from '@angular/core/testing';
44
import { AngularFireModule, FIREBASE_APP_NAME, FIREBASE_OPTIONS, FirebaseApp } from '@angular/fire';
55
import { AngularFireAuth, AngularFireAuthModule } from '@angular/fire/auth';
66
import { COMMON_CONFIG } from '../test-config';
7-
import 'firebase/auth';
7+
import { User } from 'firebase/auth';
88
import { rando } from '../firestore/utils.spec';
99

1010
const firebaseUser = {
1111
uid: '12345',
1212
providerData: [{ displayName: 'jeffbcrossyface' }]
13-
} as firebase.User;
13+
} as User;
1414

1515
describe('AngularFireAuth', () => {
1616
let app: FirebaseApp;
1717
let afAuth: AngularFireAuth;
18-
let mockAuthState: Subject<firebase.User>;
18+
let mockAuthState: Subject<User>;
1919

2020
beforeEach(() => {
2121
TestBed.configureTestingModule({
@@ -28,13 +28,13 @@ describe('AngularFireAuth', () => {
2828
app = TestBed.inject(FirebaseApp);
2929
afAuth = TestBed.inject(AngularFireAuth);
3030

31-
mockAuthState = new Subject<firebase.User>();
31+
mockAuthState = new Subject<User>();
3232
// @ts-ignore
3333
spyOn(afAuth, 'authState').and.returnValue(mockAuthState);
3434
// @ts-ignore
3535
spyOn(afAuth, 'idToken').and.returnValue(mockAuthState);
36-
(afAuth as any).authState = mockAuthState as Observable<firebase.User>;
37-
(afAuth as any).idToken = mockAuthState as Observable<firebase.User>;
36+
(afAuth as any).authState = mockAuthState as Observable<User>;
37+
(afAuth as any).idToken = mockAuthState as Observable<User>;
3838
});
3939

4040
afterEach(() => {

src/auth/auth.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import {
1313
ɵkeepUnstableUntilFirstFactory,
1414
ɵapplyMixins
1515
} from '@angular/fire';
16-
import firebase from 'firebase/app';
16+
import { Auth, AuthSettings, UserCredential, User, IdTokenResult, Persistence, getRedirectResult, useAuthEmulator, setPersistence } from 'firebase/auth';
1717
import { isPlatformServer } from '@angular/common';
1818
import { proxyPolyfillCompat } from './base';
1919
import { ɵfetchInstance } from '@angular/fire';
2020

21-
export interface AngularFireAuth extends ɵPromiseProxy<firebase.auth.Auth> {}
21+
export interface AngularFireAuth extends ɵPromiseProxy<Auth> {}
2222

2323
type UseEmulatorArguments = [string, number];
2424
export const USE_EMULATOR = new InjectionToken<UseEmulatorArguments>('angularfire2.auth.use-emulator');
2525

26-
export const SETTINGS = new InjectionToken<firebase.auth.AuthSettings>('angularfire2.auth.settings');
26+
export const SETTINGS = new InjectionToken<AuthSettings>('angularfire2.auth.settings');
2727
export const TENANT_ID = new InjectionToken<string>('angularfire2.auth.tenant-id');
2828
export const LANGUAGE_CODE = new InjectionToken<string>('angularfire2.auth.langugage-code');
2929
export const USE_DEVICE_LANGUAGE = new InjectionToken<boolean>('angularfire2.auth.use-device-language');
@@ -37,7 +37,7 @@ export class AngularFireAuth {
3737
/**
3838
* Observable of authentication state; as of Firebase 4.0 this is only triggered via sign-in/out
3939
*/
40-
public readonly authState: Observable<firebase.User|null>;
40+
public readonly authState: Observable<User|null>;
4141

4242
/**
4343
* Observable of the currently signed-in user's JWT token used to identify the user to a Firebase service (or null).
@@ -47,19 +47,19 @@ export class AngularFireAuth {
4747
/**
4848
* Observable of the currently signed-in user (or null).
4949
*/
50-
public readonly user: Observable<firebase.User|null>;
50+
public readonly user: Observable<User|null>;
5151

5252
/**
5353
* Observable of the currently signed-in user's IdTokenResult object which contains the ID token JWT string and other
5454
* helper properties for getting different data associated with the token as well as all the decoded payload claims
5555
* (or null).
5656
*/
57-
public readonly idTokenResult: Observable<firebase.auth.IdTokenResult|null>;
57+
public readonly idTokenResult: Observable<IdTokenResult|null>;
5858

5959
/**
6060
* Observable of the currently signed-in user's credential, or null
6161
*/
62-
public readonly credential: Observable<Required<firebase.auth.UserCredential>|null>;
62+
public readonly credential: Observable<Required<UserCredential>|null>;
6363

6464
constructor(
6565
@Inject(FIREBASE_OPTIONS) options: FirebaseOptions,
@@ -68,28 +68,28 @@ export class AngularFireAuth {
6868
@Inject(PLATFORM_ID) platformId: Object,
6969
zone: NgZone,
7070
@Optional() @Inject(USE_EMULATOR) _useEmulator: any, // can't use the tuple here
71-
@Optional() @Inject(SETTINGS) _settings: any, // can't use firebase.auth.AuthSettings here
71+
@Optional() @Inject(SETTINGS) _settings: any, // can't use AuthSettings here
7272
@Optional() @Inject(TENANT_ID) tenantId: string | null,
7373
@Optional() @Inject(LANGUAGE_CODE) languageCode: string | null,
7474
@Optional() @Inject(USE_DEVICE_LANGUAGE) useDeviceLanguage: boolean | null,
75-
@Optional() @Inject(PERSISTENCE) persistence: string | null,
75+
@Optional() @Inject(PERSISTENCE) persistence: Persistence | null,
7676
) {
7777
const schedulers = new ɵAngularFireSchedulers(zone);
7878
const keepUnstableUntilFirst = ɵkeepUnstableUntilFirstFactory(schedulers);
79-
const logins = new Subject<Required<firebase.auth.UserCredential>>();
79+
const logins = new Subject<Required<UserCredential>>();
8080

8181
const auth = of(undefined).pipe(
8282
observeOn(schedulers.outsideAngular),
8383
switchMap(() => zone.runOutsideAngular(() => import('firebase/auth'))),
8484
map(() => ɵfirebaseAppFactory(options, zone, nameOrConfig)),
8585
map(app => zone.runOutsideAngular(() => {
8686
const useEmulator: UseEmulatorArguments | null = _useEmulator;
87-
const settings: firebase.auth.AuthSettings | null = _settings;
87+
const settings: AuthSettings | null = _settings;
8888
return ɵfetchInstance(`${app.name}.auth`, 'AngularFireAuth', app, () => {
8989
const auth = zone.runOutsideAngular(() => app.auth());
9090
if (useEmulator) {
9191
// Firebase Auth doesn't conform to the useEmulator convention, let's smooth that over
92-
auth.useEmulator(`http://${useEmulator.join(':')}`);
92+
useAuthEmulator(auth, `http://${useEmulator.join(':')}`);
9393
}
9494
if (tenantId) {
9595
auth.tenantId = tenantId;
@@ -98,11 +98,12 @@ export class AngularFireAuth {
9898
if (useDeviceLanguage) {
9999
auth.useDeviceLanguage();
100100
}
101-
if (settings) {
102-
auth.settings = settings;
103-
}
101+
// TODO(team): We need to initalizeAuth with settings in the NgModule
102+
// if (settings) {
103+
// auth.settings = settings;
104+
// }
104105
if (persistence) {
105-
auth.setPersistence(persistence);
106+
setPersistence(auth, persistence);
106107
}
107108
return auth;
108109
}, [useEmulator, tenantId, languageCode, useDeviceLanguage, settings, persistence]);
@@ -124,7 +125,7 @@ export class AngularFireAuth {
124125
const _ = auth.pipe(first()).subscribe();
125126

126127
const redirectResult = auth.pipe(
127-
switchMap(auth => auth.getRedirectResult().then(it => it, () => null)),
128+
switchMap(auth => getRedirectResult(auth).then(it => it, () => null)),
128129
keepUnstableUntilFirst,
129130
shareReplay({ bufferSize: 1, refCount: false }),
130131
);
@@ -170,7 +171,7 @@ export class AngularFireAuth {
170171
).pipe(
171172
// handle the { user: { } } when a user is already logged in, rather have null
172173
// TODO handle the type corcersion better
173-
map(credential => credential?.user ? credential as Required<firebase.auth.UserCredential> : null),
174+
map(credential => credential?.user ? credential as Required<UserCredential> : null),
174175
subscribeOn(schedulers.outsideAngular),
175176
observeOn(schedulers.insideAngular),
176177
);
@@ -184,7 +185,7 @@ export class AngularFireAuth {
184185
// to be consumed in .credential
185186
if (name.startsWith('signIn') || name.startsWith('createUser')) {
186187
// TODO fix the types, the trouble is UserCredential has everything optional
187-
val.then((user: firebase.auth.UserCredential) => logins.next(user as any));
188+
val.then((user: UserCredential) => logins.next(user as any));
188189
}
189190
}
190191
}});

src/auth/public_api.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
2-
import 'firebase/auth'; // removed in build process when not UMD
3-
41
export * from './auth';
52
export * from './auth.module';

0 commit comments

Comments
 (0)