I am new to android development and I am working on a simple inventory app for a project in school. I have everything set up with a room database for the users. Registering users works just fine but currently the authentication of users is not working. It will let you login no matter what you put as the user name and password. I'm not sure what I am doing wrong. All relevant code is attached.
Here is the user DAO:
public interface UserDAO {
@Query("SELECT * FROM User ORDER BY last_name ASC")
LiveData<List<User>> getAllUsers();
@Query("SELECT * FROM User WHERE User.user_name == :userName AND User.password == :password")
LiveData<User> getUser(String userName, String password);
@Insert
void insertUser(User user);
@Update
void updateUser(User user);
@Delete
void deleteUser(User user);
}
Here is the repository:
public class AppRepository {
private InventoryDAO inventoryDAO;
private UserDAO userDAO;
private LiveData<List<Inventory>> allItems;
private LiveData<List<User>> allUsers;
public AppRepository(Application application) {
AppDatabase appDb = AppDatabase.getDatabase(application);
inventoryDAO = appDb.inventoryDAO();
userDAO = appDb.userDAO();
allItems = inventoryDAO.getAllInventory();
allUsers = userDAO.getAllUsers();
}
public LiveData<List<Inventory>> getAllData() { return allItems; }
public LiveData<List<User>> getAllUsersData() {return allUsers;}
public LiveData<Inventory> get(int id) {
return inventoryDAO.get(id);
}
public LiveData<User> getUser(String userName, String password) {
return userDAO.getUser(userName, password); }
//Insert Method
public void insert(Inventory inventory) {
AppDatabase.databaseWriteExecutor.execute(() -> {
inventoryDAO.insertInventory(inventory);
});
}
//Update Method
public void update(Inventory inventory) {
AppDatabase.databaseWriteExecutor.execute(() -> {
inventoryDAO.updateInventory(inventory);
});
}
//Delete Method
public void delete(Inventory inventory) {
AppDatabase.databaseWriteExecutor.execute(() -> {
inventoryDAO.deleteInventory(inventory);
});
}
//User Insert Method
public void insertUser(User user) {
AppDatabase.databaseWriteExecutor.execute(() -> {
userDAO.insertUser(user);
});
}
//User Update Method
public void updateUser(User user) {
AppDatabase.databaseWriteExecutor.execute(()-> {
userDAO.updateUser(user);
});
}
//User Delete Method
public void deleteUser(User user) {
AppDatabase.databaseWriteExecutor.execute(()-> {
userDAO.deleteUser(user);
});
}
}
Here is the view model:
public class UserViewModel extends AndroidViewModel {
public static AppRepository repository;
public final LiveData<List<User>> allUsers;
//constructor
public UserViewModel (Application application) {
super(application);
repository = new AppRepository((application));
allUsers = repository.getAllUsersData();
}
public LiveData<List<User>> getAllUsers() {return allUsers;}
public LiveData<User> getUser(String userName, String password) {
return repository.getUser(userName, password);}
public static void insertUser(User user) {repository.insertUser(user);}
public static void updateUser(User user) {repository.updateUser(user);}
public static void deleteUser(User user) {repository.deleteUser(user);}
}
Finally, here is the login activity:
public class Login extends AppCompatActivity {
private EditText username;
private EditText password;
private Button login;
private Button forgotPassword;
private Button newUser;
private int userId = 0;
private UserViewModel userViewModel;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userViewModel = new ViewModelProvider.AndroidViewModelFactory(Login.this
.getApplication())
.create(UserViewModel.class);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login = findViewById(R.id.loginBtn);
forgotPassword = findViewById(R.id.forgotPassword);
newUser = findViewById(R.id.newUserBtn);
login.setOnClickListener(view -> {
String userNameText = username.getText().toString();
String userPassword = password.getText().toString();
LiveData<User> user = userViewModel.getUser(userNameText, userPassword);
if (user != null) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent newIntent = new Intent(Login.this, MainActivity.class);
Login.this.startActivity(newIntent);
}
else {
Toast.makeText(getApplicationContext(),
"Error wrong credentials", Toast.LENGTH_SHORT).show();
}
});
newUser.setOnClickListener(view -> {
Intent newIntent = new Intent(Login.this, RegisterUser.class);
Login.this.startActivity(newIntent);
});
}
}
Thanks in advance.
CodePudding user response:
I think when you are trying to return LiveData
from a Query
, the LiveData
itself will never be null, but the value inside could be null. So, to check whether the value is null or not, change it to this:
// Check the LiveData value instead of the LiveData itself
if (user.value != null) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent newIntent = new Intent(Login.this, MainActivity.class);
Login.this.startActivity(newIntent);
} else {
Toast.makeText(getApplicationContext(), "Error wrong credentials", Toast.LENGTH_SHORT).show();
}
CodePudding user response:
I'm not an expert, but i bet it's because of SQL. See if changing == to = helps. This might be helpful
@Query("SELECT * FROM User WHERE User.user_name == :userName AND User.password == :password")
LiveData<User> getUser(String userName, String password);