Skip to content

Add a resaturation function for auth User instances #4484

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sam-gc
Copy link
Contributor

@sam-gc sam-gc commented Feb 17, 2021

This allows symmetry for the user object:

const user = (await signInAnonymously(auth)).user;
const userString = JSON.stringify(user);
const resaturated = /* new: */ userFromJSON(auth, userString);

This also allows more flexibility in environments whose storage we don't always fully support (see #1874, #4468)

Pending API review.

@changeset-bot
Copy link

changeset-bot bot commented Feb 17, 2021

⚠️ No Changeset found

Latest commit: 5ac8e3b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@sam-gc sam-gc changed the base branch from master to samgho/recaptcha-dom February 17, 2021 20:06
@sam-gc sam-gc requested a review from yuchenshi February 17, 2021 20:06
Base automatically changed from samgho/recaptcha-dom to master February 17, 2021 20:26
@sam-gc sam-gc force-pushed the samgho/user_from_json branch from b86c40d to 5ac8e3b Compare February 17, 2021 20:26
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 17, 2021

Binary Size Report

Affected SDKs

No changes between base commit (c6eadf7) and head commit (4f547b9).

Test Logs

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 17, 2021

Size Analysis Report

Affected Products

  • @firebase/auth-exp

    • userFromJSON

      Size Table

      TypeBase (c6eadf7)Head (4f547b9)Diff
      size
      ?
      28.0 kB
      ? (?)
      size-with-ext-deps
      ?
      38.7 kB
      ? (?)

      Dependency Table

      TypeBase (c6eadf7)Head (4f547b9)Diff
      functions
      ?

      47 dependencies

      _assert
      _castAuth
      _createError
      _emulatorUrl
      _fail
      _getBrowserName
      _getClientVersion
      _getCurrentScheme
      _getFinalTarget
      _getInstance
      _getUserLanguage
      _isAndroid
      _isBlackBerry
      _isChromeIOS
      _isFirefox
      _isHttpOrHttps
      _isIEMobile
      _isOnline
      _isSafari
      _isWebOS
      _logError
      _logoutIfInvalidated
      _parseToken
      _performApiRequest
      _performFetchWithErrorHandling
      _persistenceKeyName
      _prodErrorMap
      _reloadWithoutSaving
      _tokenExpiresIn
      assertStringOrUndefined
      createErrorInternal
      debugAssert
      debugFail
      deleteAccount
      extractProviderData
      getAccountInfo
      getIdTokenResult
      getVersionForPlatform
      isUserInvalidated
      makeTaggedError
      mergeProviderData
      registerAuth
      reload
      requestStsToken
      secondsStringToMilliseconds
      userFromJSON
      utcTimestampToDateString
      

      ?
      classes
      ?

      12 dependencies

      AuthImpl
      AuthInternal
      Delay
      FetchProvider
      InMemoryPersistence
      NetworkTimeout
      PersistenceUserManager
      ProactiveRefresh
      StsTokenManager
      Subscription
      UserImpl
      UserMetadata
      

      ?
      variables
      ?

      11 dependencies

      DEFAULT_API_TIMEOUT_MS
      NETWORK_TIMEOUT
      NETWORK_TIMEOUT_DELAY
      PING_TIMEOUT
      SERVER_ERROR_MAP
      _DEFAULT_AUTH_ERROR_FACTORY
      _POLL_WINDOW_CLOSE_TIMEOUT
      inMemoryPersistence
      instanceCache
      logClient
      version
      

      ?
      enums
      ?
      ?

      External Dependency Table

      ModuleBase (c6eadf7)Head (4f547b9)Diff
      @firebase/app-exp
      ?

      SDK_VERSION
      _registerComponent
      registerVersion
      

      ?
      @firebase/util
      ?

      ErrorFactory
      FirebaseError
      base64Decode
      createSubscribe
      getUA
      isBrowserExtension
      isMobileCordova
      isReactNative
      querystring
      

      ?
      @firebase/logger
      ?

      LogLevel
      Logger
      

      ?
      tslib
      ?

      __rest
      

      ?
      @firebase/component
      ?

      Component
      

      ?

Copy link
Member

@yuchenshi yuchenshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, will approve after API proposal is reviewed and finalized

Copy link
Member

@Feiyang1 Feiyang1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Lint will pass once you merge the branch with master.

@Feiyang1 Feiyang1 assigned sam-gc and unassigned Feiyang1 Feb 24, 2021
@alexcastillo
Copy link

This is exactly what I need. Any workarounds I can use until this is merged?

@smashah
Copy link

smashah commented Nov 2, 2021

Any movement on this PR? I would really like to keep my client-side node apps running in stateless containers authenticated with a first-party solution

@jacksteamdev
Copy link
Contributor

jacksteamdev commented Nov 12, 2021

@sam-gc A feature like this would be extremely useful for MV3 Chrome Extensions, where the background service worker has a short lifespan. In MV3, authentication is crippled, see #5588

If the authentication could be handled on a normal website, and the user data passed to the extension via external messaging, then the extension could authenticate using updateCurrentUser and save the user in extension storage. Then when the service worker restarts, the extension would update the user from storage.

This way, the authentication use case is greatly simplified, and the Chrome extension code is not concerned with authentication itself.

@@ -317,3 +318,7 @@ export class UserImpl implements User {
return user;
}
}

export function userFromJSON(auth: externs.Auth, json: string): externs.User {
return UserImpl._fromJSON(_castAuth(auth), JSON.parse(json));
Copy link
Contributor

@jacksteamdev jacksteamdev Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be helpful if this could take a plain object as well (some storage solutions parse the JSON automatically), for example:

const userObj = typeof json === "string" ? JSON.parse(json) : json;
return UserImpl._fromJSON(_castAuth(auth), userObj);

@bashimr
Copy link

bashimr commented Sep 9, 2024

Was this function 'userFromJSON' ever released? I can't seem to find it in any documentation. As mentioned above, Chrome Extension firebase google authentication is very hard to achieve in the main extension context. The only viable option is to do the authentication offscreen and then use 'updateCurrentUser' to populate the Auth of the main context. But that requires creating a new user from the stingified creds passed to the main context via messaging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants