made everything use the new storage service

This commit is contained in:
Jamie Rees 2019-04-18 13:24:36 +01:00
parent a8dd23d29f
commit 90b38e09ec
9 changed files with 32 additions and 31 deletions

View file

@ -68,7 +68,7 @@ export class AppComponent implements OnInit {
} }
public ngOnInit() { public ngOnInit() {
const theme = localStorage.getItem("theme"); const theme = this.storage.get("theme");
this.onSetTheme(theme); this.onSetTheme(theme);
this.settingsService.getCustomization().subscribe(x => { this.settingsService.getCustomization().subscribe(x => {

View file

@ -3,17 +3,19 @@ import { Injectable } from "@angular/core";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { CanActivate } from "@angular/router"; import { CanActivate } from "@angular/router";
import { AuthService } from "./auth.service"; import { AuthService } from "./auth.service";
import { StorageService } from "../shared/storage/storage-service";
@Injectable() @Injectable()
export class AuthGuard implements CanActivate { export class AuthGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) { } constructor(private auth: AuthService, private router: Router,
private store: StorageService) { }
public canActivate() { public canActivate() {
if (this.auth.loggedIn()) { if (this.auth.loggedIn()) {
return true; return true;
} else { } else {
localStorage.removeItem("token"); this.store.remove("token");
this.router.navigate(["login"]); this.router.navigate(["login"]);
return false; return false;
} }

View file

@ -6,11 +6,13 @@ import { Observable } from "rxjs";
import { ServiceHelpers } from "../services"; import { ServiceHelpers } from "../services";
import { ILocalUser, IUserLogin } from "./IUserLogin"; import { ILocalUser, IUserLogin } from "./IUserLogin";
import { StorageService } from "../shared/storage/storage-service";
@Injectable() @Injectable()
export class AuthService extends ServiceHelpers { 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); super(http, "/api/v1/token", href);
} }
@ -39,7 +41,7 @@ export class AuthService extends ServiceHelpers {
public claims(): ILocalUser { public claims(): ILocalUser {
if (this.loggedIn()) { if (this.loggedIn()) {
const token = localStorage.getItem("id_token"); const token = this.store.get("id_token");
if (!token) { if (!token) {
throw new Error("Invalid token"); throw new Error("Invalid token");
} }
@ -68,6 +70,6 @@ export class AuthService extends ServiceHelpers {
} }
public logout() { public logout() {
localStorage.removeItem("id_token"); this.store.remove("id_token");
} }
} }

View file

@ -1,19 +1,21 @@
import { Component, OnInit } from "@angular/core"; import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { CookieService } from "ng2-cookies"; import { CookieService } from "ng2-cookies";
import { StorageService } from "../shared/storage/storage-service";
@Component({ @Component({
templateUrl: "cookie.component.html", templateUrl: "cookie.component.html",
}) })
export class CookieComponent implements OnInit { export class CookieComponent implements OnInit {
constructor(private readonly cookieService: CookieService, constructor(private readonly cookieService: CookieService,
private readonly router: Router) { } private readonly router: Router,
private store: StorageService) { }
public ngOnInit() { public ngOnInit() {
const cookie = this.cookieService.getAll(); const cookie = this.cookieService.getAll();
if (cookie.Auth) { if (cookie.Auth) {
const jwtVal = cookie.Auth; const jwtVal = cookie.Auth;
localStorage.setItem("id_token", jwtVal); this.store.save("id_token", jwtVal);
this.router.navigate(["search"]); this.router.navigate(["search"]);
} else { } else {
this.router.navigate(["login"]); this.router.navigate(["login"]);

View file

@ -14,6 +14,7 @@ import { DomSanitizer } from "@angular/platform-browser";
import { ImageService } from "../services"; import { ImageService } from "../services";
import { fadeInOutAnimation } from "../animations/fadeinout"; import { fadeInOutAnimation } from "../animations/fadeinout";
import { StorageService } from "../shared/storage/storage-service";
@Component({ @Component({
templateUrl: "./login.component.html", 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, 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 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.href = href;
this.route.params this.route.params
.subscribe((params: any) => { .subscribe((params: any) => {
@ -115,7 +117,7 @@ export class LoginComponent implements OnDestroy, OnInit {
} }
this.authService.login(user) this.authService.login(user)
.subscribe(x => { .subscribe(x => {
localStorage.setItem("id_token", x.access_token); this.store.save("id_token", x.access_token);
if (this.authService.loggedIn()) { if (this.authService.loggedIn()) {
this.ngOnDestroy(); this.ngOnDestroy();
@ -153,7 +155,7 @@ export class LoginComponent implements OnDestroy, OnInit {
public getPinResult(pinId: number) { public getPinResult(pinId: number) {
this.authService.oAuth(pinId).subscribe(x => { this.authService.oAuth(pinId).subscribe(x => {
if(x.access_token) { if(x.access_token) {
localStorage.setItem("id_token", x.access_token); this.store.save("id_token", x.access_token);
if (this.authService.loggedIn()) { if (this.authService.loggedIn()) {
this.ngOnDestroy(); this.ngOnDestroy();

View file

@ -3,6 +3,7 @@ import { ActivatedRoute, Router } from "@angular/router";
import { AuthService } from "../auth/auth.service"; import { AuthService } from "../auth/auth.service";
import { NotificationService } from "../services"; import { NotificationService } from "../services";
import { StorageService } from "../shared/storage/storage-service";
@Component({ @Component({
templateUrl: "./loginoauth.component.html", templateUrl: "./loginoauth.component.html",
@ -12,7 +13,8 @@ export class LoginOAuthComponent implements OnInit {
public error: string; public error: string;
constructor(private authService: AuthService, private router: Router, 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 this.route.params
.subscribe((params: any) => { .subscribe((params: any) => {
this.pin = params.pin; this.pin = params.pin;
@ -26,7 +28,7 @@ export class LoginOAuthComponent implements OnInit {
public auth() { public auth() {
this.authService.oAuth(this.pin).subscribe(x => { this.authService.oAuth(this.pin).subscribe(x => {
if (x.access_token) { if (x.access_token) {
localStorage.setItem("id_token", x.access_token); this.store.save("id_token", x.access_token);
if (this.authService.loggedIn()) { if (this.authService.loggedIn()) {
this.router.navigate(["search"]); this.router.navigate(["search"]);

View file

@ -3,6 +3,7 @@ import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { INavBar } from '../interfaces/ICommon'; import { INavBar } from '../interfaces/ICommon';
import { StorageService } from '../shared/storage/storage-service';
@Component({ @Component({
selector: 'app-my-nav', selector: 'app-my-nav',
@ -24,13 +25,14 @@ export class MyNavComponent implements OnInit {
@Output() public themeChange = new EventEmitter<string>(); @Output() public themeChange = new EventEmitter<string>();
public theme: string; public theme: string;
constructor(private breakpointObserver: BreakpointObserver) { constructor(private breakpointObserver: BreakpointObserver,
private store: StorageService) {
} }
public ngOnInit(): void { public ngOnInit(): void {
this.theme = localStorage.getItem("theme"); this.theme = this.store.get("theme");
if(!this.theme) { if(!this.theme) {
localStorage.setItem("theme","light"); this.store.save("theme","light");
} }
} }
@ -49,14 +51,13 @@ export class MyNavComponent implements OnInit {
public switchTheme() { public switchTheme() {
if (this.theme) { if (this.theme) {
localStorage.removeItem("theme");
let newTheme = ""; let newTheme = "";
if (this.theme === "dark") { if (this.theme === "dark") {
newTheme = "light"; newTheme = "light";
} else { } else {
newTheme = "dark"; newTheme = "dark";
} }
localStorage.setItem("theme", newTheme) this.store.save("theme", newTheme)
this.theme = newTheme; this.theme = newTheme;
this.themeChange.emit(newTheme); this.themeChange.emit(newTheme);
} }

View file

@ -78,15 +78,4 @@ export class IdentityService extends ServiceHelpers {
public getNotificationPreferencesForUser(userId: string): Observable<INotificationPreferences[]> { public getNotificationPreferencesForUser(userId: string): Observable<INotificationPreferences[]> {
return this.http.get<INotificationPreferences[]>(`${this.url}notificationpreferences/${userId}`, {headers: this.headers}); return this.http.get<INotificationPreferences[]>(`${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;
}
} }

View file

@ -5,6 +5,7 @@ import { PlatformLocation } from "@angular/common";
import { AuthService } from "../../auth/auth.service"; import { AuthService } from "../../auth/auth.service";
import { PlexOAuthService, PlexService, PlexTvService, SettingsService } from "../../services"; import { PlexOAuthService, PlexService, PlexTvService, SettingsService } from "../../services";
import { IdentityService, NotificationService } from "../../services"; import { IdentityService, NotificationService } from "../../services";
import { StorageService } from "../../shared/storage/storage-service";
@Component({ @Component({
templateUrl: "./plex.component.html", templateUrl: "./plex.component.html",
@ -23,7 +24,7 @@ export class PlexComponent implements OnInit, OnDestroy {
private identityService: IdentityService, private plexTv: PlexTvService, private identityService: IdentityService, private plexTv: PlexTvService,
private settingsService: SettingsService, private settingsService: SettingsService,
private location: PlatformLocation, private authService: AuthService, private location: PlatformLocation, private authService: AuthService,
private plexOauth: PlexOAuthService) { } private plexOauth: PlexOAuthService, private store: StorageService) { }
public ngOnInit(): void { public ngOnInit(): void {
const base = this.location.getBaseHrefFromDOM(); const base = this.location.getBaseHrefFromDOM();
@ -95,7 +96,7 @@ export class PlexComponent implements OnInit, OnDestroy {
}).subscribe(u => { }).subscribe(u => {
if (u.result) { if (u.result) {
this.authService.oAuth(pinId).subscribe(c => { 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"]); this.router.navigate(["login"]);
}); });
} else { } else {