Understanding the createUser function for firebase (in particular, the Android library)

So, I have the following code that I got from the firebase documentation (which I have already implemented in my application and it works fine):

    Firebase ref = new Firebase("https://myapp.firebaseio.com");
    ref.createUser("bobtony@firebase.com", "correcthorsebatterystaple", new Firebase.ValueResultHandler<Map<String, Object>>() {
       @Override
       public void onSuccess(Map<String, Object> result) {
          System.out.println("Successfully created user account with uid: " + result.get("uid"));
       }
       @Override
       public void onError(FirebaseError firebaseError) {
        // there was an error
       }
    });

after creating the user, he prints his uid on the console. However, when I enter my myapp.firebaseio.com, there is nothing there. So I have a few questions:

  • Where does this new user store firebase?
  • How to add some custom fields? (these functions use only email and password) ie Username

So, what I tried to do was inside onSuccess () I used ref.push () some values ​​for myapp.firebaseio.com, but then .. how can I check if the user uid created by createUser () is the same like the one i pushed? (id are different!)

, , , !

!

+4
3

Firebase. OAuth . + , . + "" "" , .

Firebase, , . Firebase, , .

, , .

+5

; firebase ( Login & Auth ). . , firebase :

static void createUser(final String username, final String password) {

    final Firebase rootRef = new Firebase("YOUR_FIREBASE_URL");

    rootRef.createUser(
        username, 
        password, 
        new Firebase.ResultHandler() {
            @Override
            public void onSuccess() {
                // Great, we have a new user. Now log them in:
                rootRef.authWithPassword(
                    username, 
                    password,
                    new Firebase.AuthResultHandler() {
                        @Override
                        public void onAuthenticated(AuthData authData) {
                            // Great, the new user is logged in. 
                            // Create a node under "/users/uid/" and store some initial information, 
                            // where "uid" is the newly generated unique id for the user:
                            rootRef.child("users").child(authData.getUid()).child("status").setValue("New User");
                        }

                        @Override
                        public void onAuthenticationError(FirebaseError error) {
                            // Should hopefully not happen as we just created the user.
                        }
                    }
                );
            }

            @Override
            public void onError(FirebaseError firebaseError) {
                // Couldn't create the user, probably invalid email.
                // Show the error message and give them another chance.
            }
        }
    );
}

. , - , ( ). , ...

+3

, Firebase.

//create user
                auth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                if (!task.isSuccessful()) {
                                    Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
                                            Toast.LENGTH_SHORT).show();
                                } else {
                                    Log.e("task",String.valueOf(task));

                                    getUserDetailse(auth);



                                }
                            }
                        });

/ Detailse FirebaseAuth auth/

 public static  void getUserDetailse(FirebaseAuth auth)
    {

        //
        auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
                final FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    Log.i("AuthStateChanged", "User is signed in with uid: " + user.getUid());
                    String name = user.getDisplayName();
                    String email = user.getEmail();
                    Uri photoUrl = user.getPhotoUrl();

                    // The user ID, unique to the Firebase project. Do NOT use this value to
                    // authenticate with your backend server, if you have one. Use
                    // FirebaseUser.getToken() instead.
                    String uid = user.getUid();
                    Log.e("user",name+email+photoUrl);

                } else {
                    Log.i("AuthStateChanged", "No user is signed in.");
                }
            }
        });

    }

detailse

0

Source: https://habr.com/ru/post/1606664/


All Articles