I've been trying so make auth with firebase and all went pretty well. But at the moment i tested it, it didn't work. The problem is that the function called createUserWithEmailAndPassowrd
is not successful. I think the firebase it's connected to android studio, because the analytics works perfectly. Could you give me a hand please?
Here is the code:
package com.example.authtest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val etEmail = findViewById<EditText>(R.id.EmailEt)
val etPwd = findViewById<EditText>(R.id.PasswordEt)
val registerBtn = findViewById<Button>(R.id.RegisterBtn)
val logInBtn = findViewById<Button>(R.id.LogInBtn)
val tvMessage = findViewById<TextView>(R.id.MessageTv)
val auth = FirebaseAuth.getInstance()
registerBtn.setOnClickListener {
if (etEmail.text.isNotEmpty() && etPwd.text.isNotEmpty()) {
auth.createUserWithEmailAndPassword(etEmail.toString(),etPwd.toString()).addOnCompleteListener {
if (it.isSuccessful) {
tvMessage.text = "Registered as: " it.result?.user?.email ?: ""
} else {
tvMessage.text = "Error registering your account!"
}
}
}
}
logInBtn.setOnClickListener {
if (etEmail.text.isNotEmpty() && etPwd.text.isNotEmpty()) {
auth.signInWithEmailAndPassword(etEmail.toString(),etPwd.toString()).addOnCompleteListener {
if (it.isSuccessful) {
tvMessage.text = "Logged in as: " it.result?.user?.email ?: ""
} else {
tvMessage.text = "Incorrect username/password!"
}
}
}
}
}
}
Thanks in advance!
CodePudding user response:
Try posting the code where you have defined the function.
----OR-----
The function in my code is defined like this:
loginButton.setOnClickListener{
val emailID:String=loginEmail.text.toString().trim{it <=' '}
val pass:String=loginPassword.text.toString().trim{it <=' '}
when{
TextUtils.isEmpty(emailID)->{
Toast.makeText(this@Login,"Enter Email ID! ", LENGTH_SHORT).show()
}
TextUtils.isEmpty(pass)-> {
Toast.makeText(this@Login, "Enter Password! ", LENGTH_SHORT).show()
}
else->{
FirebaseAuth.getInstance().signInWithEmailAndPassword(emailID,pass).addOnCompleteListener{ task->
if(task.isSuccessful){
//Store user id in a variable to pass on main activity:
val firebaseUser: FirebaseUser = task.result!!.user!!
Toast.makeText(this@Login,"Logged In Successfully" ,LENGTH_SHORT).show()
val intent = Intent(this@Login,MainActivity::class.java)
intent.flags= Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra("user_id",firebaseUser)
intent.putExtra("email_id",emailID)
startActivity(intent)
finish()
}else{
Toast.makeText(this@Login,task.exception!!.message.toString(),
LENGTH_SHORT).show()
}
}
}
}
}
OR checkout this repo. It has login, register functions defined, which work perfectly.
CodePudding user response:
The auth.createUserWithEmailAndPassword
call (and many other Firebase API calls) returns a Task
that is marked as either being successful or as having failed. In the case where the task has failed, it contains an exception that gives the root cause of that failure.
You should log the exception, and fix the root cause:
FirebaseAuth.getInstance().signInWithEmailAndPassword(emailID,pass)
.addOnCompleteListener{ task->
if (task.isSuccessful) {
...
} else {
Log.e("Firebase Auth", "Sign-in failed", task.exception); //