0

I am currently trying to convert an anonymous account into a permanent one using Android Firebase. I am using the email-password provider as a method to create an account. The docs state that you must pass a password parameter into the following code :

val credential = EmailAuthProvider.getCredential(email, password)

My code is the following, I use the FirebaseAuth instance to access the current user which then allows me to access their email however I cannot find a way to access the password:

val credential = EmailAuthProvider.getCredential(mAuth.currentUser.email, password)

Other links I have taken a look at do not improve clarity and cause further confusion.

Firebase Authentication : how to get current user's Password? How to create "credential" object needed by Firebase web user.reauthenticateWithCredential() method? Firebase get current user password

Additionally, the last link states that for security reasons we cannot get the password.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
SibMo
  • 68
  • 1
  • 6

2 Answers2

1

I am currently trying to convert an anonymous account into a permanent one using Android Firebase. I am using the email-password provider as a method to create an account.

From this sentence, I understand that you already have anonymous accounts in your project and you try to convert them into permanent users using user and password.

The docs state that you must pass a password parameter into the following code:

val credential = EmailAuthProvider.getCredential(email, password)

That is a normal requirement. Since your anonymous users can join your app without any credentials, in order to be able to convert them, you need to ask each user for an email and a password. That can be simply done in Android, using two EditText objects.

Once you got the user input, you can call getCredential() and pass those values as arguments. Once did that, you can call:

auth.currentUser!!.linkWithCredential(credential)
                            .addOnCompleteListener(this) { /* ... */ }

Please note that auth.currentUser represents the anonymous users. When you call linkWithCredential() and you pass the credential object as an argument, you're actually converting the anonymous user into a permanent user.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • When you mean use two EditText objects are you referring to customisation? At the moment, when I click a button that says "Create account" it takes me through to the sign-in process that I cannot customise myself, I see the password EditText but I assumed that this was an unreachable part of the code that comes with using Firebase. – SibMo Mar 09 '22 at 16:56
  • *What you mean to use two EditText objects* I mean two fields (added by you) from which you can get user input, basically the email and the password. If you are using other libraries, it's ok, but I recommend you use basic stuff. – Alex Mamo Mar 09 '22 at 20:20
  • 1
    Thanks for the quick responses and your comment pointed me in the correct direction. Cheers – SibMo Mar 10 '22 at 11:48
0

Since the current account is anonymous, it doesn't have an email and password yet.

The common flow is that you first sign in the user anonymously, without asking them for any information. Then later (when the user is ready to sign in explicitly) you ask them for their email and password, use those to create email credentials, and then link them to the anonymous account.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807