daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest May 16th, 2018 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
  3. import { Observable } from 'rxjs/Observable';
  4. import { AngularFireAuth } from 'angularfire2/auth';
  5. import 'rxjs/add/operator/do';
  6. import 'rxjs/add/operator/map';
  7. import 'rxjs/add/operator/take';
  8.  
  9.  
  10. @Injectable()
  11. export class AuthGuard implements CanActivate {
  12.   constructor(private afAuth: AngularFireAuth, private router: Router, private) {}
  13.  
  14.   canActivate(
  15.     next: ActivatedRouteSnapshot,
  16.     state: RouterStateSnapshot): Observable<boolean> {
  17.  
  18.       return this.afAuth.authState
  19.        .take(1)
  20.        .map(user => {
  21.          return !!user
  22.        })
  23.        .do(loggedIn => {
  24.          if (!loggedIn) {
  25.            console.log("access denied")
  26.            this.router.navigate(['/login']);
  27.          }
  28.      })
  29.    }
  30. }
RAW Paste Data
Top