eagle
eagle

Reputation: 461

How to get user uid from firebase on android?

I want to save data to firebase. This is my Activity Code :

private Firebase firebaseRef;
Firebase.setAndroidContext(this);

try {
    mUserId = firebaseRef.getAuth().getUid();
} catch (Exception e) {
    //  loginWithMailView();
}
itemsUrl = Constants.FIREBASE_URL + "/Person/" + mUserId +"/Information/";

//Person + null (?) + the other information

and this is for my button code :

buttonSave = (Button) findViewById(R.id.btnKaydolKayit);
buttonSave.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        firebaseRef = new Firebase(Constants.FIREBASE_URL);
        String name = editTextName.getText().toString().trim();
        String surname = editTextSurname.getText().toString().trim();
        String gender = final_result.getText().toString();
        String mail = editTextMail.getText().toString();
        String pass = editTextPass.getText().toString().trim();
        String phone = editTextPhone.getText().toString().trim();
        String trom = final_resultT.getText().toString();
        String bloodGroup = spinnerBlood.getSelectedItem().toString();
        String dateBlood = dateTextViewBlood.getText().toString();
        String dateTrombo = dateTextViewTrom.getText().toString();

        if (pass.isEmpty() || mail.isEmpty()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
            builder.setMessage(R.string.signup_error_message)
                    .setTitle(R.string.signup_error_title)
                    .setPositiveButton(android.R.string.ok, null);
            AlertDialog dialog = builder.create();
            dialog.show();
        } else {
            firebaseRef.createUser(mail, pass, new Firebase.ResultHandler(){

                @Override
                public void onSuccess() {
                    firebaseRef = new Firebase(Constants.FIREBASE_URL);
                    //  firebaseRef.setValue();
                    AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
                    builder.setMessage("Hesap başarılı bir şekilde oluşturuldu!").setPositiveButton(R.string.login_button_label, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(KaydolActivity.this, loginWithMail.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }

                @Override
                public void onError(FirebaseError firebaseError) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
                    builder.setMessage(firebaseError.getMessage())
                            .setTitle(R.string.signup_error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            });
        }

        new Firebase(itemsUrl).push().child("name").setValue(name);
        new Firebase(itemsUrl).push().child("surname").setValue(surname);
        new Firebase(itemsUrl).push().child("gender").setValue(gender);
        new Firebase(itemsUrl).push().child("mail").setValue(mail);
        new Firebase(itemsUrl).push().child("pass").setValue(pass);
        new Firebase(itemsUrl).push().child("phone").setValue(phone);
        new Firebase(itemsUrl).push().child("trom").setValue(trom);
        new Firebase(itemsUrl).push().child("bloodGroup").setValue(bloodGroup);
        new Firebase(itemsUrl).push().child("trom").setValue(trom);
        new Firebase(itemsUrl).push().child("dateBlood").setValue(dateBlood);
        new Firebase(itemsUrl).push().child("dateTrombo").setValue(dateTrombo);
    }
});

my source code (for help / for example ) : https://www.sitepoint.com/creating-a-cloud-backend-for-your-android-app-using-firebase/

When I save a data to firebase as the following photo ( Login&Auth ) enter image description here

but mUserId is null. so when I look database mUserid is null ( like a photo ) enter image description here

Upvotes: 21

Views: 76332

Answers (6)

Chetan
Chetan

Reputation: 94

In your code, your firebaseRef seems to be null and therefore results in null uid, the recommended way of getting the user id from Firebase is to do below anywhere in your app after user logged-in

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

Upvotes: 3

Heartless Vayne
Heartless Vayne

Reputation: 992

in REACT NATIVE it's just like this.

  import firebase as 'firebase';

    HandleGetUserId=()=>{
    let userId = firebase.auth().currentUser.uid;
    return userId;
    }

just call the function HandleGetUserId to get the id like this

this.HandleGetUserId();

Upvotes: 0

Arthur Imona
Arthur Imona

Reputation: 1

I was also having the same problem with getting the uid. When i had it printed on the log (Log.i), i could see the uid, however when trying to use it as one of the fields in one of my objects being created, the field does not get created.

I was doing thing : String currentUId = FirebaseAuth.getInstance().getCurrentUser().getUid();

This didn't work well for me.

SOLUTION I FOUND OUT - (worked for me - try it out)

Use : FirebaseUser currentU = FirebaseAuth.getInstance().getCurrentUser(); String uId = currentU.getUid();

Upvotes: -1

Siddy Hacks
Siddy Hacks

Reputation: 2310

To get Current User UID, you just need this line of code:

String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();

Upvotes: 17

Mr. Suryaa Jha
Mr. Suryaa Jha

Reputation: 1582

You can use these lines of code to get Current User Uid with Android Firebase Framework

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
Toast.makeText(this, "" + currentFirebaseUser.getUid(), Toast.LENGTH_SHORT).show();

Upvotes: 36

acrisfh
acrisfh

Reputation: 51

the problem is that in firebase when you create a user it doesn't sign in the user. Here is from the docs:

Creates a new email / password user with the provided credentials. This method does not authenticate the user. After the account is created, the user may be authenticated with authWithPassword(). https://www.firebase.com/docs/web/api/firebase/createuser.html

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
    ref.authWithPassword(mail, pass, new Firebase.AuthResultHandler() {
        @Override
        public void onAuthenticated(AuthData authData) {
            System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
        }
        @Override
        public void onAuthenticationError(FirebaseError firebaseError) {
            // there was an error
        }
    });

So once your user is created call the authWithPassword function. Hope it helps!

Upvotes: 3

Related Questions