beamable / BeamableProduct

The beamable product suite including com.beamable, com.beamable.server, microservice base image, portal, the installer, and build scripts
Other
5 stars 0 forks source link

Failed to refresh account in Unity Editor when access token has expired #3617

Open allister-beamable opened 3 weeks ago

allister-beamable commented 3 weeks ago

Describe the Bug

When the access token for a developer in Unity Editor has passed its 10-day expiration, the SDK fails to fully initialize and prints a bunch of errors in the console, including Failed to refresh account and Exception: Service not found IPlatformService. It is possible to solve this by logging out and logging back in, but this is not an ideal user experience.

Steps to reproduce

  1. Log into a Beamable project in Unity Editor
  2. Close your computer and walk away for 11+ days (or have unlucky timing where the 10-day refresh falls on a long weekend or something)
  3. Reopen the project in Unity
  4. Try to do anything that requires Beamable backend access (such as downloading content)

Expected behavior

Actual behavior

Metadata

Additional information

When this situation occurs, developers can resolve it by using Beamable Toolbox to log out and log back in again.

The refresh failure only seems to occur rarely, such as when a developer goes on vacation or other circumstances that lead them to leave the project aside for multiple days; chances are that when developers are interacting with the SDK on a regular workday basis, the SDK is able to preemptively refresh the token before it actually fully expires.

allister-beamable commented 3 weeks ago

When stepping through in a debugger, we found that the exception was thrown when PlatformRequester tries to resolve IPlatformService via dependency injection: https://github.com/beamable/BeamableProduct/blob/1.19.22/client/Packages/com.beamable/Runtime/Core/Platform/SDK/PlatformRequester.cs#L423

cdhanna commented 2 weeks ago

The exception is being thrown from code that is shared between Runtime and Editor. The Runtime properly fills out a Dependency Injectable scope; but the Editor does not. When this error pathway happens in the Editor, the Editor tries to use DI to resolve a dependency that has not been registered.

Instead of using the IPlatformService, we should use the IUserContext, and implement the IUserContext on the Beamable.Editor.AccountService (which is where the editor account info is stored), and then inject the IUserContext into the BeamEditor DI scope.

public long UserId => Account.user.id;