最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Firebase Anonymous Authentication, error, invalid JSON - Stack Overflow

programmeradmin3浏览0评论

Recently when trying to sign in anonymously with Firebase I get the following error

{"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:

Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).

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?

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.

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.
localStorage.setItem(process.env.REACT_APP_LOCAL_STORAGE, JSON.stringify(user))

Recently when trying to sign in anonymously with Firebase I get the following error

{"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:

Check that the JSON message is properly formatted and contains valid fields (for instance, making sure the right data type is passed in).

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?

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.

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.
localStorage.setItem(process.env.REACT_APP_LOCAL_STORAGE, JSON.stringify(user))
Share Improve this question edited Nov 16, 2019 at 6:04 Sofyan Thayf 1,3282 gold badges15 silver badges26 bronze badges asked Apr 29, 2019 at 5:48 Steve2056726Steve2056726 4672 gold badges6 silver badges20 bronze badges 4
  • Can you post more of your authentication code? Specifically, which firebase methods you use? – laenkeio Commented Apr 29, 2019 at 6:39
  • question is updated – Steve2056726 Commented Apr 29, 2019 at 7:26
  • Did you ever figure this out? I'm getting same error – Greg Ennis Commented May 17, 2019 at 13:56
  • 4 Yes, a developer I hired has disabled Anonymous Authentication, so make sure you have this method turned on in the Firebase dashboard – Steve2056726 Commented May 18, 2019 at 16:36
Add a comment  | 

1 Answer 1

Reset to default 33

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论