javascript - auth.onAuthStateChanged 在 vue3 和 firebase 9.0.0 beta.3 上不起作用

标签 javascript firebase firebase-authentication vuejs3

** 更新 ** 我让它与以下一起工作 **main.js **


import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { onAuthStateChanged} from "firebase/auth";
import { auth } from './firebase'

let app
onAuthStateChanged(auth, (user) => {
  if (!app) {
    app = createApp(App).use(store).use(router).mount('#app')
  }

  if (user) {
    //store.dispatch('fetchUserProfile', user)
  }
})

** firebase.js **

// Firebase App (the core Firebase SDK) is always required and must be listed first
import { initializeApp } from 'firebase/app';

// Add the Firebase products that you want to use
import { getAuth } from "firebase/auth";
import { getFirestore, collection } from 'firebase/firestore';

const firebaseConfig = {
  // config settings
};
const firebaseApp = initializeApp(firebaseConfig);
// utils
const db = getFirestore();
const auth = getAuth(firebaseApp)
// collection references
const usersCollection = collection(db, "users")
const postsCollection = collection(db, "posts")
const commentsCollection = collection(db, "comments")
const likesCollection = collection(db, "likes")
export {
  db,
  auth,
  usersCollection,
  postsCollection,
  commentsCollection,
  likesCollection
};

** 当创建 vue3 和 npm run 服务时它工作。**

但是当将 auth.onAuthStateChanged 添加到 main.js 和 npm run serve 时,它​​会显示空白页面。 我已经尝试了 auth.auth().onAuthStateChanged 没用

** main.js **

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { auth } from './firebase'

let app
auth.onAuthStateChanged(user => {
  if (!app) {
    app = createApp(App).use(store).use(router).mount('#app')
  }

  if (user) {
    //store.dispatch('fetchUserProfile', user)
  }
})

** firebase.js **

/* eslint-disable */
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';


const firebaseConfig = {
  //... config
};
firebase.initializeApp(firebaseConfig);

// utils
const db = firebase.firestore();
const auth = firebase.auth();
const authProvider = firebase.auth;

// collection references
const usersCollection = db.collection('users');
const postsCollection = db.collection('posts');
const commentsCollection = db.collection('comments');
const likesCollection = db.collection('likes');

// export utils/refs
export {
  db,
  auth,
  authProvider,
  firebase,
  usersCollection,
  postsCollection,
  commentsCollection,
  likesCollection
};

** 我尝试了另一种方法但没有成功 **

** main.js **


import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { auth, authState } from './firebase';

let app
authState(auth, (user) => {
  if (!app) {
    app = createApp(App).use(store).use(router).mount('#app')
  }

  if (user) {
    //store.dispatch('fetchUserProfile', user)
  }
})

** firebase.js**


// Firebase App (the core Firebase SDK) is always required and must be listed first
import { initializeApp } from 'firebase/app';

// Add the Firebase products that you want to use
import { getAuth, onAuthStateChanged, GoogleAuthProvider } from "firebase/auth";
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {
  \\ config
};
const firebaseApp = initializeApp(firebaseConfig);

// utils
const db = getFirestore();
const auth = getAuth();
const authState = onAuthStateChanged();
const googleAuthProvider = GoogleAuthProvider();

// collection references
const usersCollection = db.collection('users');
const postsCollection = db.collection('posts');
const commentsCollection = db.collection('comments');
const likesCollection = db.collection('likes');

// export utils/refs
export {
  db,
  auth,
  authState,
  googleAuthProvider,
  firebaseApp,
  usersCollection,
  postsCollection,
  commentsCollection,
  likesCollection
};

** 另一个问题 ** 像上面这样编码是好的做法吗

谢谢

最佳答案

即使 OP 已经解决了,我也会回答这个问题。

您必须将 firebaseApp 实例传递给 Firebase 9.0 中的 getAuth 函数。这是因为您可能在同一应用中使用不同的 firebase 实例。

示例代码:

import { initializeApp } from "firebase/app";
import { getAuth, onAuthStateChanged } from "firebase/auth";

var firebaseConfig = {
  apiKey: "",
  authDomain: "",
  ...
};

const firebaseApp = initializeApp(firebaseConfig);
const authInstance = getAuth(firebaseApp);

onAuthStateChanged(authInstance, user => console.log(user));

关于javascript - auth.onAuthStateChanged 在 vue3 和 firebase 9.0.0 beta.3 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68070827/

相关文章:

javascript - 使用多个 div 在按钮单击时显示 div

javascript - 将 (SSH) 终端嵌入到某些 HTML5 表示系统(例如,reveal.js)中有什么想法吗?

java - 将数据插入firebase实时数据库后看不到数据

python - 如何在 Pyrebase 和 Flask 中知道用户是否登录?

javascript - JavaScript 数组中的唯一计数,按计数排序

javascript - ReactJS 中的 CSS - 媒体查询和分组选择器

firebase - gradle 在 Android Studio 中使用 flutter 失败

android - 如何使用 recycleradapter 和 firebase 在详细 Activity 中获取位置和显示数据

android - Firebase admin sdk 无法解码 jwt token

ios - 尝试显示身份验证选择器时 FirebaseUI 崩溃