android 如何在firebase中保存用户名和邮件?[duplicate]

mwg9r5ms  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(107)
    • 此问题在此处已有答案**:

How do you include a username when storing email and password using Firebase (BaaS) in an Android app?(3个答案)
六年前关闭了。
Firebase只保存用户的电子邮件。我也想保存他/她的名字。
如何做到这一点?
下面是我用来创建和登录用户的一些代码:

public void loginByEmailMethod() {
        //creating account
        ref.createUser(email, password, 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"));
                Toast.makeText(getBaseContext(), "Account successfully created!", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(FirebaseError firebaseError) {
                // there was an error
                Toast.makeText(getBaseContext(), "Account not created!", Toast.LENGTH_LONG).show();
            }
        });

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //logging in the user
                ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {
                    @Override
                    public void onAuthenticated(AuthData authData) {
                        System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
                        Intent mainActivityIntent = new Intent(SignupScreenActivity.this, MainActivity.class);
                        startActivity(mainActivityIntent);

                        Toast.makeText(getBaseContext(), "User logged in successfully!", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onAuthenticationError(FirebaseError firebaseError) {
                        // there was an error
                        Toast.makeText(getBaseContext(), "User not logged in!", Toast.LENGTH_LONG).show();
                        progressDialog.dismiss();
                    }
                });
            }
        }, 2000);
    }

问题是,使用这种方法,我只能保存用户的电子邮件地址,而不是他/她的名字。
请让我知道我如何保存名称与电子邮件。
先谢了。

ztyzrc3y

ztyzrc3y1#

您通常希望在/users节点中存储任何其他用户信息。
像这样

uid
  name: "Ted"
  fav_movie "Airplane"
  position: "Sitting front facing forward"

uid是在Firebase中创建用户时创建的用户ID。
这是Firebase中非常常见的设计模式,在他们的User Authentication指南中有一个非常棒的示例。
在"存储用户数据"部分中大约1/2处是您要查看的内容。

相关问题