Question: My application isn't taking the user input and checking them towards the database. When I fill in the fields email and password in my app

My application isn't taking the user input and checking them towards the database. When I fill in the fields "email and password" in my app the message "Login failed" pops up even though I know that the informasjon that I have typed in is in my database. Can anyone help me solve this problem? I have a repository and view model I don't know what more code is needed. I have made a signup function and that works great, but I am having issues with the login part. Thank you! Login code:  class Login : AppCompatActivity() { private lateinit var binding: ActivityLoginBinding private lateinit var appDb: Userdatabase private lateinit var button: Button private lateinit var userDao: Dao override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLoginBinding.inflate(layoutInflater) setContentView(binding.root) appDb = Userdatabase.getDatabase(this) binding.btnSignUp.setOnClickListener { val intent = Intent(this, Signup::class.java) startActivity(intent) } binding.btnLogin.setOnClickListener { readData() } } private fun readData() { val email = binding.edtEmail.text.toString() val password = binding.edtPassword.text.toString() if(email.isNotBlank() && password.isNotBlank()) { lifecycleScope.launch(Dispatchers.IO) { try { val userRepository = UserRepository(userDao) val user = userRepository.verifyUser(email, password) //appDb.dao().getUser(user) runOnUiThread { Toast.makeText(this@Login, "Login Success", Toast.LENGTH_SHORT).show() val intent1 = Intent(this@Login, Home::class.java) startActivity(intent1) } } catch (e: Exception) { runOnUiThread { Toast.makeText(this@Login, "Login Failed", Toast. LENGTH_SHORT).show() } } } } else { Toast.makeText(this@Login, "Login Failed", Toast.LENGTH_SHORT).show() } } }

This is function in the repository:

fun verifyUser(email: String, password: String): User { try { Log.d("UserRepository", "verifyUser started") val user = userDao.getUser(email, password) Log.d("UserRepository", "getUser success") if (user.password == password) { return user } else { throw Exception("Invalid Password") } } catch (e: Exception) { Log.e("UserRepository", "verifyUser failed: ${e.message}") throw e } } }

This is the function in the view model:

fun verifyUser(edtEmail: String, edtPassword: String) { viewModelScope.launch(Dispatchers.IO) { repository.verifyUser(edtEmail, edtPassword) } }

This is the query in my Dao file:

@Query("SELECT email FROM Bruker_Tabell where email LIKE :edtEmail AND password LIKE :edtPassword") fun getUser(edtEmail: String, edtPassword: String): User

This is my table:

@Entity(tableName = "Bruker_Tabell") data class User ( @PrimaryKey(autoGenerate = true) val id: Int?, val userName: String?, val email: String?, val password: String? ) 

I am thankful for any help you guys can provide :) Have a great day!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!