Question: Hello, am having trouble setting up a login function for my android application. I have done a lot of research and stil haven't managed to

Hello, am having trouble setting up a login function for my android application. I have done a lot of research and stil haven't managed to find the solution. The solutions that I may have found looks more complicated then what I think it needs to be. I have made a simple user registration, where a user can register him/herself and that informasjon gets stored in a database. The only thing that am struggling with now is to check the user input towards my database. Can anyone give me a hand? Been struggeling with this problem for some time now. Thanks in advance :)

This is my login activity: package com.hdtchat import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast import androidx.lifecycle.lifecycleScope import com.hdtchat.data.User import com.hdtchat.data.Userdatabase import com.hdtchat.databinding.ActivityLoginBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class Login : AppCompatActivity() { private lateinit var binding: ActivityLoginBinding private lateinit var appDb: Userdatabase private lateinit var button: Button //private var email = binding.edtEmail.text.toString() //private var password = binding.edtPassword.text.toString() //private lateinit var btn_sign_up: Button 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()) { lateinit var user: User lifecycleScope.launch { appDb.dao().getUser() } Toast.makeText(this@Login, "Login Success", Toast.LENGTH_SHORT).show() val intent1 = Intent(this, Home::class.java) startActivity(intent1) }else { Toast.makeText(this@Login, "Login Failed", Toast.LENGTH_SHORT).show() } } }

This is my UserViewModel:

package com.hdtchat.data import android.app.Application import android.content.Context import android.provider.ContactsContract.CommonDataKinds.Email import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch //The ViewModel role is to provide data to the UI and survive configuration changes. A viewmodel acts as a communication center between the repository and the UI. class UserViewModel(application: Application): AndroidViewModel(application) { private val readAllData: LiveData> private val repository: UserRepository init { val userDao = Userdatabase.getDatabase(application).dao() repository = UserRepository(userDao) readAllData = repository.readAllData } fun addUser(user: User) { viewModelScope.launch(Dispatchers.IO) { repository.addUser(user) } } }

This Is my UserRepository:

package com.hdtchat.data import android.provider.ContactsContract.CommonDataKinds.Email import androidx.lifecycle.LiveData import com.hdtchat.data.Dao import com.hdtchat.data.User //the UserRepository class will abstract access to multiple data sources. The repostory is not part of the architecture component libraries, but is a suggested best practice for code separation and architecture class UserRepository(private val userDao: Dao) { val readAllData: LiveData> = userDao.readAllData() suspend fun addUser(user: User) { userDao.addUser(user) } suspend fun verifyUser(edtEmail: String, edtPassword: String): User { return userDao.getUser(edtEmail = edtEmail, edtPassword = edtPassword) } }

This is the DAO:

package com.hdtchat.data import androidx.lifecycle.LiveData import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query //I denne filen s inneholder alle metodene'ene/queries som skakl bli brukt. @Dao interface Dao { @Insert(onConflict = OnConflictStrategy.IGNORE) //Denne her gjr det snn ingen kan bruke ett brukernavn som allerede finnes. suspend fun addUser(user: User) @Query(value = "SELECT * FROM Bruker_Tabell ORDER BY id ASC") fun readAllData(): LiveData> @Query("SELECT email FROM Bruker_Tabell where email LIKE :edtEmail AND password LIKE :edtPassword") fun getUser(edtEmail: String, edtPassword: String): User } This is the main activity:
package com.hdtchat import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.hdtchat.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }

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!