2021-03-26 10:17:28 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Router} from '@angular/router';
|
|
|
|
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
|
|
|
|
import {AuthService} from '../services/auth.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) {
|
|
|
|
return that.router.parseUrl('/unavailable');
|
2020-11-02 08:29:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthenticatedGuard 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('/login');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-03-26 10:17:28 +01:00
|
|
|
}).catch(function(error) {
|
|
|
|
return that.router.parseUrl('/unavailable');
|
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) {
|
|
|
|
return that.router.parseUrl('/unavailable');
|
2020-11-02 08:29:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|