Created
August 8, 2017 19:22
-
-
Save shafiqshams/253b2252ccd92b2dc35417984c937318 to your computer and use it in GitHub Desktop.
Redirect the user to main page if it's not authenticated, works even after logout. - [Angular 2 & Firebase App [Part 4] - Firebase Authentication]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { AngularFireAuth } from 'angularfire2'; | |
import { Router } from '@angular/router'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent implements OnInit { | |
constructor( | |
private auth: AngularFireAuth, | |
private router: Router | |
) {} | |
ngOnInit() { | |
this.auth.subscribe(user => { | |
if (!user) { | |
this.router.navigate(['/']); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment