简体   繁体   English

使用 Firebase 匿名身份验证,错误,JSON 无效

[英]Using Firebase Anonymous Authentication, error, invalid JSON

Recently when trying to sign in anonymously with Firebase I get the following error最近尝试使用 Firebase 匿名登录时出现以下错误

{"error":{"code":400,"message":"ADMIN_ONLY_OPERATION","errors":[{"message":"ADMIN_ONLY_OPERATION","domain":"global","reason":"invalid"}]}} {"error":{"code":400,"message":"ADMIN_ONLY_OPERATION","errors":[{"message":"ADMIN_ONLY_OPERATION","domain":"global","reason":"invalid"} ]}}

The function I am calling is the one provided in the docs我调用的函数是文档中提供的函数

doSignInAnonymously = () => {

   this.auth.signInAnonymously()
}

I don't understand the error code as Error 400 means an Invalid JSON in your request:我不明白错误代码,因为错误 400 表示您的请求中有无效的 JSON:

Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).检查 JSON 消息的格式是否正确并包含有效字段(例如,确保传入正确的数据类型)。

I am using a function provided by the API so I don't know how I could have implemented it wrong, or what exactly it means when it says ADMIN_ONLY_OPERATION , why would signing in anonymously have anything to do with an admin?我正在使用 API 提供的功能,所以我不知道我怎么会错误地实现它,或者当它说ADMIN_ONLY_OPERATION时它到底意味着什么,为什么匿名登录与管理员有任何关系?

Does anyone have any experience with this type of error or can see any obvious errors in my use of the function?有没有人有过此类错误的经验,或者在我使用该功能时可以看到任何明显的错误?

my firebase.js file where I have all my authentication functions.我的 firebase.js 文件,其中包含我所有的身份验证功能。

import app from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'
import * as firebase from 'firebase';

const config = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  databaseURL: process.env.REACT_APP_DATABASE_URL,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
}

class Firebase {
  constructor() {
    app.initializeApp(config);

    /* Helper */

    this.fieldValue = app.firestore.FieldValue;
    this.emailAuthProvider = app.auth.EmailAuthProvider;

    /* Firebase APIs */

    this.auth = app.auth();
    this.db = app.firestore();
  }

  // *** Auth API ***

  // eslint-disable-next-line max-len
  doCreateUserWithEmailAndPassword = (email, password) => this.auth.createUserWithEmailAndPassword(email, password)

  // eslint-disable-next-line max-len
  doSignInWithEmailAndPassword = (email, password) => this.auth.signInWithEmailAndPassword(email, password)

  doSignInAnonymously = () => {

    this.auth.signInAnonymously()
  }

Code at the call site:调用站点的代码:

<Button
        variant="contained"
        color="primary"
        onClick={() => firebase
              .doSignInAnonymously()
              .then(({ user }) => {
                localStorage.setItem(process.env.REACT_APP_LOCAL_STORAGE, JSON.stringify(user))
                history.push(ROUTES.USER)
               })
              .catch(({ message }) => setErrMessage(message))
        }
>
Continue As Guest
</Button>
  • I tried removing the following line as the error is related to a malformed JSON, but this does't seem to help.我尝试删除以下行,因为该错误与格式错误的 JSON 相关,但这似乎无济于事。
localStorage.setItem(process.env.REACT_APP_LOCAL_STORAGE, JSON.stringify(user))

Go to Firebase console and check your Sign-in providers under the Sign-in Methods tab in the Authentication service and make sure Anonymous is enabled.转到Firebase 控制台并在身份验证服务的登录方法选项卡下检查您的登录提供商,并确保启用了匿名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM