I want a method to login to my Parse account with either my Username OR my Email. The code I have now works only with my username. I want to know a way to identify if the text field has a username or an email.
Here is the code,
private class SignInOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// Get the username and password from the view
final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
final String password = mPasswordEtxt.getText().toString();
if (isFormInputValid(username_email, password)) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
final String email = mUsernameEmailEtxt.getText().toString();
ParseUser.logInInBackground(email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
} else {
ParseUser.logInInBackground(username_email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
findViewById(R.id.error).setVisibility(View.VISIBLE);
}
}
});
}
}
}
}
private class SignInOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// Get the username and password from the view
final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
final String password = mPasswordEtxt.getText().toString();
if (isFormInputValid(username_email, password)) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
final String email = mUsernameEmailEtxt.getText().toString();
ParseUser.logInInBackground(email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
} else {
ParseUser.logInInBackground(username_email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
findViewById(R.id.error).setVisibility(View.VISIBLE);
}
}
});
}
}
}
}
UPDATE: I've put up logs and made some changes (credits: @kevin), detects emails but refuses to login with it.
Here is the code,
private class SignInOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// Get the username and password from the view
final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
final String password = mPasswordEtxt.getText().toString();
final String email = mUsernameEmailEtxt.getText().toString().toLowerCase();
if (isFormInputValid(username_email, password)) {
if (username_email.indexOf('@') != -1) { // HERE!
Log.d("detector", "username_email detected as email:" + email.toString());
ParseUser.logInInBackground(email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
} else {
Log.d("detector", "username_email detected as username:" + username_email.toString());
ParseUser.logInInBackground(username_email, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
findViewById(R.id.error).setVisibility(View.VISIBLE);
Log.d("error", "username or email invalid");
}
}
});
}
}
}
}