101 lines
3.9 KiB
TypeScript
101 lines
3.9 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 { ProfileService } from '../services/profile.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=' + encodeURIComponent(state.url)), { skipLocationChange: true });
|
|
});
|
|
}
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class AuthenticatedGuard implements CanActivate {
|
|
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.navigateByUrl(that.router.parseUrl('/login?target=' + encodeURIComponent(state.url)), { skipLocationChange: true, replaceUrl: true });
|
|
}
|
|
|
|
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") {
|
|
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) {
|
|
localStorage.setItem("bstly.locale", profileField.value);
|
|
updateLocale = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (darktheme != localStorage.getItem("bstly.darkTheme")) {
|
|
localStorage.setItem("bstly.darkTheme", darktheme);
|
|
updateTheme = true;
|
|
}
|
|
|
|
if (updateLocale || updateTheme) {
|
|
window.location.reload();
|
|
}
|
|
})
|
|
|
|
return true;
|
|
}).catch(function (error) {
|
|
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) => {
|
|
if (data.authenticated) {
|
|
this.router.navigateByUrl('/account/info');
|
|
return false;
|
|
}
|
|
return true;
|
|
}).catch(function (error) {
|
|
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { replaceUrl: true });
|
|
});
|
|
}
|
|
|
|
} |