45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
|
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';
|
||
|
import { Subscription } from 'rxjs';
|
||
|
|
||
|
@Injectable()
|
||
|
|
||
|
export class AuthGuardService implements CanActivate {
|
||
|
private UserSub: Subscription = new Subscription();
|
||
|
private addr = '';
|
||
|
constructor(
|
||
|
private router: Router,
|
||
|
private http: HttpClient,
|
||
|
public userService: UserService
|
||
|
){}
|
||
|
|
||
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||
|
const token = localStorage.getItem('s4z_token');
|
||
|
|
||
|
if(token != null){
|
||
|
this.userService.getUser(token);
|
||
|
this.UserSub = this.userService.getZaddrUpdateListener().
|
||
|
subscribe((addr) => {
|
||
|
if (addr != null) {
|
||
|
console.log(addr);
|
||
|
this.addr = addr;
|
||
|
} else {
|
||
|
console.log("No record for that token");
|
||
|
}
|
||
|
});
|
||
|
this.UserSub.unsubscribe();
|
||
|
if (this.addr != null) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
|
console.log("Not logged in");
|
||
|
this.router.navigate(['/login']);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|