I'm currently working on an app that shows users in a listview and when you click on a user, you get to see the details.
Now you can 'add' users by filling in editText field with his or her info. Now I want that only the person himself can add his or her info. I added a editText asking for your email and an editText asking for a password. These credentials should match an account previously created in the app in Firebase. I do not seem to accomplish it.
This is my code:
String email2 = mEmailField.getText().toString();
String pw = mPassword.getText().toString();
firebaseAuth.signInWithEmailAndPassword(email2, pw).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
When the user fills in his email and password everything works, but when the incorrect password is entered, it works as well and that should not happen. What did I do wrong?
Inside OnComplete check if the Authentication was successful.
public void onComplete(#NonNull final Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getContext(), "Authentication Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Authentication failed.",Toast.LENGTH_SHORT).show();
}
}
mAuth.signInWithEmailAndPassword(et_LogIn_Email.getText().toString(),et_LogIn_Pasword.getText().toString())
.addOnCompleteListener((EntryActivity) requireActivity(), new OnCompleteListener<AuthResult>() {
#SuppressLint("SetTextI18n")
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText((EntryActivity) requireActivity(), "LogIn...", Toast.LENGTH_SHORT).show();
loadingDialog.stopLoading();
gotoUiPage();
} else {
Toast.makeText((EntryActivity) requireActivity(), "Error! try again...", Toast.LENGTH_SHORT).show();
tv_LogIn_warning.setText("Enter Carefully!...");
loadingDialog.stopLoading();
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
tv_LogIn_warning.setText(e.getMessage());
}
});
onFailureListener provide an exception by which you handle it.
Related
I don't know if the problem is from the code. After signing up, when i try to log in, it shows "Wrong Credentials or Bad Connection! Try Again", which is the error to be called if the password is wrong or the email id is wrong.
firebaseAuth.signInWithEmailAndPassword(id,pass).addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
String id=editID.getEditText().getText().toString().trim()+"#gmail.com";
db.collection("User").whereEqualTo("email",id).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
User obj=new User();
for(QueryDocumentSnapshot doc:queryDocumentSnapshots)
obj=doc.toObject(User.class);
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
#Override
public void onComplete(#NonNull com.google.android.gms.tasks.Task<String> task) {
try {
String token = task.getResult();
Log.e("DeviceToken = ",token);
}catch (Exception e){
e.printStackTrace();
}
}
});
db.document("User/"+firebaseAuth.getCurrentUser().getEmail()).update("fcmToken",SharedPref.getInstance(getApplicationContext()).getToken())
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
Toast.makeText(SignInActivity.this, "Registered for Notifications Successfully !", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(SignInActivity.this, "Registration for Notifications Failed !\nPlease Sign in Again to Retry", Toast.LENGTH_SHORT).show();
}
}
});
If a Task fails, it contains an exception with additional information in why it failed. You should log that exception:
db.document("User/"+firebaseAuth.getCurrentUser().getEmail()).update("fcmToken",SharedPref.getInstance(getApplicationContext()).getToken())
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful())
{
Toast.makeText(SignInActivity.this, "Registered for Notifications Successfully !", Toast.LENGTH_SHORT).show();
}
else
{
// 👇
Log.e("Auth", "Registration for Notifications Failed", task.getException());
Toast.makeText(SignInActivity.this, "Registration for Notifications Failed !\nPlease Sign in Again to Retry", Toast.LENGTH_SHORT).show();
}
}
});
Once you get the exception, search for the error message to see if others have dealt with the problem before.
I am testing the createUserWithEmailAndPassword feature but It does not seem to be working.
private void createUserAccount() {
String userEmail, userPassword;
userEmail = emailRegisterEditText.getText().toString();
userPassword = passwordRegisterEditText.getText().toString();
if (!TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userPassword)) {
mAuth.createUserWithEmailAndPassword(userEmail, userPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterUserActivity.this, "Account created successfully", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
startActivity(new Intent(RegisterUserActivity.this, InstagramHomeActivity.class));
} else {
Toast.makeText(RegisterUserActivity.this, "Failed to create account", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
});
}
}
I have googled the issue and others have similar problems but they have a toast appear for failure. Neither a toast appears for success or failure and the ProgressDialog does not disappear for me. I am thinking that maybe it's processing or there is an error on the firebase side? Any help is appreciated. Thanks
I have created the Register method using firebase authentication user register method.
How to delete a registered user by using firebase auth and android studio?
private void registerUser(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>(){
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//user successfully registered and logged in
Toast.makeText(MainActivity.this, "Registered Successfully",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
finish();
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
}else{
Toast.makeText(MainActivity.this, "Could Not Register. Please Try Again Later",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
private void deleteUser(){
// TODO: Fill this method
}
You can use delete() method to remove the desired user from the Firebase. Please use this code:
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential authCredential = EmailAuthProvider.getCredential("user#example.com", "password1234");
firebaseUser.reauthenticate(authCredential).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
firebaseUser.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User account deleted!");
}
}
});
}
});
Having created a new account, I automatically enter it.
How do I prevent my account from being created?
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Пользователь добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
May be need workes whis admin sdk?
May be who worked with admin sdk firebase android?
Are there any examples or lessons on working with admin sdk?
For I do not understand how to work with this.
The way I understand Firebase to work is that if the task is successful then Firebase actually actually signs in the user.
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseAuth.getInstance().getCurrentUser();
//Ther will be a current user at this point. This is the way Firebase just works.
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
So you can actually get the user after the task is successful because the user is automatically set if the task is successful. If you would not like there to be any user when the task is successful. You can call
FirebaseAuth.getInstance().signOut();
This will sign out the user and when you call
Firebase.getInstance().getCurrentUser()
This will return nothing, that way there will be no user in your system.
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseAuth.getInstance().getCurrentUser();
//Ther will be a current user at this point. This is the way Firebase just works.
FirebaseAuth.getInstance().signOut();// sign out the user
Firebase.geInstance().getCurrentUser(); // this will now be null
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
I am new to Firebase development. I am creating a user login system with Firebase. I am struggling with verifying emails registered users. Here is the code I wrote.
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.i("Success", "Yes");
}
else{
Log.i("Success", "No");}
}
});
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
}
});
}
});
EDIT: No verification email is triggered when sign up is successful. The error I am getting is
10-12 10:41:47.579 10529-10529/com.firebase I/Success:
Nocom.google.firebase.FirebaseException: An internal error has
occurred. [ USER_NOT_FOUND ]
You need to check the status of createUserWithEmailAndPassword() to ensure that it is successful before getting the current user and calling sendEmailVerification(). There are a number of reasons why createUserWithEmailAndPassword() might fail (account already exists, weak password, malformed email address, etc.). When it does fail, the current user with be the previously signed-in user or null, if there has not been a previous sign-in.
Restructure your code like this:
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.i("Success", "Yes");
}
else{
Log.i("Success", "No");}
}
});
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
}
});
You need to Enable Email & Password Authentication, Configuring Email & Password
In this page you can find many tutorials on the page.
If you are using the new version of firebase, you could try to check your connection... Info here