I am making an app with login, signup and so on. For each operation I have a different activity and, to clean up my code, I created a Kotlin class just for the server communication with methods for every server operation. Here it is:
package com.mainpackage.provachat
import com.google.firebase.auth.FirebaseAuth
class ServerCommunication {
private var auth = FirebaseAuth.getInstance()
fun login(activity: android.app.Activity, email: String, psswd: String): Boolean{
var isLogInSucceeded = false
auth.signInWithEmailAndPassword(email, psswd)
.addOnCompleteListener(activity) { task ->
isLogInSucceeded = task.isSuccessful
}
return isLogInSucceeded
}
fun register(activity: android.app.Activity, email: String, psswd: String): Boolean{
var isSignUpSucceeded = false
auth.createUserWithEmailAndPassword(email, psswd)
.addOnCompleteListener(activity) { task ->
isSignUpSucceeded = task.isSuccessful
}
return isSignUpSucceeded
}
//----THE METHODS BELOW WORK PERFECTLY------
fun disconnectUser(){
auth.signOut()
}
fun isCurrentUserLogged(): Boolean{
if(auth.currentUser == null){
return false
}
return true
}
}
The problem comes when I try to use the method login()
or register()
infact they never succeed. I can tell it because I use a bool to verify if the task in addOnCompleteListener(activity)
is completed correctly.
Here you are my call of the method login in an activity:
var isLoginSucceeded = sc.login(this, email, psswd)
I think that the problem is due to the activity object that I pass at register()
or login()
with this
.
Thanks guys.