I am currently registering users on Android with an email, password and a username. I currently have the identifier set to the email. The issue is, when I register a new user, the user can register with a username that is already in use. If I change the identifier to the username, I get the same problem with the user being able to use an email that is already in use. How can I modify my code to make it so that if the user is trying to register with a username that is already in use, it will let the user know that the username is already taken? Here is all of my code for registration:
BaseActivity.java:
public class BaseActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
@Override
public void onDestroy() {
super.onDestroy();
hideProgressDialog();
}
}
CreateAccountActivity.java:
public class CreateAccountActivity extends BaseActivity {
private EditText inputUsername;
private EditText inputEmail;
private EditText inputPassword;
private Button buttonCreateAccount;
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFields();
}
@Override
public void afterTextChanged(Editable s) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
inputUsername = (EditText) findViewById(R.id.createAccountUsernameInput);
inputEmail = (EditText) findViewById(R.id.createAccountEmailInput);
inputPassword = (EditText) findViewById(R.id.createAccountPasswordInput);
inputUsername.addTextChangedListener(textWatcher);
inputEmail.addTextChangedListener(textWatcher);
inputPassword.addTextChangedListener(textWatcher);
buttonCreateAccount = (Button) findViewById(R.id.createAccountButton);
TextView textViewLogin = (TextView) findViewById(R.id.loginLink);
buttonCreateAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createUser();
}
});
assert textViewLogin != null;
textViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateAccountActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
private void createUser() {
showProgressDialog();
BackendlessUser user = new BackendlessUser();
user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
user.setEmail(inputEmail.getText().toString().trim());
user.setPassword(inputPassword.getText().toString());
Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser backendlessUser) {
hideProgressDialog();
Toast.makeText(CreateAccountActivity.this, Defaults.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
hideProgressDialog();
String errorCode = backendlessFault.getCode();
String errorMessage;
switch (errorCode) {
case "3040":
errorMessage = BackendSettings.ERROR_3040;
break;
case "3033":
errorMessage = BackendSettings.ERROR_3033;
break;
default:
errorMessage = "An unknown error occurred. Try again.";
}
Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
}
});
}
private void enableButton() {
buttonCreateAccount.setEnabled(true);
}
private void disableButton() {
buttonCreateAccount.setEnabled(false);
}
private void checkFields() {
String username = inputUsername.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString();
if (username.isEmpty() || email.isEmpty() || password.isEmpty()) {
disableButton();
} else {
enableButton();
}
}
}