zgo/src/app/auth-guard.service.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-10-15 19:14:49 +00:00
import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';
import {HttpClient, HttpParams} from '@angular/common/http';
import { UserService } from './user.service';
2022-01-17 20:49:21 +00:00
import { Subscription, Observable } from 'rxjs';
import { Owner } from './owner.model';
2021-10-15 19:14:49 +00:00
@Injectable()
export class AuthGuardService implements CanActivate {
private UserSub: Subscription = new Subscription();
private addr = '';
2022-01-17 20:49:21 +00:00
private paid = false;
private paidUpdate: Observable<boolean>;
2021-10-15 19:14:49 +00:00
constructor(
private router: Router,
private http: HttpClient,
public userService: UserService
2022-01-17 20:49:21 +00:00
){
this.paidUpdate = this.userService.paidUpdate;
this.paidUpdate.subscribe((indicator) => {
this.paid = indicator;
});
}
2021-10-15 19:14:49 +00:00
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const token = localStorage.getItem('s4z_token');
2022-02-01 18:04:16 +00:00
var path = route.url[0].path;
2021-10-15 19:14:49 +00:00
if(token != null){
this.userService.uZaddrUpdate.
2021-10-15 19:14:49 +00:00
subscribe((addr) => {
if (addr != null) {
console.log(addr);
this.addr = addr;
} else {
console.log("No record for that token");
}
});
2022-02-01 18:04:16 +00:00
if (path === 'biz') {
if (this.addr.length > 0) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
2021-10-15 19:14:49 +00:00
} else {
2022-02-01 18:04:16 +00:00
if (this.addr != null && this.paid) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
2021-10-15 19:14:49 +00:00
}
} else {
console.log("Not logged in");
this.router.navigate(['/login']);
return false;
}
}
}