web-dev-qa-db-ja.com

Firebaseの許可が拒否されました

私はコーディングに比較的慣れておらず、問題を抱えています。

私はfirebaseにデータを送るためにこのコードを持っています

app.userid = app.user.uid

var userRef = app.dataInfo.child(app.users);

var useridRef = userRef.child(app.userid);

useridRef.set({
  locations: "",
  theme: "",
  colorScheme: "",
  food: ""
});

しかし、私はエラーを受け続けます:

緊急警告:/ users /(GoogleID)に設定できませんでした:permission_denied 2016-05-23 22:52:42.707 firebase.js:227キャッチされませんでした(約束あり)エラー:PERMISSION_DENIED:権限が拒否されました(…)

これを調べようとすると、Firebaseのルールについて話します。これは、まだ学習していない言語のようです(または、頭の中で過ぎています)。誰かが問題の原因を説明できますか?私はそれが私がそれに電子メールとユーザー表示名を保存するように頼んでいると思いました、そして、あなたはこれをすることが許されなかった、しかし私がそれらを取り除いたとき私はまだ同じ問題を抱えていました。ルールを設定せずにこのエラーを回避する方法はありますか。それとも、ルールを1日の書き方で自分自身に教えることができるのでしょうか。

助けてくれてありがとう!

84
Robert Prine

デフォルトでは、 新しいFirebase Console のプロジェクト内のデータベースは、認証されたユーザーのみが読み取り/書き込み可能です。

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Firebaseデータベースのセキュリティ規則については クイックスタートを参照してください

コードからユーザーをサインインしていないので、データベースはデータへのアクセスを拒否します。これを解決するには、データベースへの認証されていないアクセスを許可するか、データベースにアクセスする前にユーザーをサインインする必要があります。

データベースへの認証されていないアクセスを許可する

当面の間(チュートリアルが更新されるまで)の最も簡単な回避策は、プロジェクトのコンソールのデータベースパネルに移動し、[ルール]タブを選択して、内容を次のルールに置き換えることです。

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

これにより、データベースのURLを知っている人なら誰でもそのデータベースを読み書きできるようになります。本番環境に入る前に、データベースを確実に再び保護してください。そうしないと、誰かがデータベースを悪用し始める可能性があります。

データベースにアクセスする前にユーザーにサインインする

(もう少し)より時間がかかるがより安全な解決策のために、 Firebase AuthenticationsignIn...メソッドの1つを呼び出して、データベースにアクセスする前にユーザーがサインインしていることを確認します。これをする最も簡単な方法は 匿名認証を使うことです

firebase.auth().signInAnonymously().catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

サインインが検出されたらリスナーを接続します

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var isAnonymous = user.isAnonymous;
    var uid = user.uid;
    var userRef = app.dataInfo.child(app.users);

    var useridRef = userRef.child(app.userid);

    useridRef.set({
      locations: "",
      theme: "",
      colorScheme: "",
      food: ""
    });

  } else {
    // User is signed out.
    // ...
  }
  // ...
});
200

私は同様の問題に直面していました、そして、このエラーがリアルタイムデータベースのために読み書き操作のために設定された不正確な規則が原因であることを知りました。デフォルトでは、Google Firebaseは最近、リアルタイムデータベースではなくクラウドストアを読み込みます。リアルタイムに切り替えて正しいルールを適用する必要があります。

enter image description here

ご覧のとおり、Cloud Firestoreはリアルタイムデータベースではありません。正しいデータベースに切り替えると、ルールの下に適用されます。

{
   "rules": {
       ".read": true,
       ".write": true
     }
 }
WORKING!!
Go to the "Database" option you mentioned.

    1. There on the Blue Header you'll find a dropdown which says Cloud Firestore Beta
    2. Change it to "Realtime database"
    3. Go to Rules and set .write .read both to true

Let me know how it goes.
copied from
https://github.com/firebase/quickstart-js/issues/239
25
lilhamad

データベースのタイトルの横には2つのオプションがあります。

Cloud Firestore、リアルタイムデータベース

リアルタイムデータベースを選択してルールに進む

ルールをtrueに変更します。

これで私の問題は解決した。

5
Jaywant Narwade
  1. Firebaseを開き、左側のデータベースを選択します。
  2. 右側のドロップダウンメニューから[Realtime database]を選択し、ルールを次のように変更します。{"rules":{".read":true、 ".write":true}}

できます..!!

もう1つの解決策は、すでに資格情報が手元にある場合は、実際にユーザーを自動的に作成またはログインすることです。これがPlain JSを使ったやり方です。

function loginToFirebase(callback)
{
    let email = '[email protected]';
    let password = 'xxxxxxxxxxxxxx';
    let config =
    {
        apiKey: "xxx",
        authDomain: "xxxxx.firebaseapp.com",
        projectId: "xxx-xxx",
        databaseURL: "https://xxx-xxx.firebaseio.com",
        storageBucket: "gs://xx-xx.appspot.com",
    };

    if (!firebase.apps.length)
    {
        firebase.initializeApp(config);
    }

    let database = firebase.database();
    let storage = firebase.storage();

    loginFirebaseUser(email, password, callback);
}

function loginFirebaseUser(email, password, callback)
{
    console.log('Logging in Firebase User');

    firebase.auth().signInWithEmailAndPassword(email, password)
        .then(function ()
        {
            if (callback)
            {
                callback();
            }
        })
        .catch(function(login_error)
        {
            let loginErrorCode = login_error.code;
            let loginErrorMessage = login_error.message;

            console.log(loginErrorCode);
            console.log(loginErrorMessage);

            if (loginErrorCode === 'auth/user-not-found')
            {
                createFirebaseUser(email, password, callback)
            }
        });
}

function createFirebaseUser(email, password, callback)
{
    console.log('Creating Firebase User');

    firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function ()
        {
            if (callback)
            {
                callback();
            }
        })
        .catch(function(create_error)
        {
            let createErrorCode = create_error.code;
            let createErrorMessage = create_error.message;

            console.log(createErrorCode);
            console.log(createErrorMessage);
        });
}
1
Parampal Pooni