control back button

This commit is contained in:
_Bastler 2021-09-08 11:09:26 +02:00
parent 258f253a14
commit 885c95e63b
3 changed files with 49 additions and 49 deletions

View File

@ -1,19 +1,19 @@
import {Injectable} from '@angular/core'; import { Injectable } from '@angular/core';
import {Router} from '@angular/router'; import { Router } from '@angular/router';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import {AuthService} from '../services/auth.service'; import { AuthService } from '../services/auth.service';
import {ProfileService} from '../services/profile.service'; import { ProfileService } from '../services/profile.service';
import {I18nService} from '../services/i18n.service'; import { I18nService } from '../services/i18n.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthUpdateGuard implements CanActivate { export class AuthUpdateGuard implements CanActivate {
constructor(private authService: AuthService) {} constructor(private authService: AuthService) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.authService.getAuth().catch(function(error) {}); this.authService.getAuth().catch(function (error) { });
return true; return true;
} }
} }
@ -22,14 +22,14 @@ export class AuthUpdateGuard implements CanActivate {
providedIn: 'root' providedIn: 'root'
}) })
export class AuthGuard implements CanActivate { export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {} constructor(private authService: AuthService, private router: Router) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this; const that = this;
return this.authService.getAuth().then(response => { return this.authService.getAuth().then(response => {
return true; return true;
}).catch(function(error) { }).catch(function (error) {
return that.router.parseUrl('/unavailable?target=' + state.url); return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { skipLocationChange: true });
}); });
} }
} }
@ -38,43 +38,43 @@ export class AuthGuard implements CanActivate {
providedIn: 'root' providedIn: 'root'
}) })
export class AuthenticatedGuard implements CanActivate { 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) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this; const that = this;
return this.authService.getAuth().then((data: any) => { return this.authService.getAuth().then((data: any) => {
if(!data.authenticated) { if (!data.authenticated) {
return that.router.parseUrl('/login?target=' + state.url); 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 updateLocale = false;
let darktheme = 'false'; let darktheme = 'false';
let updateTheme = false; let updateTheme = false;
for(let profileField of profileFields) { for (let profileField of profileFields) {
if(profileField.name == "darkTheme") { if (profileField.name == "darkTheme") {
darktheme = profileField.value; darktheme = profileField.value;
} else if(profileField.name == "locale" && this.i18nService.locales.indexOf(profileField.value) != -1 && localStorage.getItem("bstly.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) { if (this.i18nService.locale != profileField.value) {
localStorage.setItem("bstly.locale", profileField.value); localStorage.setItem("bstly.locale", profileField.value);
updateLocale = true; updateLocale = true;
} }
} }
} }
if(darktheme != localStorage.getItem("bstly.darkTheme")) { if (darktheme != localStorage.getItem("bstly.darkTheme")) {
localStorage.setItem("bstly.darkTheme", darktheme); localStorage.setItem("bstly.darkTheme", darktheme);
updateTheme = true; updateTheme = true;
} }
if(updateLocale || updateTheme) { if (updateLocale || updateTheme) {
window.location.reload(); window.location.reload();
} }
}) })
return true; return true;
}).catch(function(error) { }).catch(function (error) {
return that.router.parseUrl('/unavailable?target=' + state.url); return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { skipLocationChange: true });
}); });
} }
} }
@ -83,18 +83,18 @@ export class AuthenticatedGuard implements CanActivate {
providedIn: 'root' providedIn: 'root'
}) })
export class AnonymousGuard implements CanActivate { export class AnonymousGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {} constructor(private authService: AuthService, private router: Router) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this; const that = this;
return this.authService.getAuth().then((data: any) => { return this.authService.getAuth().then((data: any) => {
if(data.authenticated) { if (data.authenticated) {
this.router.navigateByUrl('/account/info'); this.router.navigateByUrl('/account/info');
return false; return false;
} }
return true; return true;
}).catch(function(error) { }).catch(function (error) {
return that.router.parseUrl('/unavailable?target=' + state.url); return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { replaceUrl: true });
}); });
} }

