1

I have a firebase config file like this:

import { firebase } from "@firebase/app";
import "@firebase/firestore";

const firebaseApp = firebase.initializeApp({
    apiKey: "myKey",
    authDomain: "myDomain",
    databaseURL: "myURL",
    projectId: "myProject",
    storageBucket: "myBucket",
    messagingSenderId: "999999999"
});

export const db = firebaseApp.firestore();
export const firebaseApp = firebaseApp;

I am trying to authenticate a user but keep getting firebaseApp.auth() is not a function.

let me = db.collection('staff').where('email', '==', this.current_user.email)
    .get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
           this.fbUser = doc.id;
           let email = doc.data().email;
           let pw = doc.data().key;

           firebaseApp.auth().onAuthStateChanged(user => {
              if (user) {
                 //console.log('Already authenticated.');
              } else {
                 firebaseApp.auth().signInWithEmailAndPassword(email, pw)
                    .then(liu => {
                        //console.log('Logged in', liu.uid);
                        let uid = liu.uid;
                        this.$localStorage.set('fbId',this.fbUser, 0);
                        this.$localStorage.set('fbAuthId', uid, 0);
                        me.update({
                           is_active: true
                        });
                  });
              } // end if
          });
      });

Don't I have to configure the app? There's no auth functionality in @firebase.

Any help is appreciated.

1
  • I believe auth() is a method of your firebase module rather than your app instance. Have you tried accessing it via firebase.auth()? Commented Jul 29, 2018 at 18:50

1 Answer 1

5

You should add the auth Firebase service in your initialization, as follows:

import { firebase } from "@firebase/app";
import "@firebase/firestore";
import "@firebase/auth";  // <- NEW

const firebaseApp = firebase.initializeApp({
    apiKey: "myKey",
    authDomain: "myDomain",
    databaseURL: "myURL",
    projectId: "myProject",
    storageBucket: "myBucket",
    messagingSenderId: "999999999"
});

export const db = firebaseApp.firestore();
export const auth = firebaseApp.auth();  // <- NEW
export const firebaseApp = firebaseApp;

Then, in your component, you do:

auth.onAuthStateChanged(user => {})

and

auth.signInWithEmailAndPassword(email, pw)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.