I'm still very new at this so apologize for any minor mistake I made
I'm trying to learn how to make a login system with email and password using Firebase but it doesn't seem to work as intended. The code to check whether the email is empty, incorrect format, password length not matching the parameter is not working too. I double check my code before running and I'm not getting any result.
I already connect this app to Firebase and add Firebase Authentication SDK
Clicking the login button will not make any error as intended
loginPage.kt
class loginPage : AppCompatActivity() {
lateinit var binding : LoginPageBinding
lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
binding = LoginPageBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
setContentView(R.layout.login_page)
auth = FirebaseAuth.getInstance()
login_back_button.setOnClickListener{
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
binding.loginButton.setOnClickListener{
val email = binding.emailField.text.toString()
val password = binding.passwordField.text.toString()
if (email.isEmpty()){
binding.emailField.error = "Email cannot be empty"
binding.emailField.requestFocus()
return@setOnClickListener
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
binding.emailField.error = "Email not valid"
binding.emailField.requestFocus()
return@setOnClickListener
}
if (password.length > 6){
binding.passwordField.error = "Minimal password length is 6"
binding.passwordField.requestFocus()
return@setOnClickListener
}
RegisterFirebase(email,password)
}
}
private fun RegisterFirebase(email: String, password: String) {
auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this){
if (it.isSuccessful){
Toast.makeText(this, "Register Successful", Toast.LENGTH_SHORT).show()
val intent = Intent(this, homePage::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "${it.exception?.message}", Toast.LENGTH_SHORT).show()
}
}
}
}
homePage.kt
class homePage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
setContentView(R.layout.home_page)
}
}
EditText for entering email (in login_page.xml)
<EditText
android:id="@ id/email_field"
android:layout_width="272dp"
android:layout_height="31dp"
android:background="@drawable/email_password_filler"
android:hint="@string/enter_your_email"
android:paddingStart="20sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:textSize="13sp"
android:maxLines="1"
android:lines="1"
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.126"
tools:ignore="Autofill,RtlSymmetry,TextFields" />
For entering password (in login_page.xml)
<EditText
android:id="@ id/password_field"
android:layout_width="272dp"
android:layout_height="31dp"
android:background="@drawable/email_password_filler"
android:hint="@string/enter_your_password"
android:paddingStart="20sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:textSize="13sp"
android:maxLines="1"
android:lines="1"
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.208"
tools:ignore="Autofill,RtlSymmetry,TextFields" />
CodePudding user response:
The password should always be greater than or equal to 6 and in your case, the condition is opposite. Fix it like this:
if (password.length < 6) {
binding.passwordField.error = "Minimal password length is 6"
binding.passwordField.requestFocus()
return@setOnClickListener
}
login_back_button
is missing binding
reference as well as if it's a back button then you should just use finish
to go back to previous activity rather than creating new instance again. Like this:
binding.loginBackButton.setOnClickListener { finish() }
Update:
The issue is that you are using binding
but not passing it in setContentView
. Fix it like this:
setContentView(binding.root)