-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser.service.ts
140 lines (127 loc) · 3.59 KB
/
user.service.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
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { auth, User } from 'firebase/app';
import { Observable, of } from 'rxjs';
import { tap, switchMap, shareReplay, filter, map } from 'rxjs/operators';
import { AngularFirestore } from '@angular/fire/firestore';
import {
FsPath,
Relawan,
LOCAL_STORAGE_LAST_URL,
Upsert,
RelawanPhotos,
lsGetItem,
LOCK_DOWN
} from 'shared';
import { ApiService } from './api.service';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class UserService {
user: User;
relawan$: Observable<Relawan>;
relawanPhotos$: Observable<RelawanPhotos>;
scoreboard$: Observable<any>;
upsert$: { [imageId: string]: Observable<Upsert> } = {};
isLoading = true;
constructor(
private afAuth: AngularFireAuth,
private fsdb: AngularFirestore,
private api: ApiService,
private router: Router
) {
this.isLoading = true;
const user$ = this.afAuth.user.pipe(shareReplay(1));
this.relawan$ = user$.pipe(
switchMap(user =>
user
? this.fsdb
.doc<Relawan>(FsPath.relawan(user.uid))
.valueChanges()
.pipe(
filter(r => !!r),
tap(r => (r.auth = this.user = user))
)
: of(null)
),
switchMap(async r => {
if (r && (!r.profile || !r.profile.link)) {
console.log(`User profile access is required`);
await this.logout();
this.router.navigate(['/']);
return null;
}
this.isLoading = false;
return r;
}),
shareReplay(1)
);
this.scoreboard$ = this.fsdb
.doc<any>(FsPath.scoreboard())
.valueChanges()
.pipe(shareReplay(1));
this.relawanPhotos$ = user$.pipe(
switchMap(user =>
user
? this.fsdb
.doc<RelawanPhotos>(FsPath.relawanPhoto(user.uid))
.valueChanges()
: of(null)
),
shareReplay(1)
);
if (LOCK_DOWN) {
return;
}
this.afAuth.auth
.getRedirectResult()
.then(async result => {
const profile: any =
result &&
result.user &&
result.additionalUserInfo &&
result.additionalUserInfo.profile;
if (profile) {
const url = lsGetItem(LOCAL_STORAGE_LAST_URL);
if (url) {
console.log('Navigate to last url: ', url);
this.router.navigateByUrl(url);
}
// @ts-ignore
const body = { token: result.credential.accessToken };
return this.api.post(result.user, `register/login`, body);
}
})
.catch(console.error);
}
login(method: string) {
const a = this.afAuth.auth;
switch (method) {
case 'anon':
return a.signInAnonymously();
case 'google':
return a.signInWithRedirect(new auth.GoogleAuthProvider());
case 'facebook':
const provider = new auth.FacebookAuthProvider();
provider.addScope('email');
provider.addScope('user_link');
provider.setCustomParameters({ auth_type: 'rerequest' });
return a.signInWithRedirect(provider);
case 'twitter':
return a.signInWithRedirect(new auth.TwitterAuthProvider());
}
}
logout() {
return this.afAuth.auth.signOut();
}
getUpsert$(imageId: string) {
if (!this.upsert$[imageId]) {
this.upsert$[imageId] = this.fsdb
.doc<Upsert>(FsPath.upserts(imageId))
.valueChanges()
.pipe(shareReplay(1));
}
return this.upsert$[imageId];
}
}