### File

`src/app/core/guards/auth.guard.ts`

### Constructor

`constructor(store: Store, router: Router)`
Defined in src/app/core/guards/auth.guard.ts:13

### Methods

canActivate
`canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)`
Defined in src/app/core/guards/auth.guard.ts:21
Returns: `void`

ngOnDestroy
`ngOnDestroy()`
Defined in src/app/core/guards/auth.guard.ts:37
Returns: `void`

### Properties

isAuthenticated
`isAuthenticated: `boolean``
Defined in src/app/core/guards/auth.guard.ts:12

subscription
`subscription: `Subscription``
Defined in src/app/core/guards/auth.guard.ts:13

    import { Subscription } from 'rxjs/Subscription';
    import { Observable } from 'rxjs/Observable';
    import { Injectable, OnDestroy } from '@angular/core';
    import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    import { Store } from '@ngrx/store';
    import { AppState } from '../../interfaces';
    import { getAuthStatus } from '../../auth/reducers/selectors';

    @Injectable()
    export class CanActivateViaAuthGuard implements CanActivate, OnDestroy{
      isAuthenticated: boolean;
      subscription: Subscription;

      constructor(
        private store: Store<AppState>,
        private router: Router
      ) {
      }

      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        this.subscription = this.store
          .select(getAuthStatus)
          .subscribe(isAuthenticated => {
            this.isAuthenticated = isAuthenticated;
            if (!isAuthenticated) {
              this.router.navigate(
                ['/auth/login'],
                { queryParams: { returnUrl: state.url }}
              );
            }
          });

        return this.isAuthenticated;
      }

      ngOnDestroy() {
        this.subscription.unsubscribe();
      }

    }

# results matching ""

# No results matching ""