src/app/providers/guards/payment-required.guard.ts
Methods |
constructor(router: Router, app: AppStateStore)
|
|||||||||
Parameters :
|
canActivate |
canActivate()
|
Returns :
Observable<boolean>
|
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators/map';
import { AppStateStore } from '../stores/app-state.store';
@Injectable()
export class PaymentRequiredGuard implements CanActivate {
constructor(private router: Router, private app: AppStateStore) {}
canActivate(): Observable<boolean> {
return this.app.state$.pipe(
map(data => data.payment),
map(payment => {
if (payment) {
console.log('passed payment required guard');
return true;
}
console.log('failed payment required guard');
// no payment (either in a bad state or user refreshed)
this.router.navigate(['/start']);
return false;
})
);
}
}