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

102 lines
3.7 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 { 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) {
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.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) => {
if (!data || !data.authenticated) {
return that.router.navigateByUrl(that.router.parseUrl('/login?target=' + encodeURIComponent(state.url)), { skipLocationChange: true, replaceUrl: true });
}
this.userService.get().subscribe((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) {
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.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) => {
if (data && data.authenticated) {
this.router.navigateByUrl('/');
return false;
}
return true;
}).catch(function (error) {
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + next.url), { replaceUrl: true });
});
}
}