1

So I have been trying to experiment with Angular and Firebase, and wanted to get the angularfire2 auth system working. I followed some online tutorials and frankensteined this app together. I have a little experience in Angular2 doing a project ionic app a year or so ago but have not worked with Firebase though.

The error is when I ng serve get hit with a blank screen and a console error:

TypeError app.auth is not a function

Seems this must be caused by an incorrect setup of the AngularFireAuthModule or something?

I referred to the https://github.com/angular/angularfire2/blob/master/docs/auth/getting-started.md and it seems correct. Here is a link to the app https://github.com/jburns24/angularfire2-auth-bad-demo.

Seems like the exact issue mentioned in this AngularFire2 TypeError: app.auth is not a function.

1

2 Answers 2

1

You are currently following demo application and I show there are lot of conflicts. But your aim here is to experiment with Angular and Firebase. I'm suggesting you to follow these steps.

Before that create a new Angular project.

eg.

ng new my-app

Now install the angularfire2 and firbase:

npm install angularfire2 firebase --save

Add Firebase config to environments variable:

firebase: {
    apiKey: '<your-key>',
    authDomain: '<your-project-authdomain>',
    databaseURL: '<your-database-URL>',
    projectId: '<your-project-id>',
    storageBucket: '<your-storage-bucket>',
    messagingSenderId: '<your-messaging-sender-id>'
  }

Setup @NgModule for the AngularFireModule and AngularFireAuthModule :

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

In you AppComponent import AngularFireAuth and auth:

import { AngularFireAuth } from 'angularfire2/auth';
import { auth } from 'firebase';

And implement following functions:

export class AppComponent {
  constructor(public afAuth: AngularFireAuth) {
  }
  login() {
    this.afAuth.auth.signInWithPopup(new auth.GoogleAuthProvider());
  }
  logout() {
    this.afAuth.auth.signOut();
  }
}

HTML:

<div *ngIf="afAuth.user | async as user; else showLogin">
  <h1>Hello {{ user.displayName }}!</h1>
  <button (click)="logout()">Logout</button>
</div>
<ng-template #showLogin>
  <p>Please login.</p>
  <button (click)="login()">Login with Google</button>
</ng-template>

These steps are same as their DOC. Please refer bellow links:

Hope this will help you!

Sign up to request clarification or add additional context in comments.

4 Comments

made the suggested changes and I am still getting the same console error.
Looked at the projects github and the release history they introduced a breaking change where you need to use auth from firebase now not import */firbase. github.com/angular/angularfire2/releases/tag/5.0.0-rc.10
@Buster it's better to follow the updated answer steps
Thanks I should have tried taking back to bare minimum thanks for your help.
1

If anyone else comes across this issue, I eventually fixed it by deleting everything out of my node modules folder, deleting the package lock file and the running npm install.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.