Angular Guards Collection

JS
S
JavaScript

Collection of Angular Guards

1// ========================================================================
2@Injectable()
3export class AuthGuard implements CanActivate {
4  constructor(private authService: AuthService) {}
5
6  public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
7    return this.authService.currentUser$.pipe(
8      flatMap(user => {
9        if (user) {
10          return of(true)
11        } else {
12          this.authService.setRedirectAfterLoginIfNoneExists(state.url)
13          return of(false)
14        }
15      })
16    )
17  }
18}
19
20// ========================================================================
21@Component()
22export class ContactsDetailComponent implements OnInit {
23
24  contact;
25
26  constructor(private route: ActivatedRoute) {}
27
28  ngOnInit() {
29    this.contact = this.route.snapshot.data['contact'];
30  }
31}
32
33// ========================================================================
34{
35  path: 'items/:date',
36  component: ItemsComponent,
37  resolve: { items: APIResolver }
38}

Created on 7/2/2019