firestore vue.js:auth()不是函数

时间:2018-07-29 18:39:18

标签: firebase vuejs2 firebase-authentication vuefire

我有一个像这样的firebase配置文件:

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;

我正在尝试对用户进行身份验证,但一直在获取firebaseApp.auth()不是一个函数。

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
          });
      });

我不必配置该应用程序吗? @firebase中没有身份验证功能。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您应该在初始化中添加auth Firebase服务,如下所示:

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;

然后,在组件中执行以下操作:

auth.onAuthStateChanged(user => {})

auth.signInWithEmailAndPassword(email, pw)