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.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.isLoggedIn = !!this.tokenStorageService.getToken();
if (this.isLoggedIn) {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.canCreateAccount = hasPermission(
this.user.permissions,
@ -85,9 +78,6 @@ export class AppComponent implements OnDestroy, OnInit {
);
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 { 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 { baseCurrency } from '@ghostfolio/common/config';
import { User } from '@ghostfolio/common/interfaces';
@ -28,8 +26,6 @@ export class AboutPageComponent implements OnInit {
*/
public constructor(
private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {}
@ -37,21 +33,14 @@ export class AboutPageComponent implements OnInit {
* Initializes the controller
*/
public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken();
if (this.isLoggedIn)
this.tokenStorageService
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
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 { 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 { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config';
import { Access, User } from '@ghostfolio/common/interfaces';
@ -31,7 +30,6 @@ export class AccountPageComponent implements OnDestroy, OnInit {
public constructor(
private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {
this.dataService
@ -46,15 +44,11 @@ export class AccountPageComponent implements OnDestroy, OnInit {
);
});
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.hasPermissionToUpdateUserSettings = hasPermission(
this.user.permissions,
@ -62,7 +56,7 @@ export class AccountPageComponent implements OnDestroy, OnInit {
);
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 { DataService } from '@ghostfolio/client/services/data.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 { User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -43,7 +42,6 @@ export class AccountsPageComponent implements OnInit {
private impersonationStorageService: ImpersonationStorageService,
private route: ActivatedRoute,
private router: Router,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {
this.routeQueryParams = route.queryParams
@ -77,27 +75,23 @@ export class AccountsPageComponent implements OnInit {
this.hasImpersonationId = !!aId;
});
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.hasPermissionToCreateAccount = hasPermission(
user.permissions,
this.user.permissions,
permissions.createAccount
);
this.hasPermissionToDeleteAccount = hasPermission(
user.permissions,
this.user.permissions,
permissions.deleteAccount
);
this.cd.markForCheck();
});
}
});
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 { CacheService } from '@ghostfolio/client/services/cache.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 { DEFAULT_DATE_FORMAT } from '@ghostfolio/common/config';
import { AdminData, User } from '@ghostfolio/common/interfaces';
@ -35,7 +34,6 @@ export class AdminPageComponent implements OnInit {
private cacheService: CacheService,
private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {}
@ -45,16 +43,12 @@ export class AdminPageComponent implements OnInit {
public ngOnInit() {
this.fetchAdminData();
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
});
.subscribe((state) => {
if (state?.user) {
this.user = state.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 { DataService } from '@ghostfolio/client/services/data.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 {
PortfolioItem,
@ -45,7 +44,6 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
private dataService: DataService,
private deviceService: DeviceDetectorService,
private impersonationStorageService: ImpersonationStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {}
@ -81,18 +79,14 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.cd.markForCheck();
});
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.cd.markForCheck();
});
}
});
}

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

@ -10,7 +10,6 @@ import {
RANGE,
SettingsStorageService
} 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 {
PortfolioOverview,
@ -67,7 +66,6 @@ export class HomePageComponent implements OnDestroy, OnInit {
private route: ActivatedRoute,
private router: Router,
private settingsStorageService: SettingsStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {
this.routeQueryParams = this.route.queryParams
@ -78,18 +76,14 @@ export class HomePageComponent implements OnDestroy, OnInit {
}
});
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.hasPermissionToAccessFearAndGreedIndex = hasPermission(
user.permissions,
this.user.permissions,
permissions.accessFearAndGreedIndex
);
@ -105,12 +99,12 @@ export class HomePageComponent implements OnDestroy, OnInit {
}
this.hasPermissionToReadForeignPortfolio = hasPermission(
user.permissions,
this.user.permissions,
permissions.readForeignPortfolio
);
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 { 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 { baseCurrency } from '@ghostfolio/common/config';
import { User } from '@ghostfolio/common/interfaces';
@ -24,8 +22,6 @@ export class PricingPageComponent implements OnInit {
*/
public constructor(
private cd: ChangeDetectorRef,
private dataService: DataService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {}
@ -33,21 +29,14 @@ export class PricingPageComponent implements OnInit {
* Initializes the controller
*/
public ngOnInit() {
this.isLoggedIn = !!this.tokenStorageService.getToken();
if (this.isLoggedIn)
this.tokenStorageService
.onChangeHasToken()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
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 { DataService } from '@ghostfolio/client/services/data.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 { User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -43,7 +42,6 @@ export class TransactionsPageComponent implements OnInit {
private impersonationStorageService: ImpersonationStorageService,
private route: ActivatedRoute,
private router: Router,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {
this.routeQueryParams = route.queryParams
@ -77,27 +75,23 @@ export class TransactionsPageComponent implements OnInit {
this.hasImpersonationId = !!aId;
});
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.hasPermissionToCreateOrder = hasPermission(
user.permissions,
this.user.permissions,
permissions.createOrder
);
this.hasPermissionToDeleteOrder = hasPermission(
user.permissions,
this.user.permissions,
permissions.deleteOrder
);
this.cd.markForCheck();
});
}
});
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 { DataService } from '@ghostfolio/client/services/data.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 { PortfolioPerformance, User } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
@ -36,26 +35,21 @@ export class ZenPageComponent implements OnDestroy, OnInit {
private dataService: DataService,
private deviceService: DeviceDetectorService,
private impersonationStorageService: ImpersonationStorageService,
private tokenStorageService: TokenStorageService,
private userService: UserService
) {
this.tokenStorageService
.onChangeHasToken()
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
this.hasPermissionToReadForeignPortfolio = hasPermission(
user.permissions,
this.user.permissions,
permissions.readForeignPortfolio
);
this.cd.markForCheck();
});
}
});
}

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

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

Loading…
Cancel
Save