了解firebase(特别是android库)的createUser函数



所以我有以下代码,我从firebase文档(我在我的应用程序中已经实现,它的工作很好):

    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
       }
    });

在我创建一个用户之后,它在控制台上打印它的uid。然而,当我进入我的myapp.firebaseio.com时,那里什么也没有…所以我有一些问题:

  1. firebase将创建的新用户存储在哪里?
  2. 我如何添加一些自定义字段?(此函数仅使用电子邮件和密码)即Username

所以,我试图做的是在onSuccess()中,我使用ref.push()一些值到myapp.firebaseio.com,但是然后…我如何检查由createUser()创建的用户uid是否与我推送的用户uid相同?(id不同!)

我希望我的文字是清楚的,如果没有问,我可以再解释一次!

多谢了!

用户信息是而不是存储在您的Firebase数据库中。对于匿名用户和OAuth用户,任何地方都不会存储任何信息。电子邮件+密码用户的信息保存在您无法访问的单独数据库中。电子邮件+密码用户在登录&当然是仪表板的Auth选项卡,只是不在数据库中。

如果您想在您自己的Firebase数据库中存储用户信息,您必须在创建或验证用户时自己将其存储在那里。在Firebase文档中有一节介绍如何存储用户数据。

必须自己存储信息的一个好处是,您可以确定哪些信息要存储,哪些不存储。

正如Frank所说;在创建用户时,不会自动将用户信息放入firebase本身(请查看仪表板侧栏中的loginauth)。新用户创建后甚至没有登录。这是我在注册时用于登录并在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.
            }
        }
    );
}
到目前为止,这对我来说工作得很好。我想如果连接在中间被中断,可能会出现问题(可能最终导致用户没有初始信息)。

根据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);

                                }
                            }
                        });

/get user detail against 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's 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.");
                }
            }
        });
    }

查看详情

最新更新