How to get an username for firebase android - android

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.

Related

cannot resolve the symbol "path"

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")

Firebase Authentication Android : Delete Any Authenticated User [duplicate]

This question already has an answer here:
How to delete a registered user by using firebase auth in android studio?
(1 answer)
Closed 4 years ago.
I have created one application, in that I am managing three levels of users like "Admin", "Sub Admin" and "Customers"
Now I am adding Functionality of "Delete Sub Admin" that I want to remove from Authentication of Firebase.
I found some answers but that is for currentUser only.
I show deleteUser method in docs but I think its removed or deprecated.
Any other solution replaced for this method? Any clue?
Your help would be appreciated, Thanks.
You can use this code to delete an authenticated user:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential("email", "password");
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
user.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "User deleted", Toast.LENGTH_SHORT).show();
}
}
});
}
});
The above code first checks if a user is authenticated or not by re-authenticating, and it deletes the user if the task is successful.

Firebase user delete code not working from application

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.");
}
}
});

How to link Multiple Auth Providers to an Firebase Account on Android?

I referenced the documentation in Firebase page. My problem is in this paragraph:
The call to linkWithCredential will fail if the credentials are already linked to another user account. In this situation, you must handle merging the accounts and associated data as appropriate for your app:
FirebaseUser prevUser = currentUser; currentUser = auth.signInWithCredential(credential).await().getUser(); // Merge
prevUser and currentUser accounts and data // ...
I can't figure out how to add this code into my project. When and where do I put this code
auth.signInWithCredential(credential).await().getUser();
into my java file? Android Studio announced me that it can't resolve await() method.
What should I do to resolve that problem. Thank you in advance!
You should handle this part of code when the linkWithCredentials call fails.
Here is a small example :
mAuth.getCurrentUser().linkWithCredential(credentials).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
FirebaseUser prevUser = currentUser;
try {
currentUser = Tasks.await(auth.signInWithCredential(credential)).getUser();
} catch (Exception e) {
e.printStackStrace();
}
// Merge prevUser and currentUser accounts and data
// ...
} else {
}
}
});
NB: Task.await() doesn't exist anymore you should use Tasks.await(...) static method instead.
Suppose that:
One account per email address setting is enabled on firebase authentication
User are logging in with anonymous authentication
The linkWithCredential usually fails with FirebaseAuthUserCollisionException, it could happen either because this credential is used to login before (you can check by searching this email on firebase console) or this email account is used to login before but with different providers ( such as a user logged in with abc#gmail.com by using google sign in before, now he/she uses abc#gmail.com but with Facebook login ) To handle it, you need 2 steps:
Step 1: link with anonymous account
private void linkWithAnonymousAccount(final AuthCredential credential) {
mFirebaseAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
getFirebaseToken();
} else {
Exception exception = task.getException();
if (exception instanceof FirebaseAuthUserCollisionException) {
signInWithFirebase(credential);
} else {
Utils.showDialogMessage(mContext, task.getException().getLocalizedMessage());
}
}
}
});
}
Step 2: sign with firebase
private void signInWithFirebase(AuthCredential credential) {
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
getFirebaseToken();
} else {
Utils.showDialogMessage(mContext, task.getException().getLocalizedMessage());
}
}
});
}
Google says the step 2 is merging because after signed in, you need to get information of the previous user and put into the new user.
I hope it helpful.

New to firebase auth and android development

I am pretty new to android development and Firebase(the new google one). I want to use the email and password feature, but sign specific users in(the ones in my firebase console). I know this is a pretty broad question, but any help would be much appreciated.
First of all, you should check a documentation for Firebase. You can find on official Firebase website:
1. https://firebase.google.com/docs/auth/android/manage-users
2. https://firebase.google.com/docs/auth/android/password-auth
Then you should check Firebase Authentication samples and look for Email/Password Setup.
After you have created your project and added your app to it, you have to go to the console/auth/sign_in method and enable the email and password authentication. Then you have to create a activity/fragment to represent the login, sign up etc.
On the launcher activity you should have this method to check the state of current user and do the necessary action of user != null or if user == null based on your preferences.
FirebaseAuth.AuthStateListener authStateListener;
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
startActivity(new Intent(LoginPage.this,MainActivity.class));
}
}
};
auth.addAuthStateListener(authStateListener);//this one in onStart
then you can use these 2 methods for signin and create new user respectively
FirebaseAuth auth;
auth = FirebaseAuth.getInstance();
auth.signInWithEmailAndPassword(email , password)
.addOnCompleteListener(LoginPage.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
}else {
}
}
});
auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(SignUpPage.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
}else {
}
}
});
go to all the links provided by mmBs and also see this link for a simple and working example

Categories

Resources