Reputation: 3730
I'm using realtime database to save user info but the problem is that it is not working at all , meanwhile storage and cloud firestore work properly , tried changing rules , tried different methods but none really worked , if someone could help thank you in advance .
class CustomerRegistrationActivity : AppCompatActivity() {
private lateinit var imageDownloadUrl : String
private var imageUrl : Uri? = null
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var databaseReference: DatabaseReference
private lateinit var storageReference: StorageReference
private var _binding : ActivityCustomerRegistrationBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityCustomerRegistrationBinding.inflate(layoutInflater)
setContentView(binding.root)
init()
binding.register.setOnClickListener {
binding.register.startAnimation()
registerNewCustomer()
}
binding.customerImg.setOnClickListener {
selectCustomerImage()
}
binding.haveAccount.setOnClickListener {
Intent(this, CustomerLoginAcitivity::class.java).apply {
startActivity(this)
finish()
}
}
}
private fun init(){
firebaseAuth = FirebaseAuth.getInstance()
databaseReference = FirebaseDatabase.getInstance().reference
storageReference = FirebaseStorage.getInstance().reference
}
private fun registerNewCustomer(){
val customerUserName = binding.userName.text.toString()
val customerPhone = binding.phone.text.toString()
val customerEmail = binding.email.text.toString()
val customerPassword = binding.password.text.toString()
if(TextUtils.isEmpty(customerUserName)){
Toast.makeText(this,"Missing Username..",Toast.LENGTH_SHORT).show()
return
}
if(TextUtils.isEmpty(customerPhone)){
Toast.makeText(this,"Missing phone..",Toast.LENGTH_SHORT).show()
return
}
if(TextUtils.isEmpty(customerEmail)){
Toast.makeText(this,"Missing Email..",Toast.LENGTH_SHORT).show()
return
}
if(TextUtils.isEmpty(customerPassword)){
Toast.makeText(this,"Missing password..",Toast.LENGTH_SHORT).show()
return
}
lifecycleScope.launch(Dispatchers.Main) {
try {
// creating user account
firebaseAuth.createUserWithEmailAndPassword(customerEmail,customerPassword).await()
// check if image url is not null
imageDownloadUrl = if(imageUrl == null){
""
} else {
// if url not null , push into firestorage
val task = storageReference.child("Customers")
.child(firebaseAuth.currentUser?.uid!!).putFile(imageUrl!!)
task.await().storage.downloadUrl.toString()
}
// creating a map for user info
val credentialsMap = hashMapOf<String,Any>()
credentialsMap["customerName"] = customerUserName
credentialsMap["customerEmail"] = customerEmail
credentialsMap["CustomerPhone"] = customerPhone
credentialsMap["accountType"] = "Customer"
credentialsMap["profileImage"] = imageDownloadUrl
// pushing data into realtime database
// the issue is here , i even debugged the code , it executes but don't pass to
// to the next code and it is not saving data
databaseReference
.child("customers")
.child(firebaseAuth.currentUser?.uid!!)
.setValue(credentialsMap).await()
// the code here gets never exeucte but there is issue with code above of real db
binding.register.stopAnimation()
CoroutineScope(Dispatchers.Main).launch {
delay(1000)
Intent(this@CustomerRegistrationActivity,CustomerMainActivity::class.java).apply {
startActivity(this)
finish()
}
}
}catch (ex : Exception){
binding.register.stopAnimation()
Log.d(Utils.ACTIVITY_TAG,"Exception Registration ${ex.message}")
}
}
}
private fun selectCustomerImage(){
Intent().apply {
type = "image/*"
action = Intent.ACTION_GET_CONTENT
startActivityForResult(this, IMAGE_REQUEST_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == IMAGE_REQUEST_CODE){
data?.data.let {
imageUrl = it
binding.customerImg.setImageURI(imageUrl)
}
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
companion object {
const val IMAGE_REQUEST_CODE = 1001
}
}
{
"rules": {
".read": "auth !== null",
".write": "auth !== null"
}
}
Upvotes: 1
Views: 398
Reputation: 34
Try to remove .await() while pushing and set db rules to true
".read": true,
".write": true
Upvotes: 1