I am trying to build an Angular2 app with firebase authentication, but there are version errors.
The tutorial I followed used an older version of AngularFire. Now since the versions don't match I am getting errors like AuthMethods not exported, AuthProviders not exported. I figured I have to make changes on the calls, but where and and what should be replaced I have no idea. I am pasting the code, any help would be greatly appreciated.
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 { AngularFireModule} from 'angularfire2';
import { AppComponent } from './app.component'; import { AuthService } from './providers/auth.service'; import { LoginPageComponent } from './login-page/login-page.component'; import { HomePageComponent } from './homepage/homepage.component';
//for new version import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Observable } from 'rxjs/Observable'; import * as firebase from 'firebase/app'; import { AuthProviders, AuthMethods } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';
export const firebaseConfig = { apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messagingSenderId: "" };
const routes: Routes = [ { path: '', component: HomePageComponent }, { path: 'login', component: LoginPageComponent } ];
@NgModule({ declarations: [ AppComponent, LoginPageComponent, HomePageComponent ], imports: [ BrowserModule, FormsModule, HttpModule, AngularFireModule.initializeApp(firebaseConfig), RouterModule.forRoot(routes), AngularFireDatabaseModule, AngularFireAuthModule ], providers: [AuthService], bootstrap: [AppComponent] }) export class AppModule { }`
app.component.ts
`import { Component } from '@angular/core'; import { Router } from '@angular/router';
import { AuthService } from './providers/auth.service';
//for new version import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Observable } from 'rxjs/Observable'; import * as firebase from 'firebase/app'; import { AuthProviders, AuthMethods } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';
@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(public authService: AuthService, private router: Router) { //this.authService.af.auth.subscribe(
//for new version
this.afAuth.authState.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(['']);
}
}
);
} }`
auth.service.ts
`import { Injectable } from '@angular/core';
//import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2';
//for new version import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Observable } from 'rxjs/Observable'; import * as firebase from 'firebase/app'; import { AuthProviders, AuthMethods } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';
@Injectable() export class AuthService {
//constructor(public af: AngularFire) { }
//for new version constructor(public afAuth: AngularFireAuth) { }
loginWithGoogle() { //return this.af.auth.login({
//for new version
return this.afAuth.authState.login({
provider: AuthProviders.Google,
method: AuthMethods.Popup
});
}
logout() { //return this.af.auth.logout();
//for new version
return this.afAuth.authState.logout();
} }`
login-page.component.ts
`import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router';
import { AuthService } from '../providers/auth.service';
//for new version import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Observable } from 'rxjs/Observable'; import * as firebase from 'firebase/app'; import { AuthProviders, AuthMethods } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';
@Component({ selector: 'app-login-page', templateUrl: './login-page.component.html', styleUrls: ['./login-page.component.css'] }) export class LoginPageComponent implements OnInit {
constructor(public authService: AuthService, private router:Router) { }
ngOnInit() { }
login() { this.authService.loginWithGoogle().then((data) => { this.router.navigate(['']); }) }
}`
homepage.component.ts
`import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { AuthService } from '../providers/auth.service';
//for new version import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Observable } from 'rxjs/Observable'; import * as firebase from 'firebase/app'; import { AuthProviders, AuthMethods } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';
@Component({ selector: 'app-home-page', templateUrl: './homepage.component.html', styleUrls: ['./homepage.component.css'] }) export class HomePageComponent implements OnInit {
constructor(private authService: AuthService, private router: Router) {
}
ngOnInit() { }
logout() { this.authService.logout(); this.router.navigate(['login']); }
}`
The errors are: Property 'afAuth' does not exist on type 'AppComponent'. no exported member 'AuthMethods'. no exported member 'AuthProviders' Property 'afAuth' does not exist on type 'AppComponent'.
See our 4.0 migration guide
There are no longer AuthProviders, you should use the Firebase JS SDK directly. https://firebase.google.com/docs/auth/web/start and stuff like this.afAuth.authState.logout() should be this.afAuth.auth.logout(), as .auth returns an initialized firebase.auth.Auth instance.
I'd recommend using an editor that picks up our typings, will make your life much easier.