From 885c95e63be463623121c1855ef4135d3aa3c9d1 Mon Sep 17 00:00:00 2001 From: _Bastler <_Bastler@bstly.de> Date: Wed, 8 Sep 2021 11:09:26 +0200 Subject: [PATCH] control back button --- src/app/auth/auth.guard.ts | 56 +++++++++---------- .../pages/form-login/form-login.component.ts | 24 ++++---- .../unavailable/unavailable.component.ts | 18 +++--- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/app/auth/auth.guard.ts b/src/app/auth/auth.guard.ts index 7edf35a..d60c470 100644 --- a/src/app/auth/auth.guard.ts +++ b/src/app/auth/auth.guard.ts @@ -1,19 +1,19 @@ -import {Injectable} from '@angular/core'; -import {Router} from '@angular/router'; -import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; -import {AuthService} from '../services/auth.service'; -import {ProfileService} from '../services/profile.service'; -import {I18nService} from '../services/i18n.service'; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; +import { AuthService } from '../services/auth.service'; +import { ProfileService } from '../services/profile.service'; +import { I18nService } from '../services/i18n.service'; @Injectable({ providedIn: 'root' }) export class AuthUpdateGuard implements CanActivate { - constructor(private authService: AuthService) {} + constructor(private authService: AuthService) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - this.authService.getAuth().catch(function(error) {}); + this.authService.getAuth().catch(function (error) { }); return true; } } @@ -22,14 +22,14 @@ export class AuthUpdateGuard implements CanActivate { providedIn: 'root' }) export class AuthGuard implements CanActivate { - constructor(private authService: AuthService, private router: Router) {} + constructor(private authService: AuthService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const that = this; return this.authService.getAuth().then(response => { return true; - }).catch(function(error) { - return that.router.parseUrl('/unavailable?target=' + state.url); + }).catch(function (error) { + return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { skipLocationChange: true }); }); } } @@ -38,43 +38,43 @@ export class AuthGuard implements CanActivate { providedIn: 'root' }) export class AuthenticatedGuard implements CanActivate { - constructor(private authService: AuthService, private profileService: ProfileService, private i18nService: I18nService, private router: Router) {} + constructor(private authService: AuthService, private profileService: ProfileService, private i18nService: I18nService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const that = this; return this.authService.getAuth().then((data: any) => { - if(!data.authenticated) { - return that.router.parseUrl('/login?target=' + state.url); + if (!data.authenticated) { + return that.router.navigateByUrl(that.router.parseUrl('/login?target=' + state.url), { skipLocationChange: true, replaceUrl: true }); } - this.profileService.get(["locale", "darkTheme"]).subscribe((profileFields: any) => { + this.profileService.get([ "locale", "darkTheme" ]).subscribe((profileFields: any) => { let updateLocale = false; let darktheme = 'false'; let updateTheme = false; - for(let profileField of profileFields) { - if(profileField.name == "darkTheme") { + for (let profileField of profileFields) { + if (profileField.name == "darkTheme") { darktheme = profileField.value; - } else if(profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1 && localStorage.getItem("bstly.locale") != profileField.value) { - if(this.i18nService.locale != profileField.value) { + } else if (profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1 && localStorage.getItem("bstly.locale") != profileField.value) { + if (this.i18nService.locale != profileField.value) { localStorage.setItem("bstly.locale", profileField.value); updateLocale = true; } } } - if(darktheme != localStorage.getItem("bstly.darkTheme")) { + if (darktheme != localStorage.getItem("bstly.darkTheme")) { localStorage.setItem("bstly.darkTheme", darktheme); updateTheme = true; } - if(updateLocale || updateTheme) { - window.location.reload(); + if (updateLocale || updateTheme) { + window.location.reload(); } }) return true; - }).catch(function(error) { - return that.router.parseUrl('/unavailable?target=' + state.url); + }).catch(function (error) { + return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { skipLocationChange: true }); }); } } @@ -83,18 +83,18 @@ export class AuthenticatedGuard implements CanActivate { providedIn: 'root' }) export class AnonymousGuard implements CanActivate { - constructor(private authService: AuthService, private router: Router) {} + constructor(private authService: AuthService, private router: Router) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const that = this; return this.authService.getAuth().then((data: any) => { - if(data.authenticated) { + if (data.authenticated) { this.router.navigateByUrl('/account/info'); return false; } return true; - }).catch(function(error) { - return that.router.parseUrl('/unavailable?target=' + state.url); + }).catch(function (error) { + return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { replaceUrl: true }); }); } diff --git a/src/app/pages/form-login/form-login.component.ts b/src/app/pages/form-login/form-login.component.ts index 4f5cbdd..780da65 100644 --- a/src/app/pages/form-login/form-login.component.ts +++ b/src/app/pages/form-login/form-login.component.ts @@ -1,40 +1,40 @@ -import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; -import {ActivatedRoute, Router} from '@angular/router'; +import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; -import {environment} from '../../../environments/environment'; +import { environment } from '../../../environments/environment'; @Component({ selector: 'app-form-login', templateUrl: './form-login.component.html', - styleUrls: ['./form-login.component.scss'] + styleUrls: [ './form-login.component.scss' ] }) export class FormLoginComponent implements OnInit { @ViewChild('loginForm') loginForm: ElementRef; loginInvalid: boolean; apiUrl = environment.apiUrl; - targetRoute : string; + targetRoute: string; constructor( private router: Router, - private route: ActivatedRoute) {} + private route: ActivatedRoute) { } async ngOnInit() { this.route.queryParams.subscribe(params => { - if(params['target']) { - this.targetRoute = params['target']; - this.router.navigate([], {queryParams: {target: null}, queryParamsHandling: 'merge'}); + if (params[ 'target' ]) { + this.targetRoute = params[ 'target' ]; + this.router.navigate([], { queryParams: { target: null }, queryParamsHandling: 'merge', replaceUrl: true }); } - if(params['error'] || params['error'] == '') { + if (params[ 'error' ] || params[ 'error' ] == '') { this.loginInvalid = true; - this.router.navigate([], {queryParams: {error: null}, queryParamsHandling: 'merge'}); + this.router.navigate([], { queryParams: { error: null }, queryParamsHandling: 'merge', replaceUrl: true }); } }); } ngAfterViewInit(): void { - if(this.targetRoute) { + if (this.targetRoute) { this.loginForm.nativeElement.action = this.loginForm.nativeElement.action + "?forward=" + window.location.origin + this.targetRoute; } } diff --git a/src/app/pages/unavailable/unavailable.component.ts b/src/app/pages/unavailable/unavailable.component.ts index 76a394a..b5abdbe 100644 --- a/src/app/pages/unavailable/unavailable.component.ts +++ b/src/app/pages/unavailable/unavailable.component.ts @@ -1,6 +1,6 @@ -import {Component, OnInit} from '@angular/core'; -import {Location} from '@angular/common' -import {Router, ActivatedRoute} from '@angular/router'; +import { Component, OnInit } from '@angular/core'; +import { Location } from '@angular/common' +import { Router, ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-unavailable', @@ -13,23 +13,23 @@ export class UnavailableComponent implements OnInit { constructor( private location: Location, private router: Router, - private route: ActivatedRoute) {} + private route: ActivatedRoute) { } ngOnInit(): void { this.route.queryParams.subscribe(params => { - if(params['target']) { - this.targetRoute = params['target']; - this.router.navigate([], {queryParams: {target: null}, queryParamsHandling: 'merge'}); + if (params[ 'target' ]) { + this.targetRoute = params[ 'target' ]; + this.router.navigate([], { queryParams: { target: null }, queryParamsHandling: 'merge', skipLocationChange: true }); } }); } retry() { - if(!this.targetRoute || this.targetRoute === "unavailable" || this.targetRoute === "/unavailable") { + if (!this.targetRoute || this.targetRoute === "unavailable" || this.targetRoute === "/unavailable") { this.location.back; } else { - this.router.navigate([this.targetRoute]); + this.router.navigate([ this.targetRoute ], { skipLocationChange: true }); } }