Reputation: 1061
class LoginActivity : AppCompatActivity() {
private val firebaseAuth = FirebaseAuth.getInstance()
private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
val user = firebaseAuth.currentUser?.uid
user?.let {
startActivity(HomeActivity.newIntent(this))
finish()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
loginProgressLayout.setOnTouchListener { v, event -> true }
}
fun onLogin(v: View) {
var proceed = true
if (emailET.text.isNullOrEmpty()) {
emailTIL.error = "email is required"
emailTIL.isErrorEnabled = true
proceed = false
}
if(passwordET.text.isNullOrEmpty()) {
passwordTIL.error = "password is required"
passwordTIL.isErrorEnabled = true
proceed = false
}
if(proceed){
loginProgressLayout.visibility = View.VISIBLE
firebaseAuth.signInWithEmailAndPassword(emailET.text.toString(), passwordET.text.toString())
.addOnCompleteListener { task ->
if (!task.isSuccessful){
loginProgressLayout.visibility = View.GONE
Toast.makeText(this@LoginActivity, "LoginError", Toast.LENGTH_SHORT).show()
}
}
.addOnFailureListener { exception ->
exception.printStackTrace()
loginProgressLayout.visibility = View.GONE
}
}
} //onLogin end
I checked I got something authentication number from firebaseAuth.signInWithEmailAndPassword
code line.
But my question is about the property FirebaseAuth.AuthStateListener
, which doesn't work.
When I get authentication number and then I want the AuthStateListener
to work!
I read the Firebase API, but it didn't work. How can I make FirebaseAuth.AuthStateListener
work?
Upvotes: -1
Views: 2163
Reputation: 598847
You need to call addAuthStateListener
with your listener in order for it to work.
So for example in the onStart
of your activity:
override fun onStart() {
super.onStart()
firebaseAuth!!.addAuthStateListener(this.firebaseAuthListener!!)
}
I recommend studying this answer (more): Android Studio (Kotlin) - User has to log back into app every time app is closed
Upvotes: 3