Skip to content

Can no longer get credential (of type AuthCredential) from a UserCredential interface #5712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
varbrad opened this issue Nov 9, 2021 · 4 comments

Comments

@varbrad
Copy link

varbrad commented Nov 9, 2021

[REQUIRED] Describe your environment

  • Operating System version: MacOS 11.4
  • Browser version: Firefox 94.0.1
  • Firebase SDK version: 9.1.2
  • Firebase Product: auth

[REQUIRED] Describe the problem

Steps to reproduce:

Calling signInWithEmailAndPassword returns a UserCredential that no longer allows us to pluck the AuthCredential from.

Relevant Code:

const userCredential = await firebaseApp
          ?.auth()
          .signInWithEmailAndPassword(user.email, data.oldPassword)
const authCredential = userCredential?.credential

The above code used to work just fine in Firebase SDK 8. We are attempting to upgrade to Firebase SDK 9, but see no way of getting the credential off the return value of the function in order to trigger the reauthenticateWithCredential function afterwards (Specifically we are implementing a change-password flow here).

I'm unsure if this is a mistake/omission on the interfacebecause the documentation still mentions that the UserCredential interface should expose an AuthCredential, of it the way to do this flow has changed.

Screenshot 2021-11-09 at 09 38 10

@varbrad
Copy link
Author

varbrad commented Nov 9, 2021

For anyone else encountering this. I've been able to work around it via;

const authCred = EmailAuthProvider.credential(email, oldPassword)
const authUser = (await signInWithCredential(this.auth(), authCred)).user
await reauthenticateWithCredential(authUser, authCred)

@sam-gc
Copy link
Contributor

sam-gc commented Nov 9, 2021

With your upgrade, are you using the modular version or the compatibility layer? The modular version does not expose a credential on the UserCredential object; you need to call a provider's credentialFromResult static method (for example, GoogleAuthProvider.credentialFromResult()). For email/password, in v8 that credential field should always be null in any case.

The v9 compatibility layer (i.e. firebase/compat/auth should return a UserCredential with a credential field). If it's not there, then there's a bug

@varbrad
Copy link
Author

varbrad commented Nov 10, 2021

@sam-gc I am attempting to move over to the modular version. I have indeed just decided to use the approach taken above (and is the approach you mentioned), which seems to be working for our application.

If, as you say, the UserCredential object no longer exposes a credential: AuthCredential, then the documentation should be changed to reflect this. The link you provided still mentions that it returns an AuthCredential (as it did in v8).

Also - regarding the credential being null, our password change functionality definitely works (in our v8 implementation) and we've not had any reports from our ~20k users of it ever not working, so weird one there!

Thanks! :)

@sam-gc
Copy link
Contributor

sam-gc commented Nov 11, 2021

Thanks for the note and point out the docs. The linked PR should update the docs. EmailAuthProvider doesn't provide a credentialFromResult method in v9, so I'd still suggest following this method: #5712 (comment).

I'm going to close this one out for now, but please reopen or comment if you're still running into issues.

@sam-gc sam-gc closed this as completed Nov 11, 2021
@firebase firebase locked and limited conversation to collaborators Dec 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants