应用未通过Firebase验证。权限被拒绝错误

时间:2017-11-12 14:29:55

标签: ios firebase react-native firebase-realtime-database firebase-authentication

我一直关注此guide,使用React Native从firebase添加和检索项目。如果我将firebase上的规则设置为public,一切正常,但如果我将其设置为以下内容,则会收到拒绝权限错误。

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

我在ios.js中正确添加了所有配置数据。我在这里缺少一些基本步骤吗?

index.ios.js:

// Initialize Firebase
const firebaseConfig = {
  apiKey: 'myapikey',
  authDomain: 'myauthdomain',
  databaseURL: 'https://url.firebaseio.com',
  projectId: 'myProjectId',
  storageBucket: 'projectid.appspot.com',
  messagingSenderId: 'myMessagingSenderId'
};
const firebaseApp = firebase.initializeApp(firebaseConfig);

class MyNewAppreactold extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      })
    };
    this.itemsRef = this.getRef().child('items');
  }

  getRef() {
    return firebaseApp.database().ref();
  }

  listenForItems(itemsRef) {
    itemsRef.on('value', (snap) => {

      // get children as an array
      var items = [];
      snap.forEach((child) => {
        items.push({
          title: child.val().title,
          _key: child.key
        });
      });

      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items)
      });

    });
  }

  // componentWillMount() {
  //       firebase.initializeApp(firebaseConfig);
  //   }

  componentDidMount() {
    this.listenForItems(this.itemsRef);
  }

  render() {
    return (
      <View style={styles.container}>

        <StatusBar title="Grocery List" />

        <ListView
          dataSource={this.state.dataSource}
          renderRow={this._renderItem.bind(this)}
          enableEmptySections={true}
          style={styles.listview}/>

        <ActionButton onPress={this._addItem.bind(this)} title="Add" />

      </View>
    )
  }

  _addItem() {
    AlertIOS.prompt(
      'Add New Item',
      null,
      [
        {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
        {
          text: 'Add',
          onPress: (text) => {
            this.itemsRef.push({ title: text })
          }
        },
      ],
      'plain-text'
    );
  }

  _renderItem(item) {

    const onPress = () => {
      AlertIOS.alert(
        'Complete',
        null,
        [
          {text: 'Complete', onPress: (text) => this.itemsRef.child(item._key).remove()},
          {text: 'Cancel', onPress: (text) => console.log('Cancelled')}
        ]
      );
    };

    return (
      <ListItem item={item} onPress={onPress} />
    );
  }
}

AppRegistry.registerComponent('MyNewAppreactold', () => MyNewAppreactold);

1 个答案:

答案 0 :(得分:0)

您分享的代码不会对用户进行身份验证。由于您的安全规则要求对用户进行身份验证以允许访问数据,因此他们会拒绝您应用的未经身份验证的用户。

要解决此问题,您需要对用户进行身份验证。最简单的方法是sign the user in anonymously

firebase.auth().signInAnonymously();

然后在用户通过身份验证后附加数据库侦听器:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    this.listenForItems(this.itemsRef);
  } else {
    // User is signed out.
    // ...
  }
  // ...
});