With the code below, I am getting the error Property 'subscribe' does not exist on type Auth
. If I remove the entire block of subscribe code from app.component.ts then I am getting no error, but the browser is opening directly on the home page and not the login page. At the bottom of app.component.html I have included <router-outlet></router-outlet>
,
app.component.ts
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
import { environment } from '../environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private isLoggedIn: Boolean;
private user_displayName: String;
private user_email: String;
constructor(private router: Router, public afAuth: AngularFireAuth) {
this.afAuth.auth.subscribe(
(auth) => {
if (auth == null) {
console.log("Logged out");
this.isLoggedIn = false;
this.user_displayName = '';
this.user_email = '';
this.router.navigate(['login']);
} else {
this.isLoggedIn = true;
this.user_displayName = auth.google.displayName;
this.user_email = auth.google.email;
console.log("Logged in");
console.log(auth);
this.router.navigate(['']);
}
}
);
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { LoginPageComponent } from './login-page/login-page.component';
import { HomePageComponent } from './home-page/home-page.component';
import { Observable } from 'rxjs/Observable';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
import { environment } from '../environments/environment';
const routes: Routes = [
{ path: '', component: HomePageComponent },
{ path: 'login', component: LoginPageComponent }
];
@NgModule({
declarations: [
AppComponent,
LoginPageComponent,
HomePageComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(routes),
AngularFireModule.initializeApp(environment.firebase, 'app-root'),
AngularFireDatabaseModule,
AngularFireAuthModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
login-page.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgModule } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
import { environment } from 'environments/environment';
import * as firebase from 'firebase/app';
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.css']
})
export class LoginPageComponent implements OnInit {
constructor(private router:Router, public afAuth: AngularFireAuth) { }
ngOnInit() {
}
login() {
this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}
}
homepage.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgModule } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
import { environment } from 'environments/environment';
@Component({
selector: 'app-home-page',
templateUrl: './home-page.component.html',
styleUrls: ['./home-page.component.css']
})
export class HomePageComponent implements OnInit {
constructor(private router: Router, public afAuth: AngularFireAuth) {}
ngOnInit() {
}
logout() {
this.afAuth.auth.signOut();
}
}
environment.ts
export const environment = {
production: false,
firebase: {
apiKey: "....",
authDomain: "....",
databaseURL: ".......",
projectId: "",
storageBucket: ".........",
messagingSenderId: "........"
}
};
I want the app to function like where I can first login and then on being successful I will be lead to the homepage. But the subscribe is creating a problem.
What can I use instead of that?
Any help would be greatly appreciated.