I want to use custom function in my android application for sign up in firebase. Currently I am using the createUserWithEmailAndPassword function. It takes two parameters by default i.e Email & Password but my sign up form includes other attributes also like, phone number, name etc. So what should I do? Currently I am using the below code.
firebaseAuth.createUserWithEmailAndPassword(Email,Password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
progressDialog.cancel();
Toast.makeText(SignUp.this,"Registration Successful",Toast.LENGTH_SHORT).show();
}
else
{
progressDialog.cancel();
Toast.makeText(SignUp.this,"Could Not Register. Try Again.",Toast.LENGTH_SHORT).show();
}
}
});
Thank You :)
Related
I tried to sign in with Firebase. I coded these codes but when I sign in on my app, I take the message from !task.isSuccessful at my code. How can I solve this problem basically? I'm new for the android studio.
I think I could code wrongly in this part when I click sign in button call this function
private void createAccount(){
String email=userEmail.getText().toString();
String password=userPassword.getText().toString();
mAuth.createUserWithEmailAndPassword(email,password).
addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
//when Authentication is successful
if (task.isSuccessful()){
Log.d(TAG,"createUserWithEmail:onComplete"+task.isSuccessful());
Toast.makeText(SignInActivity.this,"Registration was
provided",Toast.LENGTH_SHORT).show();
Intent signIn= new
Intent(SignInActivity.this,UserMainActivity.class);
startActivity(signIn);
}//when Authentication is failed
else {
Toast.makeText(SignInActivity.
this,"Registration is unsuccessful! Please try
again...",Toast.LENGTH_SHORT).show();
}
}
});
}
How can store additional user properties in firebase like user's name, phone number programatically? Do I need to create additional document for every user to store their properties?
I'm creating user using like
userAuth.createUserWithEmailAndPassword(userBO.getUserPhoneNumber(),userBO.getUserPassword()).addOnCompleteListener(
getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//TODO: set user properties here
}
else{
}
}
}
);
You should not use User Properties for saving static data like name, Phone number etc. As this data is specifically used for analytics purpose. Instead I would suggest you to create another document in firebase for storing user specific details.
You can set userName and photo as:-
UserProfileChangeRequest userProfileChangeRequest = new UserProfileChangeRequest.Builder().setPhotoUri(uri).setDisplayName(name).build();
userAuth.getCurrentUser().updateProfile(userProfileChangeRequest).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
}
});
I am using Firebase Auth with the Facebook & Google login. After a successful login, I also register the user on my own server.
// facebook or google auth
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
registerOnServer(task);
}
});
That works fine the first time. But the second time the user logs in, my backend complains, because a user with those credentials already exists.
How can I detect if this is the first time a user logs in via one of the given providers? I can't use SharedPreferes, because they get erased after a user uninstalls the app, which leads to problems after reinstalling.
firebaseAuth.fetchProvidersForEmail(userEmail).
addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() {
#Override
public void onComplete(#NonNull Task<ProviderQueryResult> task) {
List<String> providerList = task.getResult().getProviders();
if (providerList.isEmpty()) {
//Empty List means new user
}
else if (providerList.contains("facebook.com")) {
//Facebook is the provider
}
}
}
Use above code to get providers.
When I am trying to sign up a user using Firebase authentication method in my Android application, this is happening:
It's not registering a user
This is the Registration code
final String _name = txtName.getText().toString().trim();
String _email = txtEmail.getText().toString().trim();
String _pass = txtPass.getText().toString().trim();
if(!TextUtils.isEmpty(_name) && !TextUtils.isEmpty(_email) && !TextUtils.isEmpty(_pass) ) {
mProgress.setMessage("Registrando ...");
mProgress.show();
firAuth.createUserWithEmailAndPassword(_email, _pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
//the user has been registered
}
}
});
You are registring the user but not hiding the progress bar after user successfully registered in the firebase. Try applying this code and check the firebase dashboard for authentication. And also Implement onFailureListener to know the reason why user is not being registered.
firAuth.createUserWithEmailAndPassword(_email, _pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
mProgress.dismis();
if(task.isSuccessful()) {
//the user has been registered
Log.d("onSuccess","USER REGISTERED");
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
mProgress.dismis();
Log.e("onFailure",e.getMessage());
}
}
);
EDIT: If you are still facing the same issue go and check these settings in your firebase console authentication tab ,
Select SIGN-IN METHOD and Check the status of Email/Password if it is Disable then switch it to the Enable.
And it looks like you are using Genymotion, so have you installed the Google Libraries into your virtual device ?
If not follow this thread or test your app on a physical device
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.