From 90b38e09ec04ce0aaac855e48d06f7b65ace8d5f Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Thu, 18 Apr 2019 13:24:36 +0100 Subject: [PATCH] made everything use the new storage service --- src/Ombi/ClientApp/src/app/app.component.ts | 2 +- src/Ombi/ClientApp/src/app/auth/auth.guard.ts | 6 ++++-- src/Ombi/ClientApp/src/app/auth/auth.service.ts | 8 +++++--- src/Ombi/ClientApp/src/app/auth/cookie.component.ts | 6 ++++-- src/Ombi/ClientApp/src/app/login/login.component.ts | 8 +++++--- .../ClientApp/src/app/login/loginoauth.component.ts | 6 ++++-- src/Ombi/ClientApp/src/app/my-nav/my-nav.component.ts | 11 ++++++----- .../ClientApp/src/app/services/identity.service.ts | 11 ----------- .../ClientApp/src/app/wizard/plex/plex.component.ts | 5 +++-- 9 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/Ombi/ClientApp/src/app/app.component.ts b/src/Ombi/ClientApp/src/app/app.component.ts index f2262c1e5..6f022eac3 100644 --- a/src/Ombi/ClientApp/src/app/app.component.ts +++ b/src/Ombi/ClientApp/src/app/app.component.ts @@ -68,7 +68,7 @@ export class AppComponent implements OnInit { } public ngOnInit() { - const theme = localStorage.getItem("theme"); + const theme = this.storage.get("theme"); this.onSetTheme(theme); this.settingsService.getCustomization().subscribe(x => { diff --git a/src/Ombi/ClientApp/src/app/auth/auth.guard.ts b/src/Ombi/ClientApp/src/app/auth/auth.guard.ts index d06d8c590..4313587ab 100644 --- a/src/Ombi/ClientApp/src/app/auth/auth.guard.ts +++ b/src/Ombi/ClientApp/src/app/auth/auth.guard.ts @@ -3,17 +3,19 @@ import { Injectable } from "@angular/core"; import { Router } from "@angular/router"; import { CanActivate } from "@angular/router"; import { AuthService } from "./auth.service"; +import { StorageService } from "../shared/storage/storage-service"; @Injectable() export class AuthGuard implements CanActivate { - constructor(private auth: AuthService, private router: Router) { } + constructor(private auth: AuthService, private router: Router, + private store: StorageService) { } public canActivate() { if (this.auth.loggedIn()) { return true; } else { - localStorage.removeItem("token"); + this.store.remove("token"); this.router.navigate(["login"]); return false; } diff --git a/src/Ombi/ClientApp/src/app/auth/auth.service.ts b/src/Ombi/ClientApp/src/app/auth/auth.service.ts index 8007dce80..cdc626745 100644 --- a/src/Ombi/ClientApp/src/app/auth/auth.service.ts +++ b/src/Ombi/ClientApp/src/app/auth/auth.service.ts @@ -6,11 +6,13 @@ import { Observable } from "rxjs"; import { ServiceHelpers } from "../services"; import { ILocalUser, IUserLogin } from "./IUserLogin"; +import { StorageService } from "../shared/storage/storage-service"; @Injectable() export class AuthService extends ServiceHelpers { - constructor(http: HttpClient, @Inject(APP_BASE_HREF) href: string, private jwtHelperService: JwtHelperService) { + constructor(http: HttpClient, @Inject(APP_BASE_HREF) href: string, private jwtHelperService: JwtHelperService, + private store: StorageService) { super(http, "/api/v1/token", href); } @@ -39,7 +41,7 @@ export class AuthService extends ServiceHelpers { public claims(): ILocalUser { if (this.loggedIn()) { - const token = localStorage.getItem("id_token"); + const token = this.store.get("id_token"); if (!token) { throw new Error("Invalid token"); } @@ -68,6 +70,6 @@ export class AuthService extends ServiceHelpers { } public logout() { - localStorage.removeItem("id_token"); + this.store.remove("id_token"); } } diff --git a/src/Ombi/ClientApp/src/app/auth/cookie.component.ts b/src/Ombi/ClientApp/src/app/auth/cookie.component.ts index 21b61b37e..515f8c603 100644 --- a/src/Ombi/ClientApp/src/app/auth/cookie.component.ts +++ b/src/Ombi/ClientApp/src/app/auth/cookie.component.ts @@ -1,19 +1,21 @@ import { Component, OnInit } from "@angular/core"; import { Router } from "@angular/router"; import { CookieService } from "ng2-cookies"; +import { StorageService } from "../shared/storage/storage-service"; @Component({ templateUrl: "cookie.component.html", }) export class CookieComponent implements OnInit { constructor(private readonly cookieService: CookieService, - private readonly router: Router) { } + private readonly router: Router, + private store: StorageService) { } public ngOnInit() { const cookie = this.cookieService.getAll(); if (cookie.Auth) { const jwtVal = cookie.Auth; - localStorage.setItem("id_token", jwtVal); + this.store.save("id_token", jwtVal); this.router.navigate(["search"]); } else { this.router.navigate(["login"]); diff --git a/src/Ombi/ClientApp/src/app/login/login.component.ts b/src/Ombi/ClientApp/src/app/login/login.component.ts index 84530c3da..2bf21701e 100644 --- a/src/Ombi/ClientApp/src/app/login/login.component.ts +++ b/src/Ombi/ClientApp/src/app/login/login.component.ts @@ -14,6 +14,7 @@ import { DomSanitizer } from "@angular/platform-browser"; import { ImageService } from "../services"; import { fadeInOutAnimation } from "../animations/fadeinout"; +import { StorageService } from "../shared/storage/storage-service"; @Component({ templateUrl: "./login.component.html", @@ -49,7 +50,8 @@ export class LoginComponent implements OnDestroy, OnInit { constructor(private authService: AuthService, private router: Router, private notify: NotificationService, private status: StatusService, private fb: FormBuilder, private settingsService: SettingsService, private images: ImageService, private sanitizer: DomSanitizer, - private route: ActivatedRoute, @Inject(APP_BASE_HREF) href:string, private translate: TranslateService, private plexTv: PlexTvService) { + private route: ActivatedRoute, @Inject(APP_BASE_HREF) href:string, private translate: TranslateService, private plexTv: PlexTvService, + private store: StorageService) { this.href = href; this.route.params .subscribe((params: any) => { @@ -115,7 +117,7 @@ export class LoginComponent implements OnDestroy, OnInit { } this.authService.login(user) .subscribe(x => { - localStorage.setItem("id_token", x.access_token); + this.store.save("id_token", x.access_token); if (this.authService.loggedIn()) { this.ngOnDestroy(); @@ -153,7 +155,7 @@ export class LoginComponent implements OnDestroy, OnInit { public getPinResult(pinId: number) { this.authService.oAuth(pinId).subscribe(x => { if(x.access_token) { - localStorage.setItem("id_token", x.access_token); + this.store.save("id_token", x.access_token); if (this.authService.loggedIn()) { this.ngOnDestroy(); diff --git a/src/Ombi/ClientApp/src/app/login/loginoauth.component.ts b/src/Ombi/ClientApp/src/app/login/loginoauth.component.ts index 18f90be5f..0671fd7c3 100644 --- a/src/Ombi/ClientApp/src/app/login/loginoauth.component.ts +++ b/src/Ombi/ClientApp/src/app/login/loginoauth.component.ts @@ -3,6 +3,7 @@ import { ActivatedRoute, Router } from "@angular/router"; import { AuthService } from "../auth/auth.service"; import { NotificationService } from "../services"; +import { StorageService } from "../shared/storage/storage-service"; @Component({ templateUrl: "./loginoauth.component.html", @@ -12,7 +13,8 @@ export class LoginOAuthComponent implements OnInit { public error: string; constructor(private authService: AuthService, private router: Router, - private route: ActivatedRoute, private notify: NotificationService) { + private route: ActivatedRoute, private notify: NotificationService, + private store: StorageService) { this.route.params .subscribe((params: any) => { this.pin = params.pin; @@ -26,7 +28,7 @@ export class LoginOAuthComponent implements OnInit { public auth() { this.authService.oAuth(this.pin).subscribe(x => { if (x.access_token) { - localStorage.setItem("id_token", x.access_token); + this.store.save("id_token", x.access_token); if (this.authService.loggedIn()) { this.router.navigate(["search"]); diff --git a/src/Ombi/ClientApp/src/app/my-nav/my-nav.component.ts b/src/Ombi/ClientApp/src/app/my-nav/my-nav.component.ts index ff125d8f6..a3825d4e8 100644 --- a/src/Ombi/ClientApp/src/app/my-nav/my-nav.component.ts +++ b/src/Ombi/ClientApp/src/app/my-nav/my-nav.component.ts @@ -3,6 +3,7 @@ import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { INavBar } from '../interfaces/ICommon'; +import { StorageService } from '../shared/storage/storage-service'; @Component({ selector: 'app-my-nav', @@ -24,13 +25,14 @@ export class MyNavComponent implements OnInit { @Output() public themeChange = new EventEmitter(); public theme: string; - constructor(private breakpointObserver: BreakpointObserver) { + constructor(private breakpointObserver: BreakpointObserver, + private store: StorageService) { } public ngOnInit(): void { - this.theme = localStorage.getItem("theme"); + this.theme = this.store.get("theme"); if(!this.theme) { - localStorage.setItem("theme","light"); + this.store.save("theme","light"); } } @@ -49,14 +51,13 @@ export class MyNavComponent implements OnInit { public switchTheme() { if (this.theme) { - localStorage.removeItem("theme"); let newTheme = ""; if (this.theme === "dark") { newTheme = "light"; } else { newTheme = "dark"; } - localStorage.setItem("theme", newTheme) + this.store.save("theme", newTheme) this.theme = newTheme; this.themeChange.emit(newTheme); } diff --git a/src/Ombi/ClientApp/src/app/services/identity.service.ts b/src/Ombi/ClientApp/src/app/services/identity.service.ts index 14db058ee..37e8afbda 100644 --- a/src/Ombi/ClientApp/src/app/services/identity.service.ts +++ b/src/Ombi/ClientApp/src/app/services/identity.service.ts @@ -78,15 +78,4 @@ export class IdentityService extends ServiceHelpers { public getNotificationPreferencesForUser(userId: string): Observable { return this.http.get(`${this.url}notificationpreferences/${userId}`, {headers: this.headers}); } - - public hasRole(role: string): boolean { - const roles = localStorage.getItem("roles") as any as string[] | null; - if (roles) { - if (roles.indexOf(role) > -1) { - return true; - } - return false; - } - return false; - } } diff --git a/src/Ombi/ClientApp/src/app/wizard/plex/plex.component.ts b/src/Ombi/ClientApp/src/app/wizard/plex/plex.component.ts index 909e21480..076b93363 100644 --- a/src/Ombi/ClientApp/src/app/wizard/plex/plex.component.ts +++ b/src/Ombi/ClientApp/src/app/wizard/plex/plex.component.ts @@ -5,6 +5,7 @@ import { PlatformLocation } from "@angular/common"; import { AuthService } from "../../auth/auth.service"; import { PlexOAuthService, PlexService, PlexTvService, SettingsService } from "../../services"; import { IdentityService, NotificationService } from "../../services"; +import { StorageService } from "../../shared/storage/storage-service"; @Component({ templateUrl: "./plex.component.html", @@ -23,7 +24,7 @@ export class PlexComponent implements OnInit, OnDestroy { private identityService: IdentityService, private plexTv: PlexTvService, private settingsService: SettingsService, private location: PlatformLocation, private authService: AuthService, - private plexOauth: PlexOAuthService) { } + private plexOauth: PlexOAuthService, private store: StorageService) { } public ngOnInit(): void { const base = this.location.getBaseHrefFromDOM(); @@ -95,7 +96,7 @@ export class PlexComponent implements OnInit, OnDestroy { }).subscribe(u => { if (u.result) { this.authService.oAuth(pinId).subscribe(c => { - localStorage.setItem("id_token", c.access_token); + this.store.save("id_token", c.access_token); this.router.navigate(["login"]); }); } else {