we_bstly-web/src/app/auth/auth.guard.ts

101 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-09-08 11:09:26 +02:00
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';
2020-11-02 08:29:52 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthUpdateGuard implements CanActivate {
2021-09-08 11:09:26 +02:00
constructor(private authService: AuthService) { }
2020-11-02 08:29:52 +01:00
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
2021-09-08 11:09:26 +02: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-09-08 11:09:26 +02: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-09-08 11:09:26 +02:00
}).catch(function (error) {
2021-10-06 12:11:52 +02:00
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { skipLocationChange: true });
2020-11-02 08:29:52 +01:00
});
}
}
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard implements CanActivate {
2021-09-08 11:09:26 +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-09-08 11:09:26 +02:00
if (!data.authenticated) {
2021-09-23 11:04:12 +02:00
return that.router.navigateByUrl(that.router.parseUrl('/login?target=' + encodeURIComponent(state.url)), { skipLocationChange: true, replaceUrl: true });
2020-11-02 08:29:52 +01:00
}
2021-09-08 11:09:26 +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-09-08 11:09:26 +02:00
for (let profileField of profileFields) {
if (profileField.name == "darkTheme") {
2021-03-29 14:59:22 +02:00
darktheme = profileField.value;
2021-09-08 11:09:26 +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-09-08 11:09:26 +02:00
if (darktheme != localStorage.getItem("bstly.darkTheme")) {
2021-03-29 14:59:22 +02:00
localStorage.setItem("bstly.darkTheme", darktheme);
updateTheme = true;
}
2021-09-08 11:09:26 +02:00
if (updateLocale || updateTheme) {
window.location.reload();
}
})
2020-11-02 08:29:52 +01:00
return true;
2021-09-08 11:09:26 +02:00
}).catch(function (error) {
2021-10-06 12:11:52 +02:00
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { skipLocationChange: true });
2020-11-02 08:29:52 +01:00
});
}
}
@Injectable({
providedIn: 'root'
})
export class AnonymousGuard implements CanActivate {
2021-09-08 11:09:26 +02: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-09-08 11:09:26 +02:00
if (data.authenticated) {
2020-11-02 08:29:52 +01:00
this.router.navigateByUrl('/account/info');
return false;
}
return true;
2021-09-08 11:09:26 +02:00
}).catch(function (error) {
2021-10-06 12:11:52 +02:00
return that.router.navigateByUrl(that.router.parseUrl('/unavailable?target=' + encodeURIComponent(state.url)), { replaceUrl: true });
2020-11-02 08:29:52 +01:00
});
}
}