Browse Source

Clean up tokenStorageService usage

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

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

@ -66,18 +66,11 @@ 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
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.canCreateAccount = hasPermission( this.canCreateAccount = hasPermission(
this.user.permissions, this.user.permissions,
@ -85,9 +78,6 @@ export class AppComponent implements OnDestroy, OnInit {
); );
this.cd.markForCheck(); this.cd.markForCheck();
});
} else {
this.user = null;
} }
}); });
} }

21
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,21 +33,14 @@ export class AboutPageComponent implements OnInit {
* Initializes the controller * Initializes the controller
*/ */
public ngOnInit() { public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken(); this.userService.stateChanged
if (this.isLoggedIn)
this.tokenStorageService
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { .subscribe((state) => {
this.user = user; if (state?.user) {
this.user = state.user;
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

16
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,15 +44,11 @@ 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,
@ -62,7 +56,7 @@ export class AccountPageComponent implements OnDestroy, OnInit {
); );
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

20
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.user = user;
this.hasPermissionToCreateAccount = hasPermission( this.hasPermissionToCreateAccount = hasPermission(
user.permissions, this.user.permissions,
permissions.createAccount permissions.createAccount
); );
this.hasPermissionToDeleteAccount = hasPermission( this.hasPermissionToDeleteAccount = hasPermission(
user.permissions, this.user.permissions,
permissions.deleteAccount 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;
});
}); });
} }

16
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.user = user;
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

20
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,18 +76,14 @@ 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.user = user;
this.hasPermissionToAccessFearAndGreedIndex = hasPermission( this.hasPermissionToAccessFearAndGreedIndex = hasPermission(
user.permissions, this.user.permissions,
permissions.accessFearAndGreedIndex permissions.accessFearAndGreedIndex
); );
@ -105,12 +99,12 @@ export class HomePageComponent implements OnDestroy, OnInit {
} }
this.hasPermissionToReadForeignPortfolio = hasPermission( this.hasPermissionToReadForeignPortfolio = hasPermission(
user.permissions, this.user.permissions,
permissions.readForeignPortfolio permissions.readForeignPortfolio
); );
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

21
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,21 +29,14 @@ export class PricingPageComponent implements OnInit {
* Initializes the controller * Initializes the controller
*/ */
public ngOnInit() { public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken(); this.userService.stateChanged
if (this.isLoggedIn)
this.tokenStorageService
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => { .subscribe((state) => {
this.user = user; if (state?.user) {
this.user = state.user;
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
} }

20
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.user = user;
this.hasPermissionToCreateOrder = hasPermission( this.hasPermissionToCreateOrder = hasPermission(
user.permissions, this.user.permissions,
permissions.createOrder permissions.createOrder
); );
this.hasPermissionToDeleteOrder = hasPermission( this.hasPermissionToDeleteOrder = hasPermission(
user.permissions, this.user.permissions,
permissions.deleteOrder permissions.deleteOrder
); );
this.cd.markForCheck(); this.cd.markForCheck();
}); }
}); });
this.fetchOrders(); this.fetchOrders();

18
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