-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogged-user.service.ts
More file actions
137 lines (117 loc) · 4.7 KB
/
logged-user.service.ts
File metadata and controls
137 lines (117 loc) · 4.7 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
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
import { Injectable } from "@angular/core";
import { catchError, first } from "rxjs/operators";
import { EMPTY, BehaviorSubject, Observable, ReplaySubject, Subject } from "rxjs";
import { NavigationService } from "../services/navigation.service";
import { MaybeNull } from "../common/app.types";
import { isNull } from "lodash";
import { AppConfigService } from "../app-config.service";
import { AccountFragment } from "../api/kamu.graphql.interface";
import { UnsubscribeDestroyRefAdapter } from "../common/unsubscribe.ondestroy.adapter";
import { AppLoginInstructions } from "../app-config.model";
import { Apollo } from "apollo-angular";
import { promiseWithCatch } from "../common/app.helpers";
import { LoginService } from "./login/login.service";
import { LocalStorageService } from "../services/local-storage.service";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
@Injectable({
providedIn: "root",
})
export class LoggedUserService extends UnsubscribeDestroyRefAdapter {
private loggedInUser: MaybeNull<AccountFragment> = null;
private loggedInUser$: Subject<MaybeNull<AccountFragment>> = new ReplaySubject<MaybeNull<AccountFragment>>(1);
private adminPrivileges$: BehaviorSubject<{ value: boolean }> = new BehaviorSubject<{ value: boolean }>({
value: false,
});
constructor(
private loginService: LoginService,
private navigationService: NavigationService,
private appConfigService: AppConfigService,
private localStorageService: LocalStorageService,
private apollo: Apollo,
) {
super();
this.loginService.accessTokenChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((token: string) => this.saveAccessToken(token)),
this.loginService.accountChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((user: AccountFragment) => this.changeUser(user));
}
public get adminPrivilegesChanges(): Observable<{ value: boolean }> {
return this.adminPrivileges$.asObservable();
}
public emitAdminPrivilegesChanges(value: boolean): void {
return this.adminPrivileges$.next({ value });
}
public initializeCompletes(): Observable<void> {
const loginInstructions: AppLoginInstructions | null = this.appConfigService.loginInstructions;
if (loginInstructions) {
return this.loginService.genericLogin(
loginInstructions.loginMethod,
loginInstructions.loginCredentialsJson,
);
} else {
return this.attemptPreviousAuthenticationCompletes();
}
}
public get loggedInUserChanges(): Observable<MaybeNull<AccountFragment>> {
return this.loggedInUser$.asObservable();
}
public get maybeCurrentlyLoggedInUser(): MaybeNull<AccountFragment> {
return this.loggedInUser;
}
public get currentlyLoggedInUser(): AccountFragment {
if (this.loggedInUser) {
return this.loggedInUser;
} else {
throw new Error("No currently logged in user");
}
}
public get isAuthenticated(): boolean {
return !isNull(this.loggedInUser);
}
public get isAdmin(): boolean {
return this.maybeCurrentlyLoggedInUser?.isAdmin ?? false;
}
public logout(): void {
this.terminateSession();
this.navigationService.navigateToHome();
}
public terminateSession(): void {
this.changeUser(null);
this.resetAccessToken();
this.clearGraphQLCache();
this.resetAdminPrivileges();
}
private attemptPreviousAuthenticationCompletes(): Observable<void> {
const accessToken: string | null = this.localStorageService.accessToken;
if (typeof accessToken === "string" && !this.isAuthenticated) {
return this.loginService.fetchAccountFromAccessToken(accessToken).pipe(
first(),
catchError(() => {
this.terminateSession();
return EMPTY;
}),
);
} else {
return EMPTY;
}
}
private changeUser(user: MaybeNull<AccountFragment>) {
this.loggedInUser = user;
this.loggedInUser$.next(user);
}
private clearGraphQLCache(): void {
promiseWithCatch(this.apollo.client.clearStore());
}
private resetAccessToken(): void {
this.localStorageService.setAccessToken(null);
}
private saveAccessToken(token: string): void {
this.localStorageService.setAccessToken(token);
}
private resetAdminPrivileges(): void {
this.localStorageService.setAdminPriveleges(null);
this.emitAdminPrivilegesChanges(false);
}
}