Now we will create a function named logInUser(), which will send a POST request to the server and match the username and password. In return of failed, it gets an error and shows the error message, or it will take to to the MainActivity. Here is the function:
private fun logInUser(){
APIClient.profileAPICall(username_input_login.text.toString(), password_input_login.text.toString())
.loginProfile(username_input_login.text.toString(),password_input_login.text.toString() )
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
newUser ->
if(newUser.error != null){
Toast.makeText(applicationContext,newUser.error!!, Toast.LENGTH_SHORT).show()
}else {
PrefUtils.storeUsernameID(this, newUser.userID!!)
PrefUtils.storeUsername(this, newUser.username!!)
PrefUtils.storePassword(this, newUser.password!!)
username_input_login.setText(PrefUtils.getUsername(this))
password_input_login.setText(PrefUtils.getPassword(this))
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
},{
error ->
Toast.makeText(applicationContext,R.string.err_login_msg, Toast.LENGTH_SHORT).show()
Log.wtf("******", error.message.toString())
})
}
Here, we store the username, password, and userID if we get the correct response.