firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firebase 9 - No fromJSON method on AuthCredential

Open geoffgscott opened this issue 4 years ago • 6 comments

There does not seem to be be a way to create an AuthCredential from a JSON credential representation. The AuthCredential has a toJSON() method but not a fromJSON() method. Ideally a separate credentialFromJson() function could be provided that is separate from the AuthCredential class.

I see the EmailAuthCredential still has this method but I am unclear on the compatibility with a JSON representation that has been created from a standard AuthCredential.

This is critical for handling third party auth provider in Electron workflows.

geoffgscott avatar May 20 '21 01:05 geoffgscott

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar May 20 '21 01:05 google-oss-bot

You're correct, @geoffgscott. It seems that the AuthCredential class of JS SDK version 9 doesn't have the fromJSON method similar to version 8.

Thanks for the feedback. I've tagged this issue as a feature request.

looptheloop88 avatar May 20 '21 13:05 looptheloop88

Hi, thanks for reaching out! AuthCredential is merely a base class that is never instantiated on its own. A fromJSON that works for all subclasses of AuthCredential would require the base class to import and reference all subclasses, which would prevent tree shaking.

Providers that do support recreation in this way should have a credentialFromJSON method. It appears that during a refactor those got removed, so those need to be added back. These will look similar to OAuthProvider.credentialFromJSON. That said, there won't be a generic fromJSON on AuthCredential itself.

sam-gc avatar May 20 '21 17:05 sam-gc

That makes sense. I do see the fromJSON() method on the OAuthCredential and most of the other credential sub classes. I am not sure if this remains just for the compat build or as a secondary way to create credentials from JSON. I do also see the credentialFromJSON() methods on the generic OAuthProvider. This is much better than having specific credentialFromJSON() methods for each OAuthProvider subclass as it you do not need additional checking of which specific OAuth provider made the JSON representation .

A generic function like getOAuthCredentialFromJSON() would be a more clear API than using a the static method on the credential subclass or a provider specific credentialFromJSON(). Alternatively an optional parameter in the constructor to provide JSON would also be quite good.

Something like this: const credential = getOAuthCredentialFromJSON(<JSON_STRING>); or const credential = new OAuthCredential(<JSON_STRING>)

Seems nicer than this: const credential = OAuthCredential.fromJSON(<JSON_STRING>) or const credential = OAuthProvider.credentialFromJSON(<JSON_STRING>)

geoffgscott avatar May 20 '21 18:05 geoffgscott

Hi, thanks for reaching out! AuthCredential is merely a base class that is never instantiated on its own. A fromJSON that works for all subclasses of AuthCredential would require the base class to import and reference all subclasses, which would prevent tree shaking.

Hi, trying to catch up on an issue in Firebase UI not working as expected with Firebase JS SDK 9

What you said makes a lot of sense for the modular version of the SDK, but shouldn't it have been kept on the compat version? As far as I can tell, the fromJSON static function is still declared in the typings but no longer implemented

https://github.com/firebase/firebase-js-sdk/blob/aaeab08ac96133f8ec33ec41517453c566c3fdc3/packages/auth-types/index.d.ts#L147-L152

muryoh avatar Jan 31 '22 18:01 muryoh

Just wanted to +1 this, as this is breaking external implementations like firebaseui-web.

boldtrn avatar Sep 21 '22 07:09 boldtrn

Hello. Any progress on this one? I've noticed issues with linking accounts using the firebase-ui lib. Would be great to get it fixed...

yurist38 avatar Jul 10 '23 12:07 yurist38