View File

@ -1,40 +1,40 @@
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import {environment} from '../../../environments/environment'; import { environment } from '../../../environments/environment';
@Component({ @Component({
selector: 'app-form-login', selector: 'app-form-login',
templateUrl: './form-login.component.html', templateUrl: './form-login.component.html',
styleUrls: ['./form-login.component.scss'] styleUrls: [ './form-login.component.scss' ]
}) })
export class FormLoginComponent implements OnInit { export class FormLoginComponent implements OnInit {
@ViewChild('loginForm') loginForm: ElementRef; @ViewChild('loginForm') loginForm: ElementRef;
loginInvalid: boolean; loginInvalid: boolean;
apiUrl = environment.apiUrl; apiUrl = environment.apiUrl;
targetRoute : string; targetRoute: string;
constructor( constructor(
private router: Router, private router: Router,
private route: ActivatedRoute) {} private route: ActivatedRoute) { }
async ngOnInit() { async ngOnInit() {
this.route.queryParams.subscribe(params => { this.route.queryParams.subscribe(params => {
if(params['target']) { if (params[ 'target' ]) {
this.targetRoute = params['target']; this.targetRoute = params[ 'target' ];
this.router.navigate([], {queryParams: {target: null}, queryParamsHandling: 'merge'}); this.router.navigate([], { queryParams: { target: null }, queryParamsHandling: 'merge', replaceUrl: true });
} }
if(params['error'] || params['error'] == '') { if (params[ 'error' ] || params[ 'error' ] == '') {
this.loginInvalid = true; this.loginInvalid = true;
this.router.navigate([], {queryParams: {error: null}, queryParamsHandling: 'merge'}); this.router.navigate([], { queryParams: { error: null }, queryParamsHandling: 'merge', replaceUrl: true });
} }
}); });
} }
ngAfterViewInit(): void { ngAfterViewInit(): void {
if(this.targetRoute) { if (this.targetRoute) {
this.loginForm.nativeElement.action = this.loginForm.nativeElement.action + "?forward=" + window.location.origin + this.targetRoute; this.loginForm.nativeElement.action = this.loginForm.nativeElement.action + "?forward=" + window.location.origin + this.targetRoute;
} }
} }

View File

@ -1,6 +1,6 @@
import {Component, OnInit} from '@angular/core'; import { Component, OnInit } from '@angular/core';
import {Location} from '@angular/common' import { Location } from '@angular/common'
import {Router, ActivatedRoute} from '@angular/router'; import { Router, ActivatedRoute } from '@angular/router';
@Component({ @Component({
selector: 'app-unavailable', selector: 'app-unavailable',
@ -13,23 +13,23 @@ export class UnavailableComponent implements OnInit {
constructor( constructor(
private location: Location, private location: Location,
private router: Router, private router: Router,
private route: ActivatedRoute) {} private route: ActivatedRoute) { }
ngOnInit(): void { ngOnInit(): void {
this.route.queryParams.subscribe(params => { this.route.queryParams.subscribe(params => {
if(params['target']) { if (params[ 'target' ]) {
this.targetRoute = params['target']; this.targetRoute = params[ 'target' ];
this.router.navigate([], {queryParams: {target: null}, queryParamsHandling: 'merge'}); this.router.navigate([], { queryParams: { target: null }, queryParamsHandling: 'merge', skipLocationChange: true });
} }
}); });
} }
retry() { retry() {
if(!this.targetRoute || this.targetRoute === "unavailable" || this.targetRoute === "/unavailable") { if (!this.targetRoute || this.targetRoute === "unavailable" || this.targetRoute === "/unavailable") {
this.location.back; this.location.back;
} else { } else {
this.router.navigate([this.targetRoute]); this.router.navigate([ this.targetRoute ], { skipLocationChange: true });
} }
} }