繁体   English   中英

如何从注册用户那里获取特定信息并将其显示在 Kotlin 和 Firebase 的应用程序中?

[英]How do I get specific information from a registered user and display it in my app in Kotlin with Firebase?

我是 Kotlin 和 Firebase 的新手,我希望这种情况发生:

当注册用户使用他的 Email 和密码登录时,我想在我的仪表板(新活动)中显示连接到该 Email 的完整名称(用户必须在注册时输入完整名称),我该怎么做?

相关代码:
MainActivity.kt:

lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)

    auth = FirebaseAuth.getInstance()

    val currentUser = auth.currentUser
    if (currentUser != null) {
        val intent = Intent(this@MainActivity, dashboard::class.java)
        startActivity(intent)
        finish()
    }
    login()
}

private fun login() {
    loginButton.setOnClickListener {
        if (TextUtils.isEmpty(emailLogin.text.toString())) {
            emailLogin.error = "Please enter registered email."
            return@setOnClickListener
        } else if (TextUtils.isEmpty(passwordLogin.text.toString())) {
            passwordLogin.error = "Please enter password."
            return@setOnClickListener
        }
        auth.signInWithEmailAndPassword(emailLogin.text.toString(), passwordLogin.text.toString())
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    val intent = Intent(this@MainActivity, dashboard::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    Toast.makeText(
                        this@MainActivity,
                        "Login failed, please try again! ",
                        Toast.LENGTH_LONG
                    ).show()
                }
            }

    }

dashboard.kt(如果相关,则为 idk):

lateinit var auth: FirebaseAuth
lateinit var toggle: ActionBarDrawerToggle
var databaseReference: DatabaseReference? = null
var database: FirebaseDatabase? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_dashboard)

    val actionBar = supportActionBar
    actionBar!!.title = "Dashboard"

    auth = FirebaseAuth.getInstance()
    database = FirebaseDatabase.getInstance()
    databaseReference = database?.reference!!.child("profile")

    toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close)
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    supportActionBar?.setDisplayHomeAsUpEnabled(true)

    nv.setNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.profile -> Toast.makeText(applicationContext, "Clicked Profile", Toast.LENGTH_SHORT).show()
            R.id.settings -> Toast.makeText(applicationContext, "Clicked Settings", Toast.LENGTH_SHORT).show()
            R.id.logoutButton -> logout()
        }
        true
    }
}

    private fun logout() {
        auth.signOut()
        startActivity(Intent(this@dashboard, MainActivity::class.java))
        finish()
    }

我希望在此显示完整名称:

<TextView
                    android:id="@+id/completeNameDisplay"
                    android:layout_width="match_parent"
                    android:layout_height="132dp"
                    android:layout_marginStart="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="15dp"
                    android:background="@drawable/dashboard_modules"
                    android:padding="10dp"
                    android:paddingStart="25dp"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    android:textStyle="bold" />

注册片段:

private fun register(){
    registerButton.setOnClickListener {
            if(TextUtils.isEmpty(fullNameRegister.text.toString())) {
                fullNameRegister.setError("Please enter full name")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(phoneNumberRegister.text.toString())) {
                phoneNumberRegister.setError("Please enter a valid phone number")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(passwordRegister.text.toString())) {
                passwordRegister.setError("Please enter password")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(regionRegister.text.toString())) {
                passwordRegister.setError("Please enter Region")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(cityRegister.text.toString())) {
                passwordRegister.setError("Please enter City")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(address1Register.text.toString())) {
                passwordRegister.setError("Please enter address")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(address2Register.text.toString())) {
                passwordRegister.setError("Please enter address")
                return@setOnClickListener
            } else if(TextUtils.isEmpty(emailRegister.text.toString())) {
                passwordRegister.setError("Please enter email")
                return@setOnClickListener
            }

        auth.createUserWithEmailAndPassword(emailRegister.text.toString(), passwordRegister.text.toString())
            .addOnCompleteListener{
                if(it.isSuccessful){
                    val currentUser = auth.currentUser
                    val currentUSerDb = databaseReference?.child((currentUser?.uid!!))
                    currentUSerDb?.child("Fullname")?.setValue(fullNameRegister.text.toString())
                    Toast.makeText(this@registration, "Registration Success! ", Toast.LENGTH_LONG).show()
                    finish()

                } else {
                    Toast.makeText(this@registration, "Registration failed, please try again! ", Toast.LENGTH_LONG).show()
                }
            }
    }
}

正如评论中所讨论的,听起来代码正在按您的预期工作。 在这种情况下,我将假设用户在登录时注册了 Firebase Auth。如果这是真的,您应该能够访问当前用户,如此处文档中所述: https://firebase.google。 com/docs/auth/android/manage-users#get_a_users_profile

val user = Firebase.auth.currentUser
user?.let {
    // Name, email address, and profile photo Url
    val name = user.displayName
    val email = user.email
    val photoUrl = user.photoUrl

    // Check if user's email is verified
    val emailVerified = user.isEmailVerified

    // 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.
    val uid = user.uid
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM