I'm making a signup activity for an app and something weird is happening can you help me?
this is the code for signup some on and it works if I comment the signOut function but I need it to signout after saving the data in the FirebaseAuth and the Firestore.
I don't get even the logs.
public void registUser() {
mAuth = FirebaseAuth.getInstance();
mAuth.createUserWithEmailAndPassword(user.getEmail(), user.getPassword()).addOnCompleteListener(SignUpUserActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(SignUpUserActivity.this, "Sucesso", Toast.LENGTH_SHORT).show();
// Sign in success, update UI with the signed-in user's information
//FirebaseUser user = mAuth.getCurrentUser();
user.setId(task.getResult().getUser().getUid());
user.save();
mAuth.signOut();
finish();
} else {
String erro = "";
try {
throw task.getException();
} catch (FirebaseAuthWeakPasswordException e) {
erro = "Digite uma senha mais forte";
} catch (FirebaseAuthInvalidCredentialsException e) {
erro = "Email invalido";
} catch (FirebaseAuthUserCollisionException e) {
erro = "Email Registado";
} catch (Exception e) {
erro = "Erro generico";
e.printStackTrace();
}
Toast.makeText(SignUpUserActivity.this, erro, Toast.LENGTH_SHORT).show();
}
}
});
}
I think its because it takes to long to save things in the database.
I have no problems with the Auth part.
Thank you.
this is the save function.
public void save(){
DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
Map<String, Object> dataToSave = new HashMap<String, Object>();
dataToSave.put("Name", name);
dataToSave.put("email", email);
dataToSave.put("clinic", clinic);
dataToSave.put("Job", job);
reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("artur", "Saved");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w("artur", "Not Saved",e);
}
});
}
To sovle this, please use the following code:
user.setId(task.getResult().getUser().getUid());
userRef.set(user).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
mAuth.signOut();
}
});
When you sign-out, you need to be sure that your write operation is done and this can only be achieved using addOnSuccessListener.
public void save(){
DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
Map<String, Object> dataToSave = new HashMap<String, Object>();
dataToSave.put("Name", name);
dataToSave.put("email", email);
dataToSave.put("clinic", clinic);
dataToSave.put("Job", job);
reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("artur", "Saved");
mAuth = FirebaseAuth.getInstance();
mAuth.signOut();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w("artur", "Not Saved",e);
}
});
}
Related
I wanna register a user in firebase firestore but when I click the register button nothing happens. I don't understand where I went wrong . It doesn't even create the user or send the data to the database
fStore = FirebaseFirestore.getInstance();
signi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String mail = regemail.getText().toString().trim();
String pin = pass.getText().toString().trim();
String confarm = confir.getText().toString().trim();
String phon = tele.getText().toString().trim();
fAuth.createUserWithEmailAndPassword(mail, pin).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(registrations.this, "Registration Successful", Toast.LENGTH_SHORT).show();
Map<String, Object> user = new HashMap<>();
user.put("Email", mail);
user.put("Password", pin);
user.put("Phone", phon);
df.collection("users")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
startActivity(new Intent(registrations.this, login.class));
}
else {
Toast.makeText(registrations.this, "Registration Failed!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();```
It was working before. I was able to create user and sign in but moving that data to the database is the problem. Also I had used realtime database before and it worked but it was overwtiting the data in the database when I tried to register a new user so I decided to use firestore instead
I want to get and delete all the documents in a subcollection called "journal" but it doesn't work and did not show any toast message or errors.
It would be very helpful if someone tells me what is wrong. Thank you a lot.
This is the code I use to get and delete all the documents in a subcollection:
final String userid = FirebaseAuth.getInstance.getCurrentUser().getUid();
FirebaseFirestore. getInstance().collection("main").document(userid).collection("journal").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (final QueryDocumentSnapshot document : task.getResult()) {
db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(SettingsActivity.this, "error getting: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
Firestore structure :
collection(main) ---> document(userid)---> collection(journal)---> document 1
---> document 2
--> document 3
Try this.
final String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore.getInstance().collection("main").document(userid).collection("journal").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot document: queryDocumentSnapshots.getDocuments()) {
db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
});
Also, please make sure you have set your db rules to allow delete event
I just want to update the count table one time but it keeps updating in the infinite loop can someone help me with this.
referral_code=referral_editText.getText().toString();
db.collection(REFERRAL_CODE_KEY).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
assert queryDocumentSnapshots != null;
for(DocumentSnapshot documentSnapshots:queryDocumentSnapshots){
if(referral_code.equals(documentSnapshots.get(REFERRAL_CODE).toString()))
{
count = parseInt(String.valueOf(documentSnapshots.get(REFERRAL_COUNT)))+1;
Map<String, Object> referralCountDocData = new HashMap<>();
referralCountDocData.put(REFERRAL_COUNT,count);
klog.d("### referral",count+"");
db.collection(REFERRAL_CODE_KEY)
.document(documentSnapshots.getId())
.update(referralCountDocData).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
finish();
}
});
}
}
}
});
Maybe, you could do something like this not everythng is perfect but it gives you a nice idea.
oldCount-> you can read it from document.getData()
var docRef = db.collection(REFERRAL_CODE_KEY).doc(referral_code);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
//read oldCount here from document.getData()
Map<String, Object> docData= new HashMap<>();
docData.put(REFERRAL_COUNT, oldcount + 1);
docRef.set(docData)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
You can detach your listener, so that callback stop getting called.
As per official document https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener You need to use remove method to detach listener
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(
new EventListener<QuerySnapshot>() {
// ...
});
// ...
// Stop listening to changes
registration.remove();
i tried to do everything correctly and can't seem to find what's wrong here, i even created the project again from scratch but still it doesn't work, but i get the "failed" toast when trying to create an account, i added the internet permission too. i also don't get any error in logcat to show it here, how can this be solved ?
public class CreateAccountActivity extends AppCompatActivity {
private Button btnCreateAcc;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser currentUser;
// firestore
private FirebaseFirestore database = FirebaseFirestore.getInstance();
private CollectionReference collectionReference = database.collection("Users");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
firebaseAuth = FirebaseAuth.getInstance();
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
currentUser = firebaseAuth.getCurrentUser();
if (currentUser != null) {
} else {
}
}
};
btnCreateAcc = findViewById(R.id.create_acct_button);
btnCreateAcc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(etEmail.getText().toString()) &&
!TextUtils.isEmpty(etPassword.getText().toString()) &&
!TextUtils.isEmpty(etUserName.getText().toString())) {
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();
String username = etUserName.getText().toString();
createUserEmailAccount(email, password, username);
} else {
Toast.makeText(CreateAccountActivity.this, "Please fill in all fields"
, Toast.LENGTH_SHORT).show();
}
}
});
}
private void createUserEmailAccount(String email, String password, final String username) {
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) &&
!TextUtils.isEmpty(username)) {
progressBar.setVisibility(View.VISIBLE);
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
currentUser = firebaseAuth.getCurrentUser();
assert currentUser != null;
final String currentUserId = currentUser.getUid();
Map<String, String> userObj = new HashMap<>();
userObj.put("userId", currentUserId);
userObj.put("username", username);
collectionReference.add(userObj)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
documentReference.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (Objects.requireNonNull(task.getResult()).exists()) {
progressBar.setVisibility(View.INVISIBLE);
String name = task.getResult()
.getString("username");
Intent intent = new Intent(CreateAccountActivity.this,
PostJournalActivity.class);
intent.putExtra("username", name);
intent.putExtra("userId", currentUserId);
startActivity(intent);
} else {
progressBar.setVisibility(View.INVISIBLE);
}
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(CreateAccountActivity.this, "failed"
, Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(CreateAccountActivity.this, "failed task"
, Toast.LENGTH_SHORT).show();
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(CreateAccountActivity.this, "failed on complete"
, Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "failed else", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onStart() {
super.onStart();
currentUser = firebaseAuth.getCurrentUser();
firebaseAuth.addAuthStateListener(authStateListener);
}
}
i fixed, the problem was i had allow read, write: if false; in firestore
and it needed to be allow read, write: if true;
This could be solved by following good debugging practices.
Meaningful error messages
When you encounter an error and wish to send a toast/alert to the user, send something meaningful to inform them what went wrong. As an example, instead of "failed" or "failed task", use "failed to upload user data" or "failed to create new user".
Each function will normally return a handful of exception classes that can be used to provide a better toast/alert message. Consulting the documentation for createUserWithEmailAndPassword(email, password) you can see what exceptions are thrown and use instanceof to determine the cause of the problem. For example, if e instanceof FirebaseAuthInvalidCredentialsException was true, you could toast "failed to create new user: invalid email".
While this seems tedious, it will save head-scratching later when a user encounters a problem and sends you a bug report/email about it. These steps will help you find any issues without needing access to logs for trivial problems such as incorrectly filled forms.
Log exceptions
The reason you have no information on what went wrong is because you haven't made use of the exception provided in each onFailure handler (public void onFailure(#NonNull Exception e) { ... }). These handlers provide you with the exception that caused the problem which you can save to the log using Log.e("yourActivityName:yourFunctionName", "short message", e). You can also use e.getMessage() to get information about the thrown error.
In an onComplete(Task<?> task) handler, if task.isSuccessful() returns false, you can find out why it is false by calling Exception e = task.getException() and then log it.
Fail-fast programming
If you ever find that you have an if-else pair where the if section contains lots more code than the else section, it is likely to be a sign that you should flip the condition.
Whilst keeping your code cleaner by using less indentation, it also avoids having to scroll through a long if that probably contains more if and else statements.
For example,
if (!requiredVariable1.isEmpty() && !requiredVariable2.isEmpty()) {
// ...
// many (nested) lines of code
// ...
} else {
Log.e(TAG, "a required variable was empty");
}
if (requiredVariable1.isEmpty() || requiredVariable2.isEmpty()) {
Log.e(TAG, "a required variable was empty");
return;
}
// ...
// many lines of code
// ...
Example
As an example of applying these changes, I have made edits to the code you provided applying fail-fast techniques, simplifying error handling, logging exceptions, using OnSuccessListener and OnFailureListener instead of OnCompleteListener where appropriate,
private void createUserEmailAccount(String email, String password, final String username) {
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(username)) {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(#NonNull Task<AuthResult> task) {
currentUser = firebaseAuth.getCurrentUser();
assert currentUser != null;
final String currentUserId = currentUser.getUid();
Map<String, String> userObj = new HashMap<>();
userObj.put("userId", currentUserId);
userObj.put("username", username); // unknown source for variable: username
collectionReference.add(userObj)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
documentReference.get() // why redownload from database? you could just use values of "userObj"
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
progressBar.setVisibility(View.INVISIBLE);
if (!task.isSuccessful()
|| !Objects.requireNonNull(task.getResult()).exists()) {
// show a error message?
return;
}
String name = task.getResult()
.getString("username");
Intent intent = new Intent(CreateAccountActivity.this,
PostJournalActivity.class);
intent.putExtra("username", name);
intent.putExtra("userId", currentUserId);
startActivity(intent);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(CreateAccountActivity.this, "failed to add user data"
, Toast.LENGTH_SHORT).show();
Log.e("CreateAccountActivity", "failed to add user data", e); // log error to logcat
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(CreateAccountActivity.this, "failed to create user"
, Toast.LENGTH_SHORT).show();
Log.e("CreateAccountActivity", "failed to create user", e); // log error to logcat
}
});
}
I'm trying to create new data in the firebase realtime database
I tried to use a HashMap and an object, none of them works.
even when I put the OnComplete/OnSuccess/OnFailure listeners I don't receive any data in the log.
private void registerUser(final String displayName, String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser currentUser = mAuth.getCurrentUser();
String uid = currentUser.getUid();
HashMap<String, String> userMap = new HashMap<>();
User user = new User(displayName, "Bonjour!", "par defaut", "def");
userMap.put("name", displayName);
userMap.put("status", "Bonjour!_Je_viens_de_m'inscirire");
userMap.put("image", "par_defaut");
userMap.put("thumb_image", "def");
Log.d(TAG, "onComplete: " + userMap);
Log.d(TAG, "onComplete: " + user);
mDatabaseReference = FirebaseDatabase.getInstance().getReference("Users").child(uid);
mDatabaseReference.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: ");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
});
Log.d(TAG, "onComplete: " + mDatabaseReference);
mRegProgressBar.setVisibility(View.GONE);
Intent maintIntent = new Intent(RegisterActivity.this, MainActivity.class);
maintIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(maintIntent);
finish();
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mRegProgressBar.setVisibility(View.GONE);
Toast.makeText(RegisterActivity.this, "impossible d'inscrire, veuillez ressayer!", Toast.LENGTH_LONG).show();
}
I tried adding this code and it gives me :" not connected " in the log.
//check if connected
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
Log.d(TAG, "connected");
} else {
Log.d(TAG, "not connected");
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.w(TAG, "Listener was cancelled");
}
});
the HashMap contains the actual data but the setValue() method does not work for some reason. When I look in the firebase console there are no changes.
the code responsible for changing the activity from RegisterActivity to MainActivity ran before actually being able to send the data to the database.
to solve the problem I just put the part of the code responsible for changing the activity and all that:
mRegProgressBar.setVisibility(View.GONE);
Intent maintIntent = new Intent(RegisterActivity.this, MainActivity.class);
maintIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(maintIntent);
finish();
inside the OnSuccessListener.
so the final code is:
private void registerUser(final String displayName, String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser currentUser = mAuth.getCurrentUser();
String uid = currentUser.getUid();
HashMap<String, String> userMap = new HashMap<>();
userMap.put("name", displayName);
userMap.put("status", "Bonjour!_Je_viens_de_m'inscirire");
userMap.put("image", "par_defaut");
userMap.put("thumb_image", "def");
mDatabaseReference = mDatabase.getReference("Users").child(uid);
mDatabaseReference.setValue(userMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: ");
mRegProgressBar.setVisibility(View.GONE);
Intent maintIntent = new Intent(RegisterActivity.this, MainActivity.class);
maintIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(maintIntent);
finish();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
});
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mRegProgressBar.setVisibility(View.GONE);
Toast.makeText(RegisterActivity.this, "impossible d'inscrire, veuillez ressayer!", Toast.LENGTH_LONG).show();
}
}
});
thanks for the commenters.