Browse Source

Clean up tokenStorageService usage

pull/117/head
Thomas 4 years ago
parent
commit
f5b8030dd5
  1. 32
      apps/client/src/app/app.component.ts
  2. 29
      apps/client/src/app/pages/about/about-page.component.ts
  3. 26
      apps/client/src/app/pages/account/account-page.component.ts
  4. 38
      apps/client/src/app/pages/accounts/accounts-page.component.ts
  5. 16
      apps/client/src/app/pages/admin/admin-page.component.ts
  6. 20
      apps/client/src/app/pages/analysis/analysis-page.component.ts
  7. 62
      apps/client/src/app/pages/home/home-page.component.ts
  8. 27
      apps/client/src/app/pages/pricing/pricing-page.component.ts
  9. 38
      apps/client/src/app/pages/transactions/transactions-page.component.ts
  10. 26
      apps/client/src/app/pages/zen/zen-page.component.ts
  11. 11
      apps/client/src/app/services/token-storage.service.ts

32
apps/client/src/app/app.component.ts

@ -66,28 +66,18 @@ export class AppComponent implements OnDestroy, OnInit {
this.currentRoute = urlSegments[0].path; this.currentRoute = urlSegments[0].path;
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.isLoggedIn = !!this.tokenStorageService.getToken(); if (state?.user) {
this.user = state.user;
if (this.isLoggedIn) {
this.userService this.canCreateAccount = hasPermission(
.get() this.user.permissions,
.pipe(takeUntil(this.unsubscribeSubject)) permissions.createUserAccount
.subscribe((user) => { );
this.user = user;
this.cd.markForCheck();
this.canCreateAccount = hasPermission(
this.user.permissions,
permissions.createUserAccount
);
this.cd.markForCheck();
});
} else {
this.user = null;
} }
}); });
} }

29
apps/client/src/app/pages/about/about-page.component.ts

@ -1,6 +1,4 @@
import { ChangeDetectorRef, Component, OnInit } from '@angular/core'; import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { DataService } from '@ghostfolio/client/services/data.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { baseCurrency } from '@ghostfolio/common/config'; import { baseCurrency } from '@ghostfolio/common/config';
import { User } from '@ghostfolio/common/interfaces'; import { User } from '@ghostfolio/common/interfaces';
@ -28,8 +26,6 @@ export class AboutPageComponent implements OnInit {
*/ */
public constructor( public constructor(
private cd: ChangeDetectorRef, private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) {} ) {}
@ -37,22 +33,15 @@ export class AboutPageComponent implements OnInit {
* Initializes the controller * Initializes the controller
*/ */
public ngOnInit() { public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken(); this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
if (this.isLoggedIn) .subscribe((state) => {
this.tokenStorageService if (state?.user) {
.onChangeHasToken() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { this.cd.markForCheck();
this.userService }
.get() });
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.cd.markForCheck();
});
});
} }
public ngOnDestroy() { public ngOnDestroy() {

26
apps/client/src/app/pages/account/account-page.component.ts

@ -1,6 +1,5 @@
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config'; import { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config';
import { Access, User } from '@ghostfolio/common/interfaces'; import { Access, User } from '@ghostfolio/common/interfaces';
@ -31,7 +30,6 @@ export class AccountPageComponent implements OnDestroy, OnInit {
public constructor( public constructor(
private cd: ChangeDetectorRef, private cd: ChangeDetectorRef,
private dataService: DataService, private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) { ) {
this.dataService this.dataService
@ -46,23 +44,19 @@ export class AccountPageComponent implements OnDestroy, OnInit {
); );
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.hasPermissionToUpdateUserSettings = hasPermission( this.hasPermissionToUpdateUserSettings = hasPermission(
this.user.permissions, this.user.permissions,
permissions.updateUserSettings permissions.updateUserSettings
); );
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

38
apps/client/src/app/pages/accounts/accounts-page.component.ts

@ -5,7 +5,6 @@ import { CreateAccountDto } from '@ghostfolio/api/app/account/create-account.dto
import { UpdateAccountDto } from '@ghostfolio/api/app/account/update-account.dto'; import { UpdateAccountDto } from '@ghostfolio/api/app/account/update-account.dto';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { User } from '@ghostfolio/common/interfaces'; import { User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -43,7 +42,6 @@ export class AccountsPageComponent implements OnInit {
private impersonationStorageService: ImpersonationStorageService, private impersonationStorageService: ImpersonationStorageService,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) { ) {
this.routeQueryParams = route.queryParams this.routeQueryParams = route.queryParams
@ -77,27 +75,23 @@ export class AccountsPageComponent implements OnInit {
this.hasImpersonationId = !!aId; this.hasImpersonationId = !!aId;
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { this.hasPermissionToCreateAccount = hasPermission(
this.user = user; this.user.permissions,
permissions.createAccount
this.hasPermissionToCreateAccount = hasPermission( );
user.permissions, this.hasPermissionToDeleteAccount = hasPermission(
permissions.createAccount this.user.permissions,
); permissions.deleteAccount
this.hasPermissionToDeleteAccount = hasPermission( );
user.permissions,
permissions.deleteAccount this.cd.markForCheck();
); }
this.cd.markForCheck();
});
}); });
this.fetchAccounts(); this.fetchAccounts();

16
apps/client/src/app/pages/admin/admin-page.component.ts

@ -2,7 +2,6 @@ import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { AdminService } from '@ghostfolio/client/services/admin.service'; import { AdminService } from '@ghostfolio/client/services/admin.service';
import { CacheService } from '@ghostfolio/client/services/cache.service'; import { CacheService } from '@ghostfolio/client/services/cache.service';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config'; import { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config';
import { AdminData, User } from '@ghostfolio/common/interfaces'; import { AdminData, User } from '@ghostfolio/common/interfaces';
@ -35,7 +34,6 @@ export class AdminPageComponent implements OnInit {
private cacheService: CacheService, private cacheService: CacheService,
private cd: ChangeDetectorRef, private cd: ChangeDetectorRef,
private dataService: DataService, private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) {} ) {}
@ -45,16 +43,12 @@ export class AdminPageComponent implements OnInit {
public ngOnInit() { public ngOnInit() {
this.fetchAdminData(); this.fetchAdminData();
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject)) }
.subscribe((user) => {
this.user = user;
});
}); });
} }

