Skip to content

Instantly share code, notes, and snippets.

@shafiqshams
Created August 8, 2017 19:22
Show Gist options
  • Save shafiqshams/253b2252ccd92b2dc35417984c937318 to your computer and use it in GitHub Desktop.
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]
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