I am new to android so there is some problem when i run the code to register or login with firebase authentication i can run the code but when i execute the signup function after pressing the register button it takes a lot time to register 1 account.
`
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
private Button btn_register;
private EditText et_name, et_age, et_email, et_password;
private ProgressBar ProgressBar;
private TextView name_project;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
name_project = (TextView) findViewById(R.id.name_project);
name_project.setOnClickListener(this);
btn_register = (Button) findViewById(R.id.btn_register);
btn_register.setOnClickListener(this);
et_name = (EditText) findViewById(R.id.et_name);
et_age = (EditText) findViewById(R.id.et_age);
et_email = (EditText) findViewById(R.id.et_email);
et_password = (EditText) findViewById(R.id.et_password);
ProgressBar = (ProgressBar) findViewById(R.id.progressBar);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.name_project:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.btn_register:
registerUser();
break;
}
}
private void registerUser() {
String email = et_email.getText().toString().trim();
String password = et_password.getText().toString().trim();
String name = et_name.getText().toString().trim();
String age = et_age.getText().toString().trim();
if (name.isEmpty()) {
et_name.setError("Please enter your full name");
et_name.requestFocus();
return;
}
if (age.isEmpty()) {
et_age.setError("Please enter your age");
et_age.requestFocus();
return;
}
if (email.isEmpty()) {
et_email.setError("Please enter your email");
et_email.requestFocus();
return;
}
if (password.isEmpty()) {
et_password.setError("Please enter your password");
et_password.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
et_email.setError("Please enter valid email");
et_email.requestFocus();
return;
}
if (password.length() < 6) {
et_password.setError("The length of password should be more than 6 letters");
et_password.requestFocus();
return;
}
ProgressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(RegisterActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();
ProgressBar.setVisibility(View.GONE);
} else {
Toast.makeText(RegisterActivity.this, "Registered fail", Toast.LENGTH_SHORT).show();
ProgressBar.setVisibility(View.GONE);
}
}
});
}
}
enter image description here`
I have tried many methods but registration takes a long time.
Please make sure you have enabled the Email/Password provider in the Authentification tab on the Firebase console.
Related
I have been able to get the email sent and verified but fire base isn't updating. Also when I check to reset the password the email is sent reset and fire base isn't changing that either. How can I change it to where the password and the email verification will update in fire base?
I have searched several sites and looked at several videos. I am really new to coding. Thanks for the help.
this is my login:
public class Login extends AppCompatActivity implements View.OnClickListener {
//Variables
private Button register, signin, forgotpassword;
private TextInputLayout Email, Password;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
register = findViewById(R.id.register);
register.setOnClickListener(this);
signin = (Button)findViewById(R.id.SignIn);
signin.setOnClickListener( this );
Email = findViewById( R.id.email );
Password = findViewById( R.id.password );
mAuth = FirebaseAuth.getInstance();
forgotpassword = (Button) findViewById(R.id.forgot_password);
forgotpassword.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register:
startActivity(new Intent(this, SignUp.class));
break;
case R.id.SignIn:
startActivity( new Intent(this,UserInterface.class) );
break;
case R.id.forgot_password:
startActivity( new Intent(this,ForgotPass.class) );
break;
}
}
private void userlogin() {
String email = Email.getEditText().getText().toString().trim();
String password = Password.getEditText().getText().toString().trim();
if (email.isEmpty()){
Email.setError("Email is required!");
Email.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Email.setError("Please provide valid Email!");
Email.requestFocus();
return;
}
if (password.isEmpty()){
Password.setError("Password is required!");
Password.requestFocus();
return;
}
if (password.length() < 6){
Password.setError("Min password length is 6 characters!");
Password.requestFocus();
return;
}
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener( new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.isEmailVerified()){
startActivity(new Intent(Login.this, UserInterface.class));
}else{
user.sendEmailVerification();
Toast.makeText(Login.this, "Check your email to verify your account!", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(Login.this, "Failed to login! Please check Email and Password", Toast.LENGTH_LONG).show();
}
}
});
}
}
This is my forgot password
public class ForgotPass extends AppCompatActivity {
private Button reset_password;
private TextInputLayout fullname, email;
FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView( R.layout.activity_forgot_pass );
email = findViewById(R.id.email);
fullname = findViewById(R.id.fullname);
reset_password = (Button) findViewById(R.id.fp_submit);
auth = FirebaseAuth.getInstance();
reset_password.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
fp_submit();
}
} );
}
private void fp_submit(){
String email = this.email.getEditText().getText().toString().trim();
String fullname = this.fullname.getEditText().getText().toString().trim();
if (fullname.isEmpty()){
this.fullname.setError( "Name is required" );
this.fullname.requestFocus();
return;
}
if (email.isEmpty()){
this.email.setError( "Email is required" );
this.email.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher( email ).matches()){
this.email.setError( "Please priovide valid email" );
this.email.requestFocus();
return;
}
//progressBar.setVisibility(View.VISIBLE)
auth.sendPasswordResetEmail( email ).addOnCompleteListener( new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText( ForgotPass.this, "Check your email to reset your password",Toast.LENGTH_LONG ).show();
startActivity( new Intent(ForgotPass.this, Login.class) );
}else{
Toast.makeText( ForgotPass.this, "Please try again!", Toast.LENGTH_LONG ).show();
}
}
} );
}
}
This is my sign up
public class SignUp extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
private TextInputLayout FullName, UserName, Email, Password;
private Button Submit, login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sign_up);
mAuth = FirebaseAuth.getInstance();
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
Submit = (Button) findViewById(R.id.submit);
Submit.setOnClickListener(this);
FullName = findViewById(R.id.fullname);
UserName = findViewById(R.id.username);
Email = findViewById(R.id.email);
Password = findViewById(R.id.password);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
startActivity(new Intent(this, Login.class));
break;
case R.id.submit:
startActivity( new Intent(this,Login.class));
break;
}
}
private void submit(){
String email = Email.getEditText().getText().toString().trim();
String password = Password.getEditText().getText().toString().trim();
String fullName = FullName.getEditText().getText().toString().trim();
String username = UserName.getEditText().getText().toString().trim();
if (fullName.isEmpty()){
FullName.setError("Full Name is required!");
FullName.requestFocus();
return;
}
if (username.isEmpty()){
UserName.setError("User Name is required!");
UserName.requestFocus();
return;
}
if (password.isEmpty()){
Password.setError("Password is required!");
Password.requestFocus();
return;
}
if (email.isEmpty()){
Email.setError( "Email is required!" );
Email.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Email.setError("Please provide valid Email!");
Email.requestFocus();
return;
}
if (password.length() < 6){
Password.setError("Min password length should be six characters!");
Password.requestFocus();
return;
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
User user = new User (fullName,email,username,password);
FirebaseDatabase.getInstance().getReference("user")
.child( FirebaseAuth.getInstance().getCurrentUser().getUid() )
.setValue( user ).addOnCompleteListener( new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText( SignUp.this, "Successfully registered", Toast.LENGTH_LONG ).show();
if (task.isSuccessful()){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.isEmailVerified()){
startActivity(new Intent(SignUp.this, Login.class));
}else{
user.sendEmailVerification();
Toast.makeText(SignUp.this, "Check your email to verify your account!", Toast.LENGTH_LONG).show();
}
}startActivity(new Intent(SignUp.this,Login.class));
}else{
Toast.makeText( SignUp.this,"Unable to register!", Toast.LENGTH_LONG ).show();
}
}
} );
}else{
Toast.makeText( SignUp.this,"Unable to register!", Toast.LENGTH_LONG ).show();
}
}
} );
} }
This question already has answers here:
Firebase: setting additional user properties
(2 answers)
Closed 3 years ago.
I want to add some extra flied to user when he signup, the method does not let me do it.
I want to add username, city, phone, as extra parameter
public class SignupActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword,inputUserName,inputCity,inputPhone_Num;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputUserName = (EditText) findViewById(R.id.username);
inputCity = (EditText) findViewById(R.id.city);
inputPhone_Num = (EditText) findViewById(R.id.phone);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
btnResetPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = inputUserName.getText().toString().trim();
String city = inputCity.getText().toString().trim();
String phone = inputPhone_Num.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(username)) {
Toast.makeText(getApplicationContext(), "Enter username address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(city)) {
Toast.makeText(getApplicationContext(), "Enter city address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(phone)) {
Toast.makeText(getApplicationContext(), "Enter phone numer address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
here I want to add username, city, phone, as extra parameter
auth.createUserWithEmailAndPassword**(email, password)**
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, ProfileActivity.class));
finish();
}
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
If you want to add username/City/address then you need to use your own implementation. The method createuserwithEmailAndPassword adds a user to the Firebase authentication console. Also it will give you a unique ID for each user. Please check the following:
https://firebase.google.com/docs/auth/android/manage-users
If you want to add those extra info, then it is better to use firebase database with firebase authentication.
I am beginner and building register and login activities using firebase
I show login activity first. It has clickable text view which reads as "No account? Click here to register" when clicked it starts registration activity and allows user to register but after registering it needs to start login activity but it is not happening
Below is Registration.java
public class Registration extends AppCompatActivity {
private EditText etname, etmail, etpass;
private Button btnreg;
FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
etname = (EditText) findViewById(R.id.etRemail);
etmail = (EditText) findViewById(R.id.etRemail);
etpass = (EditText) findViewById(R.id.etRpass);
btnreg = (Button) findViewById(R.id.btnRreg);
progressDialog = new ProgressDialog(this);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() != null) {
Intent in = new Intent(getApplicationContext(), MainActivity.class);
startActivity(in);
finish();
}
}
public void user_reg_info(View view) {
String uname = etname.getText().toString();
String uemail = etmail.getText().toString();
String upass = etpass.getText().toString();
try {
if (uname.isEmpty() || uname.trim().equals("")) {
etname.setError("Name cannot be empty!");
return;
}
if (uemail.isEmpty() || uemail.trim().equals("")) {
etmail.setError("Email cannot be empty!");
return;
}
if (upass.isEmpty() || upass.equals("")) {
etpass.setError("Password cannot be empty!");
return;
}
progressDialog.setMessage("Registering..please wait!");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(uemail, upass)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Toast.makeText(Registration.this, "Registration successful", Toast.LENGTH_LONG).show();
Intent i = new Intent(Registration.this,Login.class);
startActivity(i);
finish();
} else {
Log.e("Error", task.getException().toString());
Toast.makeText(Registration.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
On click of register button it directly starts MainActivity
Below is Login.java
public class Login extends AppCompatActivity {
private EditText etmail,etpass;
private TextView clktoreg;
private ImageView imgLogin;
private Button btnLog;
private ProgressDialog progressDialog;
FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etmail = (EditText) findViewById(R.id.eTLemail);
etpass = (EditText) findViewById(R.id.eTLpass);
clktoreg = (TextView) findViewById(R.id.click_2_reg);
imgLogin = (ImageView) findViewById(R.id.imgLogin);
btnLog = (Button) findViewById(R.id.btnLogin);
progressDialog = new ProgressDialog(this);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()!=null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
}
public void login_user(View view) {
String email = etmail.getText().toString();
String pass = etpass.getText().toString();
try {
if (email.isEmpty() || email.trim().equals("")) {
etmail.setError("E-mail cannot be empty!");
return;
}
if (pass.isEmpty() || pass.trim().equals("")) {
etpass.setError("Password cannot be empty!");
return;
} else {
progressDialog.setMessage("Logging in..please wait!");
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Intent in = new Intent(getApplicationContext(), MainActivity.class);
startActivity(in);
finish();
} else {
etmail.setError("Check email ID");
etpass.setError("Check password");
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void not_registered(View view) {
Intent in = new Intent(this,Registration.class);
startActivity(in);
}
}
I explain what your app does.
You register the user then the user is not null anymore, login activity is open and you have that in login activity:
if(firebaseAuth.getCurrentUser()!=null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
Because the user is not null anymore when the login activity start and reach this condiction the mainActivity starts.
Firebase stores the email and password.
I need to add username to registration.
I also need to authenticate (Login) Users with Username/Password NOT Email/Password.
Registration should have username, email and password...
While Login should be with username and password.
RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignUp;
private ProgressBar progressBar;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
auth = FirebaseAuth.getInstance();
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(RegisterActivity.this, WelcomeActivity.class));
finish();
}
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnLogin, btnReset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, WelcomeActivity.class));
finish();
}
setContentView(R.layout.activity_login);
inputEmail = (EditText) findViewById(R.id.login_email);
inputPassword = (EditText) findViewById(R.id.login_password);
progressBar = (ProgressBar) findViewById(R.id.login_progressBar);
btnLogin = (Button) findViewById(R.id.btn_login);
btnReset = (Button) findViewById(R.id.btn_reset_password);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resetPassword();
//startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
You can map username to email. For ex, by creating a table of username and email in db. Then, when user enters username to login, fetch corresponding email and use it to perform simple email - password login.
See this Username authentication instead of email
Alternative is to create a server with a Database instead of Firebase.
I am trying to make registration to a App in Firebase, but it keeps on saying Unsuccessful.
Here is my code.
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etEmail;
private EditText etPassword;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() != null){
//Start profile activity here.
finish();
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}
progressDialog = new ProgressDialog(this);
etEmail = (EditText) findViewById(R.id.editTextEmail);
etPassword = (EditText) findViewById(R.id.editTextPassword);
Button btnRegister = (Button) findViewById(R.id.buttonRegister);
assert btnRegister != null;
btnRegister.setOnClickListener(this);
}
#Override
public void onClick(View v) {
registerUser();
}
private void registerUser(){
String email = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
//Email is empty. Create a toast to enter a email.
Toast.makeText(this, "Please enter your email address.", Toast.LENGTH_SHORT).show();
// return to the email field.
return;
}
if (TextUtils.isEmpty(password)){
//Password is empty. Create a toast to enter a password.
Toast.makeText(this, "Please enter your password.", Toast.LENGTH_SHORT).show();
// return to the password field.
return;
}
progressDialog.setMessage("Registration on Process...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
//User is successfully registered.
Toast.makeText(RegisterActivity.this, "Registration is successful.", Toast.LENGTH_SHORT).show();
//Log in and User will start profile activity.
finish();
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}else{
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, "Registration failed. Please try again.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
I tried Login Activity with an email, that I manually created in FirebaseAuth and it was successful. But the registration activity is regularly unsuccessful.
I couldn't find any error in the code. Can you help me please?