Created
April 23, 2019 15:06
-
-
Save ismailgungor/9ea2a2a1505e68defb6e6020cffae46f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirebaseAuthManager { | |
private var mAuth: FirebaseAuth? = null | |
init { | |
this.mAuth = FirebaseAuth.getInstance() | |
} | |
fun initializeAuthListener(authListener: IFirebaseAuthListener) { | |
this.mAuth?.addAuthStateListener { firebaseAuth -> | |
if (firebaseAuth.currentUser == null) { | |
authListener.onSignOut() | |
} | |
} | |
} | |
fun reAuthenticateUser(password: String, authanticateListener: IFirebaseAuthenticateListener) { | |
this.mAuth?.currentUser?.let { | |
val credential = EmailAuthProvider | |
.getCredential(it.email!!, password) | |
it.reauthenticate(credential) | |
?.addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
authanticateListener.onComplated() | |
} else { | |
authanticateListener.onFailed() | |
} | |
} | |
} ?: authanticateListener.onFailed() | |
} | |
fun isUserLoggedIn(): Boolean { | |
mAuth?.currentUser?.let { | |
return true | |
} ?: return false | |
} | |
fun getUserId(): String? { | |
return mAuth?.currentUser?.uid | |
} | |
fun signOut() { | |
mAuth?.signOut() | |
} | |
fun signIn(password: String, email: String, authanticateListener: IFirebaseAuthenticateListener) { | |
mAuth?.signInWithEmailAndPassword(email, password) | |
?.addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
authanticateListener.onComplated() | |
} else { | |
authanticateListener.onFailed() | |
} | |
} | |
} | |
fun signUp(password: String, email: String, authanticateListener: IFirebaseAuthenticateListener) { | |
mAuth?.createUserWithEmailAndPassword(email, password) | |
?.addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
authanticateListener.onComplated() | |
} else { | |
authanticateListener.onFailed() | |
} | |
} | |
} | |
fun resetPassword(email: String, authanticateListener: IFirebaseAuthenticateListener) { | |
mAuth?.sendPasswordResetEmail(email) | |
?.addOnCompleteListener { task -> | |
if (task.isSuccessful) | |
authanticateListener.onComplated() | |
else | |
authanticateListener.onFailed() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment