【问题标题】:How can I add name, profile pic, address of a user to Firebase Database?如何将用户的姓名、个人资料图片、地址添加到 Firebase 数据库?
【发布时间】:2016-09-26 13:31:49
【问题描述】:

我正在尝试制作一个 Android 应用,我想在其中将每个用户的个人详细信息(例如姓名、个人资料图片和地址)保存到 Firebase 数据库以供将来使用。

请建议我该怎么做?

【问题讨论】:

  • @KiranBennyJoseph 是的,firebase 用于存储您的数据,firebase 有云
  • 好的。这是我的错。

标签: android firebase firebase-realtime-database


【解决方案1】:

您还没有花时间熟悉These Firebase Docs。这就是为什么有这么多反对你的问题的原因。不过,我还是想简要介绍一下 Firebase 为您提供的功能:

//这样可以获取每个登录用户的用户资料

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address, and profile photo Url
    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();
}

//这是您可以更新每个登录用户的用户配置文件数据的方法

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });

如果您在使用 firebase 时没有实施任何身份验证和安全规则,那么我会说您应该尽快更改它。由于没有身份验证和适当的安全规则,任何人都可以访问您的数据库并以他/她想要的任何方式对其进行更改。

对于已登录的用户,您无需执行任何额外或特殊操作来保存他们的个人资料详细信息。如果您使用 Gmail、Facebook 等身份验证提供商,则基本个人资料信息会由 firebase 自动保存。如果您正在使用一些自定义的身份验证方法,请参考 firebase 提供的 update user profile code sn-p。这就是您保存基本用户配置文件的方式。

如果这对您有帮助,请告诉我。

【讨论】:

  • 非常感谢你——这让我在几个小时的麻烦后工作——谢谢:)
  • 这对于姓名、电子邮件和个人资料图片非常有用,但我将如何保存更多详细信息,例如地址、帐户级别、出生日期等?
  • @Nishant-Dubey 但是,如果我需要存储有关我使用 Firestore 的用户的更多信息并根据 getUid()-s 值对用户进行索引,对吗?
【解决方案2】:

我在我的项目中使用了这个代码。这个对我有用。 signUp.dart

 onPressed: () {
                  FirebaseAuth.instance.createUserWithEmailAndPassword(
               email: _email, password: _password).then((signedInUser) {
                   _uid= signedInUser.uid;
                    Map<String,String> userDetails = {'email':this._email,'displayName':this._displayname,'uid':this._uid,
                      'photoUrl':this._photo};
                    _userManagement.addData(userDetails,context).then((result){
                    }).catchError((e){
                      print(e);
                    });
                  }).catchError((e) {
                    print(e);
                  });

                },

userManagement.dart

 Future<void> addData(postData,context) async{
  Firestore.instance.collection('/users').add(postData).then((value){
    Navigator.of(context).pop();
    Navigator.of(context).pushReplacementNamed('/homepage');
  }).catchError((e){
    print(e);
  });
}

【讨论】:

    【解决方案3】:

    从 Nishants 的回答中得到帮助,这对我目前使用 Kotlin 的项目有效。

    fun registerUser(email: String, password: String, displayName: String) {
            auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(requireActivity()) { task ->
                    if (task.isSuccessful) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success")
    
                        val user = auth.currentUser
    
                        val profileUpdates =
                            UserProfileChangeRequest.Builder()
                                .setDisplayName(displayName)
                                .setPhotoUri(Uri.parse("https://firebasestorage.googleapis.com/v0/b/fakelogisticscompany.appspot.com/o/default.png?alt=media&token=60224ebe-9bcb-45fd-8679-64b1408ec760"))
                                .build()
    
                        user!!.updateProfile(profileUpdates)
                            .addOnCompleteListener { task ->
                                if (task.isSuccessful) {
                                    Log.d(TAG, "User profile updated.")
                                }
                            }
    
                        showAlert("Created your account successfully!")
    
                        updateUI(user)
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.exception)
    
                        showAlert("Authentication failed!")
    
    //                    updateUI(null)
                    }
                }
        }
    

    我正在创建用户并在之后更新详细信息。 showAlert 和 updateUI 是我自己的函数来显示 alertdialog 和重定向用户。希望这将在不久的将来对其他人有所帮助。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2021-08-26
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多