0

I've been able to login/sign in using Firebase authentication service and add/delete items in Firebase database using React Native app.But the problem is that I need to display only the items of one user already logged in not all the available data in the database.And I managed also to get User Id already connected in the session. Here is my Code:... I think I need to use Filters in my list view but I'm not sure about it

constructor(props) {
        super(props);
        this.tasksRef = this.props.firebaseApp.database().ref("/item");
        const dataSource = new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
        });
        this.state = {
            uid: " ",
            user:null,
            loading: true,
            dataSource: dataSource,
            newTask: 
        };
    }  
    componentDidMount() {
        this.listenForTasks(this.tasksRef);  
    }

    listenForTasks(tasksRef) {
        tasksRef.on('value', (dataSnapshot) => {
            var tasks = [];
            dataSnapshot.forEach((child) => {
                tasks.push({
                    name: child.val().name,
                    type: child.val().type,
                    id: child.val().id,
                    _key: child.key
                });
            });
                this.setState({
                dataSource: this.state.dataSource.cloneWithRows(tasks)
            });
        });
    }

    componentWillMount() {
        const userData = this.props.firebaseApp.auth().currentUser;
          this.setState({
            user: userData,
            uid: userData.uid,
            loading: false
        });
    }
    _addTask() {
        if (this.state.newTask === "") {
            return;
        }
        if (this.state.newTask && this.state.uid) {
            this.tasksRef.push({
                name: this.state.newTask,
                type: this.state.newType,
                id: this.state.uid,
            });
        }

        this.setState({newTask: ""});
        this.setState({newType: ""});
        this.setState({uid: ""});
        alert("Task added successfully");
    }

    _renderItem(task) {
        const onTaskCompletion = () => {
            this.tasksRef.child(task._key).remove()
        };
        return (
            <ListItem task={task} onTaskCompletion={onTaskCompletion} />
        );
    }

    render() {
        return (
            <View style={styles.container}>
                <StatusBar title="to do List"/>                    
                <View style={accountStyles.email_container}>
                    <Text style={accountStyles.email_text}>{this.state.user.email}</Text>
                    <TouchableHighlight onPress={this.logout.bind(this)} style={styles.primaryButton}>
                        <Text style={styles.primaryButtonText}>Logout</Text>
                    </TouchableHighlight>
                </View>
                <ListView
                    dataSource={this.state.dataSource}
                    enableEmptySections={true}
                    renderRow={this._renderItem.bind(this)}
                    style={styles.listView}/>
                <TextInput
                    value={this.state.newTask}
                    style={styles.textEdit}
                    onChangeText={(text) => this.setState({newTask: text})}
                    placeholder="New Task"
                />
                <TextInput
                    value={this.state.newType}
                    style={styles.textEdit}
                    onChangeText={(text1) => this.setState({newType: text1})}
                    placeholder="New Type"
                />
                <FloatingActionButton
                    hideShadow={true}
                    buttonColor="rgba(231,76,60,1)"
                    onPress={this._addTask.bind(this)}/>
            </View>
        );
    }

    logout() {
        this.props.firebaseApp.auth().signOut().then(() => {
            this.props.navigator.push({
                component: Login
            });
        });
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3521011
  • 1,481
  • 5
  • 20
  • 35

1 Answers1

0

https://www.firebase.com/docs/security/guide/index.html you need to set user specific rules in firebase. That will make your query only send relevant information for the specific user.

When I had this same question I did what this guy says in the first response FireBase - how to list user-specific data?

"I store the records by group ids or by user ids:"

@user3521011 heres my code example:

    static setUserName(userId, userName) {

    let userNamePath = "/user/" + userId + "/details";

    return firebase.database().ref(userNamePath).update({
        userName: userName
    })

}
Community
  • 1
  • 1
Adam Clark
  • 61
  • 5
  • thank you.. can you post your code please so I can better understand – user3521011 Feb 28 '17 at 17:05
  • thank you for your help.. So just I changed this line in the constructor : ` this.tasksRef = this.props.firebaseApp.database().ref("/users/" + currentUID + "/items");` and the rules in the Firebase : { ` "rules": { "users": { "$user_id":{" .read": "data.child('user_id').val() == auth.id", ".write": "(data.child('user_id').val() == auth.id) || (newData.child('user_id').val() == auth.id)"} } } }` Follow these links for more details : https://www.firebase.com/docs/web/guide/retrieving-data.html https://www.firebase.com/docs/security/ – user3521011 Mar 01 '17 at 15:45