bstlboard-front/src/app/auth/auth.guard.ts

108 lines
4.1 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { RequestError } from '../services/requesterror';
import { UserService } from '../services/user.service';
import { I18nService } from '../services/i18n.service';
@Injectable({
providedIn: 'root'
})
export class AuthUpdateGuard implements CanActivate {
constructor(private authService: AuthService) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.authService.getAuth().catch(function (error) { });
return true;
}
}
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
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) {
if (error instanceof RequestError && (error as RequestError).getResponse().status == 401) {
return true;
}
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { skipLocationChange: true });
});
}
}
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard implements CanActivate {
constructor(private authService: AuthService, private userService: UserService, private i18nService: I18nService, private router: Router) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this;
return this.authService.getAuth().then((data: any) => {
this.userService.get().subscribe({
next: (user: any) => {
let updateLocale = false;
let updateTheme = false;
let darktheme = 'false';
if (user.darkTheme) {
darktheme = 'true';
}
if (darktheme != localStorage.getItem("bstlboard.darkTheme")) {
localStorage.setItem("bstlboard.darkTheme", darktheme);
updateTheme = true;
}
if (this.i18nService.locales.indexOf(user.locale) != -1 && localStorage.getItem("bstlboard.locale") != user.locale) {
if (this.i18nService.locale != user.locale) {
localStorage.setItem("bstlboard.locale", user.locale);
updateLocale = true;
}
}
if (updateLocale || updateTheme) {
window.location.reload();
}
}
});
return true;
}).catch(function (error) {
if (error instanceof RequestError && (error as RequestError).getResponse().status == 401) {
return that.router.navigateByUrl(that.router.parseUrl('/login?target=' + encodeURIComponent(state.url)), { skipLocationChange: true, replaceUrl: true });
}
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { skipLocationChange: true });
});
}
}
@Injectable({
providedIn: 'root'
})
export class AnonymousGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const that = this;
return this.authService.getAuth().then((data: any) => {
this.router.navigateByUrl('/');
return false;
}).catch(function (error) {
if (error instanceof RequestError && (error as RequestError).getResponse().status == 401) {
return true;
}
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { replaceUrl: true });
});
}
}