【问题标题】:React Native authentication and cloud Firestore for web apps用于 Web 应用的 React Native 身份验证和云 Firestore
【发布时间】:2021-10-31 13:07:25
【问题描述】:

我是 Firebase 和 React 的新手,所以请多多包涵。 我正在尝试在我的应用程序中使用云 Firestore 和身份验证,但有点不确定如何设置我的 firebase.js 文件。我已经扩展了互联网并观看了大量视频/教程,但似乎无法弄清楚如何在我的应用程序中实现这些。我从 2020 年开始看的一些教程似乎也没有过时。

这是我的代码:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

// import { getAuth, signInWithCustomToken } from "firebase/auth";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_FIREBASE_APP_ID
};

// const auth = getAuth();
// signInWithCustomToken(auth, token)
//   .then((userCredential) => {
//     // Signed in
//     const user = userCredential.user;
//     // ...
//   })
//   .catch((error) => {
//     const errorCode = error.code;
//     const errorMessage = error.message;
//     // ...
//   });


const app = initializeApp(firebaseConfig);
const db = getFirestore(app)
// export const auth = app.auth()
export { db } 

【问题讨论】:

  • 我们只能看到您正在初始化 Firebase。您能否分享您尝试使用但遇到问题的任何组件?
  • 再次感谢您的帮助,Dharmaraj!我想我在尝试初始化 Firebase 以进行身份​​验证和云 Firestore 时遇到了困难。我不确定如何设置它
  • 控制台中一定有一些错误或警告。可以分享一下截图吗?
  • 我有云防火墙工作,现在我想为用户添加登录/注销身份验证,但不确定如何设置身份验证初始化?
  • 我还没有到那里测试身份验证

标签: reactjs firebase google-cloud-firestore firebase-authentication


【解决方案1】:

您可以从firebase.js 导出身份验证实例,然后在您需要的任何组件中使用它。假设你有一个登录组件:

firebase.js:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth"

const app = initializeApp(firebaseConfig);

const db = getFirestore(app)
const auth = getAuth(app)

export { db, auth } 

login.jsx:

// login component

import { auth } from "./firebase.js"
import { signInWithEmailAndPassword } from "firebase/auth"

// run on button click or relevant events
signInWithEmailAndPassword(auth, email, password).then((user) => {
  console.log(user)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2019-03-25
    相关资源
    最近更新 更多