2

I am trying to write to Firebase from android. I only want authenticated users to be able to write to the database. How do I do this?

Here is my registering fragment that attempts to write to the database:

public class register_fragment extends Fragment {

    public static final String FIREBASE_URL = "my_apps_url";

    private FirebaseAuth mAuth;
    private Firebase mRootRef;

    public String studentIdString, emailString, passwordString;

    Snackbar mSnackbar;


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

        View view = inflater.inflate(R.layout.register_layout, container, false);
        Button mRegister = (Button) view.findViewById(R.id.green_register_button);
        final EditText mStudentId = (EditText) view.findViewById(R.id.student_id);
        final EditText mEmail = (EditText) view.findViewById(R.id.register_email);
        final EditText mPassword = (EditText) view.findViewById(R.id.register_password);

        mAuth = FirebaseAuth.getInstance();
        mRootRef = new Firebase(FIREBASE_URL);

        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                studentIdString = mStudentId.getText().toString();
                emailString = mEmail.getText().toString();
                passwordString = mPassword.getText().toString();
                registerUser(studentIdString,emailString,passwordString);
            }
        });
        return view;
    }

    @Override
    public void onStart(){
        super.onStart();
        //Check if user is logged in
        FirebaseUser currentUser = mAuth.getCurrentUser();
    }

    private void registerUser(String id, String email, String password){

        mAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(!task.isSuccessful()){
                            mSnackbar = Snackbar.make(getView(),"Error: "+task.getException(),Snackbar.LENGTH_SHORT);
                            mSnackbar.show();

                        }else{
                            mSnackbar = Snackbar.make(getView(),"Successfully Registered",Snackbar.LENGTH_SHORT);
                            mSnackbar.show();
                            registerUser(studentIdString,emailString);//call method to write to Firebase
                            Intent intent = new Intent(getActivity(), ChapelInProgressActivity.class);
                            intent.putExtra("user",user.getEmail());
                            startActivity(intent);
                        }
                    }
                });
    }

    private void registerUser(String id, String email){

        Map<String, Object> updates = new HashMap<>();
        updates.put("id",id);
        updates.put("email",email);
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        mRootRef.child(user.getUid()).updateChildren(updates);//trying to write to Firebase, but does not work
    }

}

I would like the rules for the Firebase database to be:

 ".read": "auth != null",
 ".write": "auth != null"

So only authenticated users can read and write

2

1 Answer 1

2

If you want to use these rules, then you have to edit the rules section in firebase console.

However, if you want to check the authentication state in your app, you can use the AuthStateListener, more information here:

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.