Getting permission denied errors when trying to setValue() using Firebase Realtime Database

Viewed 2888

I am working on a project using firebase where I wish to store basic information about a job. I create the job object and have followed what I believe to be the correct steps to store it into the database, but I keep getting the error(specifically the last line):

D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
I/AssistStructure: Flattened final assist data: 4736 bytes, containing 1 windows, 25 views
V/FA: Inactivity, disconnecting from the service
D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10151; state: DISABLED
W/RepoOperation: setValue at /Job/-MEVMjcPp1ryvkEUQ4Fx failed: DatabaseError: Permission denied

The java file where I am attempting to write to the database(AddFragment.java):

package com.example.android.portfolio;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.android.portfolio.helpers.Job;
import com.example.android.portfolio.helpers.Validation;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class AddFragment extends Fragment {
    //Database Reference
    DatabaseReference ref;

    //Create validation object
    Validation validation = new Validation();

    //Create Job object to pass into database
    Job job;

    //Component object creation
    EditText editTextCompanyName;
    EditText editTextJobName;
    EditText editTextWebsite;
    EditText editTextContactName;
    EditText editTextContactEmail;
    EditText editTextApplicationLink;
    Spinner dropdown;
    Button buttonAdd;
    String[] items = new String[]{"One Week", "Two Weeks", "One Month", "Never"};

    String companyName, jobName, website, contactName, contactEmail, applicationLink;
    int dropdownChoice;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add, container, false);




        editTextCompanyName = (EditText) view.findViewById(R.id.editTextCompanyName);
        editTextJobName = (EditText) view.findViewById(R.id.editTextJobName);
        editTextWebsite = (EditText) view.findViewById(R.id.editTextWebsite);
        editTextContactName = (EditText) view.findViewById(R.id.editTextContactName);
        editTextContactEmail = (EditText) view.findViewById(R.id.editTextContactEmail);
        editTextApplicationLink = (EditText) view.findViewById(R.id.editTextApplicationLink);
        dropdown = (Spinner) view.findViewById(R.id.spinnerRemind);
        buttonAdd = (Button) view.findViewById(R.id.buttonAdd);

        //Create the spinner and set values to it using the above array. Warnings here are for possible null values
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);

        ref = FirebaseDatabase.getInstance().getReference().child("Job");

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Get data from GUI objects and put it into variables
                companyName = editTextCompanyName.getText().toString().trim();
                jobName = editTextJobName.getText().toString().trim();
                website = editTextWebsite.getText().toString().trim();
                contactName = editTextContactName.getText().toString().trim();
                contactEmail = editTextContactEmail.getText().toString().trim();
                applicationLink = editTextApplicationLink.getText().toString().trim();
                dropdownChoice = dropdown.getSelectedItemPosition();

                //Job object initialization
                job = new Job(companyName, jobName, website, contactName, contactEmail, applicationLink, dropdownChoice);

                //Push values to database
                ref.push().setValue(job);
                Toast.makeText(AddFragment.this.getActivity(), "Data inserted to database", Toast.LENGTH_LONG);

            }
        });


        return view;
    }
}

The current rules in my firebase setup: Firebase Rules Screenshot

So far I have tried to:

  1. Change the rules configuration from true to, "auth != null", "auth == null"
  2. Add a child value to the database push.
  3. Use a different Emulator.

It is my first time using firebase so I am trying to understand using NoSQL databases, but I don't understand which part of the connection process I am missing as based on my rules file there should be no access being denied to write.

0 Answers
Related