使用 MVVM kotlin 进行 Firebase 身份验证
Firebase authentication with MVVM kotlin
我是 MVVM 和 Jetpack 库的新手。我有一个简单的 firebase 身份验证、注册和登录应用程序。我尝试了很多代码,但我没有获得视图模型和目录的流程。所以任何人都可以将此代码转换为视图模型及其工作原理。
这是登录代码
auth = FirebaseAuth.getInstance()
fun login(view: View){
var email:String = binding.editTxtEmail.text.toString()
var password:String = binding.editTxtPassword.text.toString()
auth.signInWithEmailAndPassword(email,password).addOnCompleteListener { task ->
if(task.isSuccessful){
val intent= Intent(this,MainActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(applicationContext,exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}
首先,您需要添加一个 ViewModel,然后在 ViewModel 中添加两个接受电子邮件和密码的方法,如下所示。
class AuthViewModel: ViewModel() {
fun handleSignIn(email: String, password: String) {
}
fun handleSignUp(email: String, password: String, confirmPassword: String) {
}
}
然后你需要添加一个枚举或密封class来表示你的登录流程的可能状态,这里是一个例子:
sealed class AuthState {
object Idle : AuthState()
object Loading : AuthState()
object Success : AuthState()
class AuthError(val message: String? = null) : AuthState()
}
然后您需要添加一个可观察状态并将其公开为 LiveData,如下所示:
private val _authState by lazy { MutableLiveData<AuthState>(AuthState.Idle) }
val authState: LiveData<AuthState> = _authState
添加后,您现在可以更新您的方法来处理注册并像这样注册:
fun handleSignUp(email: String, password: String, confirmPassword: String) {
if (!isEmailValid(email)) {
_authState.value = AuthState.AuthError("Invalid email")
return
}
if (password != confirmPassword) {
_authState.value = AuthState.AuthError("Password does not match")
return
}
FirebaseAuth.getInstance().createUserWithEmailAndPassword(
email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.i(TAG,"Email signup is successful")
_authState.value = AuthState.Success
} else {
task.exception?.let {
Log.i(TAG,"Email signup failed with error ${it.localizedMessage}")
_authState.value = AuthState.AuthError(it.localizedMessage)
}
}
}
}
然后您可以收听列表
val authState by viewModel.authState.observeAsState(AuthState.Idle)
祝你好运。
我是 MVVM 和 Jetpack 库的新手。我有一个简单的 firebase 身份验证、注册和登录应用程序。我尝试了很多代码,但我没有获得视图模型和目录的流程。所以任何人都可以将此代码转换为视图模型及其工作原理。
这是登录代码
auth = FirebaseAuth.getInstance()
fun login(view: View){
var email:String = binding.editTxtEmail.text.toString()
var password:String = binding.editTxtPassword.text.toString()
auth.signInWithEmailAndPassword(email,password).addOnCompleteListener { task ->
if(task.isSuccessful){
val intent= Intent(this,MainActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(applicationContext,exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}
首先,您需要添加一个 ViewModel,然后在 ViewModel 中添加两个接受电子邮件和密码的方法,如下所示。
class AuthViewModel: ViewModel() {
fun handleSignIn(email: String, password: String) {
}
fun handleSignUp(email: String, password: String, confirmPassword: String) {
}
}
然后你需要添加一个枚举或密封class来表示你的登录流程的可能状态,这里是一个例子:
sealed class AuthState {
object Idle : AuthState()
object Loading : AuthState()
object Success : AuthState()
class AuthError(val message: String? = null) : AuthState()
}
然后您需要添加一个可观察状态并将其公开为 LiveData,如下所示:
private val _authState by lazy { MutableLiveData<AuthState>(AuthState.Idle) }
val authState: LiveData<AuthState> = _authState
添加后,您现在可以更新您的方法来处理注册并像这样注册:
fun handleSignUp(email: String, password: String, confirmPassword: String) {
if (!isEmailValid(email)) {
_authState.value = AuthState.AuthError("Invalid email")
return
}
if (password != confirmPassword) {
_authState.value = AuthState.AuthError("Password does not match")
return
}
FirebaseAuth.getInstance().createUserWithEmailAndPassword(
email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.i(TAG,"Email signup is successful")
_authState.value = AuthState.Success
} else {
task.exception?.let {
Log.i(TAG,"Email signup failed with error ${it.localizedMessage}")
_authState.value = AuthState.AuthError(it.localizedMessage)
}
}
}
}
然后您可以收听列表
val authState by viewModel.authState.observeAsState(AuthState.Idle)
祝你好运。