2021-03-26 10:17:28 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Router} from '@angular/router';
|
2021-03-29 14:35:25 +02:00
|
|
|
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
|
2021-03-26 10:17:28 +01:00
|
|
|
import {AuthService} from '../services/auth.service';
|
2021-03-29 14:35:25 +02:00
|
|
|
import {ProfileService} from '../services/profile.service';
|
|
|
|
import {I18nService} from '../services/i18n.service';
|
|
|
|
|
2020-11-02 08:29:52 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthUpdateGuard implements CanActivate {
|
2021-03-26 10:17:28 +01:00
|
|
|
constructor(private authService: AuthService) {}
|
2020-11-02 08:29:52 +01:00
|
|
|
|
|
|
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
2021-03-26 10:17:28 +01:00
|
|
|
this.authService.getAuth().catch(function(error) {});
|
2020-11-02 08:29:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthGuard implements CanActivate {
|
2021-03-26 10:17:28 +01:00
|
|
|
constructor(private authService: AuthService, private router: Router) {}
|
2020-11-02 08:29:52 +01:00
|
|
|
|
2021-03-26 10:17:28 +01:00
|
|
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
2020-11-02 08:29:52 +01:00
|
|
|
const that = this;
|
|
|
|
return this.authService.getAuth().then(response => {
|
|
|
|
return true;
|
2021-03-26 10:17:28 +01:00
|
|
|
}).catch(function(error) {
|
2021-04-16 08:57:53 +02:00
|
|
|
return that.router.parseUrl('/unavailable?target=' + state.url);
|
2020-11-02 08:29:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthenticatedGuard implements CanActivate {
|
2021-03-29 14:35:25 +02:00
|
|
|
constructor(private authService: AuthService, private profileService: ProfileService, private i18nService: I18nService, private router: Router) {}
|
2020-11-02 08:29:52 +01:00
|
|
|
|
2021-03-26 10:17:28 +01:00
|
|
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
2020-11-02 08:29:52 +01:00
|
|
|
const that = this;
|
|
|
|
return this.authService.getAuth().then((data: any) => {
|
2021-03-26 10:17:28 +01:00
|
|
|
if(!data.authenticated) {
|
2021-04-16 08:57:53 +02:00
|
|
|
return that.router.parseUrl('/login?target=' + state.url);
|
2020-11-02 08:29:52 +01:00
|
|
|
}
|
2021-03-29 14:35:25 +02:00
|
|
|
|
2021-04-03 13:38:25 +02:00
|
|
|
this.profileService.get(["locale", "darkTheme"]).subscribe((profileFields: any) => {
|
2021-03-29 14:59:22 +02:00
|
|
|
let updateLocale = false;
|
|
|
|
let darktheme = 'false';
|
|
|
|
let updateTheme = false;
|
2021-03-29 14:35:25 +02:00
|
|
|
for(let profileField of profileFields) {
|
2021-03-29 14:59:22 +02:00
|
|
|
if(profileField.name == "darkTheme") {
|
|
|
|
darktheme = profileField.value;
|
2021-03-29 14:35:25 +02:00
|
|
|
} 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);
|
2021-03-29 14:59:22 +02:00
|
|
|
updateLocale = true;
|
2021-03-29 14:35:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:59:22 +02:00
|
|
|
if(darktheme != localStorage.getItem("bstly.darkTheme")) {
|
|
|
|
localStorage.setItem("bstly.darkTheme", darktheme);
|
|
|
|
updateTheme = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(updateLocale || updateTheme) {
|
|
|
|
window.location.reload();
|
2021-03-29 14:35:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-02 08:29:52 +01:00
|
|
|
return true;
|
2021-03-26 10:17:28 +01:00
|
|
|
}).catch(function(error) {
|
2021-04-16 08:57:53 +02:00
|
|
|
return that.router.parseUrl('/unavailable?target=' + state.url);
|
2020-11-02 08:29:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AnonymousGuard implements CanActivate {
|
2021-03-26 10:17:28 +01:00
|
|
|
constructor(private authService: AuthService, private router: Router) {}
|
2020-11-02 08:29:52 +01:00
|
|
|
|
2021-03-26 10:17:28 +01:00
|
|
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
2020-11-02 08:29:52 +01:00
|
|
|
const that = this;
|
|
|
|
return this.authService.getAuth().then((data: any) => {
|
2021-03-26 10:17:28 +01:00
|
|
|
if(data.authenticated) {
|
2020-11-02 08:29:52 +01:00
|
|
|
this.router.navigateByUrl('/account/info');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-03-26 10:17:28 +01:00
|
|
|
}).catch(function(error) {
|
2021-04-16 08:57:53 +02:00
|
|
|
return that.router.parseUrl('/unavailable?target=' + state.url);
|
2020-11-02 08:29:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|