20
apps/client/src/app/pages/analysis/analysis-page.component.ts

@ -2,7 +2,6 @@ import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { ToggleOption } from '@ghostfolio/client/components/toggle/interfaces/toggle-option.type'; import { ToggleOption } from '@ghostfolio/client/components/toggle/interfaces/toggle-option.type';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { import {
PortfolioItem, PortfolioItem,
@ -45,7 +44,6 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
private dataService: DataService, private dataService: DataService,
private deviceService: DeviceDetectorService, private deviceService: DeviceDetectorService,
private impersonationStorageService: ImpersonationStorageService, private impersonationStorageService: ImpersonationStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) {} ) {}
@ -81,18 +79,14 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.cd.markForCheck(); this.cd.markForCheck();
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { this.cd.markForCheck();
this.user = user; }
this.cd.markForCheck();
});
}); });
} }

62
apps/client/src/app/pages/home/home-page.component.ts

@ -10,7 +10,6 @@ import {
RANGE, RANGE,
SettingsStorageService SettingsStorageService
} from '@ghostfolio/client/services/settings-storage.service'; } from '@ghostfolio/client/services/settings-storage.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { import {
PortfolioOverview, PortfolioOverview,
@ -67,7 +66,6 @@ export class HomePageComponent implements OnDestroy, OnInit {
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private settingsStorageService: SettingsStorageService, private settingsStorageService: SettingsStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) { ) {
this.routeQueryParams = this.route.queryParams this.routeQueryParams = this.route.queryParams
@ -78,39 +76,35 @@ export class HomePageComponent implements OnDestroy, OnInit {
} }
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { this.hasPermissionToAccessFearAndGreedIndex = hasPermission(
this.user = user; this.user.permissions,
permissions.accessFearAndGreedIndex
this.hasPermissionToAccessFearAndGreedIndex = hasPermission( );
user.permissions,
permissions.accessFearAndGreedIndex if (this.hasPermissionToAccessFearAndGreedIndex) {
); this.dataService
.fetchSymbolItem('GF.FEAR_AND_GREED_INDEX')
if (this.hasPermissionToAccessFearAndGreedIndex) { .pipe(takeUntil(this.unsubscribeSubject))
this.dataService .subscribe(({ marketPrice }) => {
.fetchSymbolItem('GF.FEAR_AND_GREED_INDEX') this.fearAndGreedIndex = marketPrice;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(({ marketPrice }) => { this.cd.markForCheck();
this.fearAndGreedIndex = marketPrice; });
}
this.cd.markForCheck();
}); this.hasPermissionToReadForeignPortfolio = hasPermission(
} this.user.permissions,
permissions.readForeignPortfolio
this.hasPermissionToReadForeignPortfolio = hasPermission( );
user.permissions,
permissions.readForeignPortfolio this.cd.markForCheck();
); }
this.cd.markForCheck();
});
}); });
} }

27
apps/client/src/app/pages/pricing/pricing-page.component.ts

