Skip to content

Commit 3181166

Browse files
committed
perf: use modern firebase auth functions
1 parent f77bf55 commit 3181166

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/app/auth.service.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1-
import {Injectable} from '@angular/core';
2-
import {Auth, GoogleAuthProvider, onIdTokenChanged, signInWithPopup, signOut, User} from '@angular/fire/auth';
3-
import {BehaviorSubject} from 'rxjs';
1+
import {inject, Injectable} from '@angular/core';
2+
import {Auth, GoogleAuthProvider, idToken, user, signInWithPopup, signOut, User} from '@angular/fire/auth';
3+
import {BehaviorSubject, Subscription} from 'rxjs';
44

55
@Injectable({
66
providedIn: 'root'
77
})
88
export class AuthService {
9+
private auth: Auth = inject(Auth);
10+
idToken$ = idToken(this.auth);
11+
idTokenSubscription: Subscription;
12+
user$ = user(this.auth);
13+
userSubscription: Subscription;
14+
915
private readonly idTokenSubject = new BehaviorSubject<string|null>(null);
1016
public readonly idToken = this.idTokenSubject.asObservable();
1117
private readonly userSubject = new BehaviorSubject<User|null>(null);
1218
public readonly user = this.userSubject.asObservable();
1319

14-
constructor(private afAuth: Auth) {
15-
onIdTokenChanged(afAuth, user => {
16-
if (user != null) {
17-
this.userSubject.next(user);
18-
user.getIdToken().then(idToken => {
19-
this.idTokenSubject.next(idToken);
20-
});
21-
} else {
22-
this.userSubject.next(null);
23-
this.idTokenSubject.next(null);
24-
}
20+
constructor() {
21+
this.idTokenSubscription = this.idToken$.subscribe((token: string | null) => {
22+
console.log('idToken refreshed');
23+
this.idTokenSubject.next(token);
24+
});
25+
this.userSubscription = this.user$.subscribe((aUser: User | null) => {
26+
this.userSubject.next(aUser);
2527
});
2628
}
2729

2830
signInWithPopup() {
2931
const provider = new GoogleAuthProvider();
3032
provider.setCustomParameters({hd: 'docchula.com'});
31-
return signInWithPopup(this.afAuth, provider);
33+
return signInWithPopup(this.auth, provider);
3234
}
3335

3436
signOut() {
35-
return signOut(this.afAuth);
37+
return signOut(this.auth);
3638
}
3739

3840
}

src/app/man.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {Injectable} from '@angular/core';
22
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
33
import {combineLatestWith, Observable, of, startWith, takeUntil, timer} from 'rxjs';
4-
import {filter, map, switchMap, timeout} from 'rxjs/operators';
5-
import {environment} from '../environments/environment';
4+
import {map, switchMap, timeout} from 'rxjs/operators';
65
import {PlayHistory, PlayHistoryValue, PlayTrackerService} from './play-tracker.service';
7-
import {getStringChanges, RemoteConfig} from '@angular/fire/remote-config';
86
import {AuthService} from './auth.service';
97

108

@@ -115,6 +113,7 @@ export class ManService {
115113
}
116114

117115
updatePlayRecord(uid: string, video_id: string | number, progress: number, speed: number) {
116+
// @todo Throttle this function
118117
return this.post<JSend<null>>('v1/play_records', {
119118
uid,
120119
video_id,

0 commit comments

Comments
 (0)