skip to Main Content

I need to display the name whenever a user logs in with email. Currently Registration has been done using firebase.
The users data (Email and Password) has been saved in Firebase Database.

Registration and Login page have been developed. looking for response to display the name as "Welcome User"

Example:
Email : [email protected]
Password : 123456
I need to display "Welcome User1" on my screen.
Example 2:
Email : [email protected]
Password : 123456
I’m open to displaying either or both of the names which are present before "@".

My Login Activity

class MainActivity : AppCompatActivity() {

private lateinit var signUp: TextView
private lateinit var username: EditText
private lateinit var password: EditText
private lateinit var btnLogin: Button
private lateinit var auth: FirebaseAuth

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

    signUp = findViewById(R.id.signUp)
    btnLogin = findViewById(R.id.ln)
    username = findViewById(R.id.usn)
    password = findViewById(R.id.pwd)

    // initialising Firebase auth object
    auth = FirebaseAuth.getInstance()


    btnLogin.setOnClickListener {
        login()

    }

    signUp.setOnClickListener {
        val intent = Intent(this, Registration::class.java)
        startActivity(intent)
        finish()
    }
}

private fun login() {
    val userName = username.text.toString()
    val pass = password.text.toString()

    if (userName.isNotEmpty() && pass.isNotEmpty()) {

        auth.signInWithEmailAndPassword(userName, pass).addOnCompleteListener(this) {
            if (it.isSuccessful) {
                val intent = Intent(this, Dashboard::class.java)
                startActivity(intent)
                Toast.makeText(this, "Successfully LoggedIn", Toast.LENGTH_SHORT).show()
            } else Toast.makeText(this, "Log In failed ", Toast.LENGTH_LONG).show()
        }
    }
}

}

My Registration Activity

class Registration : AppCompatActivity() {

private lateinit var userName: EditText
private lateinit var confirmPassword: EditText
private lateinit var password: EditText
private lateinit var register: Button
private lateinit var login: TextView
private lateinit var auth: FirebaseAuth

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

    userName = findViewById(R.id.usn)
    confirmPassword = findViewById(R.id.etSConfPassword)
    password = findViewById(R.id.etSPassword)
    register = findViewById(R.id.register)
    login = findViewById(R.id.tvRedirectLogin)
    auth = Firebase.auth

    register.setOnClickListener {
        registerUser()
    }

    login.setOnClickListener {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }
}

private fun registerUser() {
    val usn = userName.text.toString()
    val pass = password.text.toString()
    val confirmPassword = confirmPassword.text.toString()

    if (usn.isBlank() || pass.isBlank() || confirmPassword.isBlank()) {
        Toast.makeText(this, "User Name and Password can't be blank", Toast.LENGTH_SHORT).show()
        return
    }

    if (pass != confirmPassword) {
        Toast.makeText(this, "Password and Confirm Password do not match", Toast.LENGTH_SHORT)
            .show()
        return
    }
    auth.createUserWithEmailAndPassword(usn, pass).addOnCompleteListener(this) {
        if (it.isSuccessful) {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
            finish()
        } else {
            Toast.makeText(this, "Registration Failed!", Toast.LENGTH_SHORT).show()
        }
    }
}

}

2

Answers


  1. Following the documentation

    First you need to store the full user email in a variable

    val userEmail = FirebaseAuth.getInstance().currentUser.email
    

    Once you have the user’s email, you can use .substringBefore()

    val email = userEmail.substringBefore("@")
    

    Kotlin Playground:

    enter image description here

    Login or Signup to reply.
  2. I’m not exactly answering to your question ("the name taking from email") but I would suggest using the displayName property of a User instead of extracting the name from the email.

    More details in the Firebase Auth documentation:

    For updating the User’s profile:

    val user = Firebase.auth.currentUser
    
    val profileUpdates = userProfileChangeRequest {
        displayName = "Jane Q. User"
    }
    
    user!!.updateProfile(profileUpdates)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Log.d(TAG, "User profile updated.")
            }
        }
    

    For getting the displayName:

    val user = Firebase.auth.currentUser
    user?.let {
        
        val name = it.displayName
        
        //...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search