I am trying to connect to the firebase database with my android app and i cannot use "path" keyword to get reference. It underlined it. I don't know why I am beginner to the Android and I have never used the firebase that why I don't know much about firebase database. Here is the code
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
User user = new User(fullName, age, email);
FirebaseDatabase.getInstance().getReference( path: "Users")
}
}
});
In Java parameters don't have names, but instead are passed in the order in which they are needed. So:
FirebaseDatabase.getInstance().getReference("Users")
Related
I am a newbie in Firebase. Recently I am trying to save users into Firebase but I can't. When I generate the APK and run it onto the real device(Not Emulator), the new user does not add it into the Firebase. Also, no errors shown up. Task executed properly and go to OTPActivity. Here is my complete code:
public void saveUser()
{
firebaseAuth.createUserWithEmailAndPassword(emailAddress.getText().toString(),password.getText().toString()).addOnCompleteListener(ChatUserSignUpActivity.this,new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
FirebaseUser user = firebaseAuth.getCurrentUser();
Intent intent = new Intent(ChatUserSignUpActivity.this, OTPActivity.class);
intent.putExtra("phoneNum",emailAddress.getText().toString());
startActivity(intent);
}
else
{
Toast.makeText(ChatUserSignUpActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
Please help me. Thanks in advance.
When you authenticate your users with email and password, no data is saved into Firestore, nor in the Realtime Database. If you need to save the database in the database, you need to write code for that. So once your user is authenticated, you can get the data from the FirebaseUser object and write it to the database of your choice.
I'm working with Firebase authentication and database.In my project user sign in and sign up with their email and password.I use a model class where i have three variable email,password and name.I can store email in authentication sector and users details in database.
code is given below:
User user = new User(name,mail,password);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
startActivity(new Intent(SignUp.this,Home.class));
finish();
}else {
Toast.makeText(SignUp.this, "Getting error", Toast.LENGTH_SHORT).show();
}
}
Is there any way that i used user name instead of getUid value?like this
To solve this, instead of passing the uid (FirebaseAuth.getInstance().getCurrentUser().getUid()) of the user to the child() method, pass the userName:
FirebaseDatabase.getInstance().getReference("Users").child(name).setValue(user).addOnCompleteListener(/* ... */);
// ^ ^
See I have passed the name instead of the uid.
You can set like below: replace FirebaseAuth.getInstance().getCurrentUser().getUid() with name. It will create node with userName.
user = new User(name,mail,password);
FirebaseDatabase.getInstance().getReference("Users")
.child(name) // replace the getUid() code with name
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
startActivity(new Intent(SignUp.this,Home.class));
finish();
}else {
Toast.makeText(SignUp.this, "Getting error", Toast.LENGTH_SHORT).show();
}
}
Hope it will help you:)
I have one node name users which is populating fine, but I am trying to incorporate a new node events, which I am having trouble with. I have copied exactly what works in the users, but I am clearly doing something wrong. It never goes into the OnCompleteListener. Is there something I am missing?
R.string.dbnode_events ="events"
Events events = new Events();
events.setEvent_key(mEventKey);
events.setEvent_title("");
events.setEvent_date("");
events.setEvent_time("");
events.setEvent_millis("");
events.setEvent_desc("");
events.setEvent_filter("");
events.setGroup_number("");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_events))
.child(mEventKey)
.setValue(events).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(CreateEventActivity.this, "Success", Toast.LENGTH_SHORT);
}
});
More Information/ Example: The top user one creates a node no problem but the events one has yet to create. I hope this might give some more insight.
User user = new User();
user.setName(email.substring(0, email.indexOf("#")));
user.setPhone("1");
user.setProfile_image("");
user.setSecurity_level("1");
user.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
user.setEmail(FirebaseAuth.getInstance().getCurrentUser().getEmail());
user.setStreet_address("");
user.setCity("");
user.setState("");
user.setZip("");
user.setMember_filter("Member");
user.setSmall_group_subscription1("");
user.setSmall_group_subscription2("");
user.setSmall_group_subscription3("");
user.setSmall_group_subscription4("");
user.setSmall_group_subscription5("");
user.setShow_phone("No");
user.setShow_email("Yes");
user.setShow_address("No");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_users))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnCompleteListener(task1 -> {
FirebaseAuth.getInstance().signOut();
redirectLoginScreen();
}).addOnFailureListener(e -> {
FirebaseAuth.getInstance().signOut();
redirectLoginScreen();
Toast.makeText(RegisterActivity.this, "Database Problem ",Toast.LENGTH_SHORT);
});
/////////////////////////
String mEventKey = UUID.randomUUID().toString();
Events events = new Events();
events.setEvent_key(mEventKey);
events.setEvent_title("");
events.setEvent_date("");
events.setEvent_time("");
events.setEvent_millis("");
events.setEvent_desc("");
events.setEvent_filter("");
events.setGroup_number("");
FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.dbnode_events))
.child(mEventKey)
.setValue(events)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(RegisterActivity.this, "Success", Toast.LENGTH_SHORT);
}
});
This is what i have done, probably it will help :D
When you add a class, you have to create a reference like this :
private DatabaseReference Accounts;
And inside the onCreate :
Accounts = FirebaseDatabase.getInstance().getReference("Accounts");
After that, set ur class. I do this for the user :
currentwithID = new Class_user(uID,uSer,matchFound);
And than set it on the node :
uID is the token given by Google
Accounts.child(uID).setValue(currentwithID);
I have done the same things to every node of my database, and it perfectly works.
Try to do that with this code, and tell me if it works :D
I have used the following code after the button clicked event :
mCurrentUser = FirebaseAuth.getInstance.getCurrentUser();
mCurrentUser.delete.addOnCompleteListner(............)
and also used if and else statement for appropriate actions for the task to be done.
But nothing's happening in the application on that click event, also not deleting the firebase user account. Help me regarding this.
thank you.
try with this answer, I had tried with this
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User account deleted.");
}
}
});
I've created a Chat function on my android app, but I wanted my users to be able to create an account. However, I managed to let users create an account using Firebase. But the problem is that it allows you to create accounts with Facebook etc, I choose for email and password. This doesn't allow you to set an username, I believe so. Maybe there's a way to change the UID?
If anyone is able to help me out here, I'd be so thankful!
Again, I'm trying to let the user create an username too.
Have a look at the FirebaseUI implementation of its Email+Password registration flow:
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser firebaseUser = task.getResult().getUser();
Task<Void> updateTask = firebaseUser.updateProfile(
new UserProfileChangeRequest
.Builder()
.setDisplayName(name).build());
updateTask.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
mActivityHelper.dismissDialog();
if (task.isSuccessful()) {
startSaveCredentials(firebaseUser, password);
}
}
});
}
Alternatively, you might simple want to use FirebaseUI, which encapsulates this and many other auth flows.