@ -1,6 +1,4 @@
import { ChangeDetectorRef, Component, OnInit } from '@angular/core'; import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { DataService } from '@ghostfolio/client/services/data.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { baseCurrency } from '@ghostfolio/common/config'; import { baseCurrency } from '@ghostfolio/common/config';
import { User } from '@ghostfolio/common/interfaces'; import { User } from '@ghostfolio/common/interfaces';
@ -24,8 +22,6 @@ export class PricingPageComponent implements OnInit {
*/ */
public constructor( public constructor(
private cd: ChangeDetectorRef, private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) {} ) {}
@ -33,22 +29,15 @@ export class PricingPageComponent implements OnInit {
* Initializes the controller * Initializes the controller
*/ */
public ngOnInit() { public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken(); this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
if (this.isLoggedIn) this.cd.markForCheck();
this.tokenStorageService }
.onChangeHasToken() });
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.cd.markForCheck();
});
});
} }
public ngOnDestroy() { public ngOnDestroy() {

38
apps/client/src/app/pages/transactions/transactions-page.component.ts

@ -5,7 +5,6 @@ import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { UpdateOrderDto } from '@ghostfolio/api/app/order/update-order.dto'; import { UpdateOrderDto } from '@ghostfolio/api/app/order/update-order.dto';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { User } from '@ghostfolio/common/interfaces'; import { User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -43,7 +42,6 @@ export class TransactionsPageComponent implements OnInit {
private impersonationStorageService: ImpersonationStorageService, private impersonationStorageService: ImpersonationStorageService,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) { ) {
this.routeQueryParams = route.queryParams this.routeQueryParams = route.queryParams
@ -77,27 +75,23 @@ export class TransactionsPageComponent implements OnInit {
this.hasImpersonationId = !!aId; this.hasImpersonationId = !!aId;
}); });
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { this.hasPermissionToCreateOrder = hasPermission(
this.user = user; this.user.permissions,
permissions.createOrder
this.hasPermissionToCreateOrder = hasPermission( );
user.permissions, this.hasPermissionToDeleteOrder = hasPermission(
permissions.createOrder this.user.permissions,
); permissions.deleteOrder
this.hasPermissionToDeleteOrder = hasPermission( );
user.permissions,
permissions.deleteOrder this.cd.markForCheck();
); }
this.cd.markForCheck();
});
}); });
this.fetchOrders(); this.fetchOrders();

26
apps/client/src/app/pages/zen/zen-page.component.ts

@ -2,7 +2,6 @@ import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { LineChartItem } from '@ghostfolio/client/components/line-chart/interfaces/line-chart.interface'; import { LineChartItem } from '@ghostfolio/client/components/line-chart/interfaces/line-chart.interface';
import { DataService } from '@ghostfolio/client/services/data.service'; import { DataService } from '@ghostfolio/client/services/data.service';
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service'; import { UserService } from '@ghostfolio/client/services/user/user.service';
import { PortfolioPerformance, User } from '@ghostfolio/common/interfaces'; import { PortfolioPerformance, User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -36,26 +35,21 @@ export class ZenPageComponent implements OnDestroy, OnInit {
private dataService: DataService, private dataService: DataService,
private deviceService: DeviceDetectorService, private deviceService: DeviceDetectorService,
private impersonationStorageService: ImpersonationStorageService, private impersonationStorageService: ImpersonationStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService private userService: UserService
) { ) {
this.tokenStorageService this.userService.stateChanged
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe((state) => {
this.userService if (state?.user) {
.get() this.user = state.user;
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.hasPermissionToReadForeignPortfolio = hasPermission( this.hasPermissionToReadForeignPortfolio = hasPermission(
user.permissions, this.user.permissions,
permissions.readForeignPortfolio permissions.readForeignPortfolio
); );
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

11
apps/client/src/app/services/token-storage.service.ts

@ -1,5 +1,4 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
const TOKEN_KEY = 'auth-token'; const TOKEN_KEY = 'auth-token';
@ -7,23 +6,15 @@ const TOKEN_KEY = 'auth-token';
providedIn: 'root' providedIn: 'root'
}) })
export class TokenStorageService { export class TokenStorageService {
private hasTokenChangeSubject = new BehaviorSubject<void>(null);
public constructor() {} public constructor() {}
public getToken(): string { public getToken(): string {
return window.localStorage.getItem(TOKEN_KEY); return window.localStorage.getItem(TOKEN_KEY);
} }
public onChangeHasToken() {
return this.hasTokenChangeSubject.asObservable();
}
public saveToken(token: string): void { public saveToken(token: string): void {
window.localStorage.removeItem(TOKEN_KEY); window.localStorage.removeItem(TOKEN_KEY);
window.localStorage.setItem(TOKEN_KEY, token); window.localStorage.setItem(TOKEN_KEY, token);
this.hasTokenChangeSubject.next();
} }
public signOut(): void { public signOut(): void {
@ -34,7 +25,5 @@ export class TokenStorageService {
if (utmSource) { if (utmSource) {
window.localStorage.setItem('utm_source', utmSource); window.localStorage.setItem('utm_source', utmSource);
} }
this.hasTokenChangeSubject.next();
} }
} }

Loading…
Cancel
Save