Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Firebase Anonymous Authentication, error, invalid JSON

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))
like image 776
Steve2056726 Avatar asked Apr 29 '19 05:04

Steve2056726


People also ask

What is firebaseauthinvaliduserexception?

In the event of an authentication failure, the Firebase SDK will throw one the following types of exception: • FirebaseAuthInvalidUserException – This exception indicates a problem with the email address entered by the user. For example, the account does not exist or has been disabled in the Firebase console.

How do I authenticate to Firebase anonymously?

Authenticate with Firebase anonymously. When a signed-out user uses an app feature that requires authentication with Firebase, sign in the user anonymously by completing the following steps: Call the signInAnonymously method: firebase.auth().signInAnonymously().catch(function(error) { // Handle Errors here.

How do I disable a user account in Firebase?

Next, open the Firebase console in a browser window and navigate to the Users screen of the Authentication section for the Firebase Examples project. Using the menu to the right of one of the existing user account entries, select the Disable Account menu option as outlined in Figure 12-1:

Why is my user’s token invalid in Firebase?

The user has either entered the wrong email address, has yet to create an account or the account has been deleted. • ERROR_USER_TOKEN_EXPIRED – The user’s token has been invalidated by the Firebase system.


1 Answers

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.

like image 128
Tal Zion Avatar answered Oct 22 '22 19:10

Tal Zion