0

I'm developing an app where certain users can create accounts for other users by inserting an e-mail, password and role (admin, editor, etc..). My initial idea was not like this so my code does not what I want. Right now, anytime I create an account, that same user logs in with the data from this new account. What I want to do is just write on firebase realtime database the data from the new account. Can you help me?

Here is my class

package projeto.a43796.dashboard

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_create_account.*
import java.util.*
import kotlin.collections.HashMap


class CreateAccount : AppCompatActivity() {

lateinit var registerButton: Button
lateinit var email: TextInputEditText
lateinit var password: TextInputEditText
lateinit var getEmail: String
lateinit var getPassword: String
lateinit var firebaseDatabase: FirebaseDatabase
lateinit var databaseReference: DatabaseReference
lateinit var userId: String
lateinit var userRoles: Spinner
lateinit var getUserRole : String
val dropdownlist = arrayOf("Admin", "Viewer", "Editor", "Developer")

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

    userRoles = findViewById(R.id.user_roles)
    registerButton = findViewById(R.id.register_button)
    email = findViewById(R.id.email)
    password = findViewById(R.id.password)

    firebaseDatabase = FirebaseDatabase.getInstance()
    databaseReference = firebaseDatabase.reference

    val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, dropdownlist)
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)
    userRoles.adapter = adapter
    userRoles.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {
            Toast.makeText(
                this@CreateAccount,
                "Selected item: " + userRoles.selectedItem,
                Toast.LENGTH_SHORT
            ).show()
        }

    }

    registerButton.setOnClickListener(){

        when{
            TextUtils.isEmpty(email.text.toString().trim{it <= ' '}) -> {
                Toast.makeText(
                    this@CreateAccount,
                    "Please enter your e-mail",
                    Toast.LENGTH_SHORT
                ).show()
            }

            TextUtils.isEmpty(password.text.toString().trim{it <= ' '}) -> {
                Toast.makeText(
                    this@CreateAccount,
                    "Please enter your password",
                    Toast.LENGTH_SHORT
                ).show()
            }

            else -> {
                email1 = email.text.toString().trim{it <= ' '}
                val password1: String = password.text.toString().trim{it <= ' '}
                userRole1 = userRoles.selectedItem as String


                FirebaseAuth.getInstance().createUserWithEmailAndPassword(email1, password1)
                    .addOnCompleteListener { task ->
                        if (task.isSuccessful) {
                            val firebaseUser: FirebaseUser = task.result!!.user!!

                            Toast.makeText(
                                this@CreateAccount,
                                "Account Created",
                                Toast.LENGTH_SHORT
                            ).show()

                            val intent =
                                Intent(this@CreateAccount, Dashboard::class.java)
                            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

                            startActivity(intent)
                            finish()

                            getEmail = email1
                            getPassword = password.text.toString()
                            userId = FirebaseAuth.getInstance().currentUser!!.uid
                            getUserRole = userRole1

                            val hashmap = HashMap<String, Any>()
                            hashmap.put("E-mail", getEmail)
                            hashmap.put("Password", getPassword)
                            hashmap.put("Role", getUserRole)

                            databaseReference.child("Users").child(userId).setValue(hashmap).addOnSuccessListener {
                                Toast.makeText(
                                    this@CreateAccount,
                                    "Data added successfully",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }
                                .addOnFailureListener{
                                    Toast.makeText(
                                        this@CreateAccount,
                                        "Error adding data",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                        }
                        else{
                            Toast.makeText(
                                this@CreateAccount,
                                task.exception!!.message.toString(),
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }

            }
        }
    }
}

companion object {
    var userRole1 : String = ""
    var email1 : String = ""

}

}
  • 1
    There is no way to create an account with the client-side Firebase Authentication SDKs without signing in. While there are workaround to implement your use-case (such as creating a second `FirebaseApp` instance and creating/signing in the account on that one), the better way to implement this is by creating the users in server-side code (like in Cloud Functions). I linked a few questions where this was covered before. – Frank van Puffelen Aug 22 '21 at 01:25

0 Answers0