【问题标题】:Understanding createUser function of firebase(specifically android library)了解firebase的createUser函数(特别是android库)
【发布时间】:2015-09-09 22:21:07
【问题描述】:

所以我有以下从 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. 如何添加一些自定义字段? (此功能仅使用电子邮件和密码)即用户名

所以,我尝试做的是在 onSuccess() 内部我使用了 ref.push() 一些值到 myapp.firebaseio.com 但是然后.. 我如何检查是否由 createUser() 创建的用户 uid和我推的那个一样吗? (id 不同!)

我希望我的文字很清楚,如果没有被问到,我可以尝试再次解释!

非常感谢!

【问题讨论】:

    标签: android firebase firebasesimplelogin firebase-authentication


    【解决方案1】:

    用户信息存储在您的 Firebase 数据库中。对于匿名用户和 OAuth 用户,任何地方都不会存储任何信息。电子邮件+密码用户的信息保存在您无权访问的单独数据库中。电子邮件+密码用户当然可以在仪表板的登录和身份验证选项卡中看到,只是在您的数据库中不可见。

    如果您想将用户信息存储在自己的 Firebase 数据库中,则必须在创建或验证用户时自行将其存储在那里。有一个section on storing user data in the Firebase documentation 显示了如何执行此操作。

    必须自己存储信息的一个好处是,您可以准确地确定哪些是存储的,哪些没有存储。

    【讨论】:

    • 感谢您的澄清,并对这个愚蠢的问题感到抱歉。我现在没有将它们存储在 Login & Auth 中,并且我没有看到存储用户数据部分(尽管我正在考虑做一些与此非常相似的事情!)。无论如何,非常感谢!
    • 不需要道歉。这不是一个愚蠢的问题。 Firebase 文档内容广泛,很容易漏掉一个部分。
    • 啊,还有一个问题:在 onSuccess() 中保存信息是否可行?就像在您发布的部分中所做的一样(使用此 authWithPassword())?
    • 如有疑问,我会按照产品文档告诉您的操作。
    • @Frank ,我关注您的共享文档。但它不起作用。新版本的firebase有什么变化吗?
    【解决方案2】:

    正如弗兰克所说;在创建用户时,不会自动将用户信息放入 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);
      
      
      
                                      }
                                  }
                              });
      

      /根据 FirebaseAuth 身份验证获取用户详细信息/

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

      check for detailse

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-10
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-06-27
        • 1970-01-01
        • 2018-09-22
        相关资源
        最近更新 更多