findOrCreate static method

Future<TransistorAuthorizationToken> findOrCreate(
  1. String orgname,
  2. String username, [
  3. String url = _DEFAULT_TRANSISTOR_URL
])

Returns a JSON Web Token (JWT) suitable for Authorization with the Transistor Software demo server at https://tracker.transistorsoft.com.

This token is typically provided to Config.transistorAuthorizationToken when first configuring the SDK with BackgroundGeolocation.ready.

Params

@param {String} orgname

Represents a "company" or "organization"; a container for posting locations from multiple devices to the same account. orgname is used for accessing your device results in web app, eg: https://tracker.transistorsoft.com/my-organization-name.

@param {String} username

Appended to the DeviceInfo.model as a means of creating a consistent and unique device identifier. For example:

  • Pixel 3a-my-username
  • A310-my-username
  • iPhone 11,3-my-username

@param {String} url [https://tracker.transistorsoft.com]

The server to register with and receive authentication tokens from. Defaults to https://tracker.transistorsoft.com. If you have a local instance of background-geolocation-console running on your localhost, you would provide the ip address of your server, eg: http://192.168.0.100:9000.


When the SDK receives the TransistorAuthorizationToken from url, it will be cached in persistant-storage within the native code. If the SDK doesn't find a cached token on the client, it will automatically register for one from url, using the provided orgname and username. Otherwise, the cached token will be immediately returned.

If the server refuses the registration (HTTP 403), the returned Future completes with an Error whose code is 403. Any other failure, such as no network connection, returns a token whose accessToken is DUMMY_TOKEN.

Example

String orgname = "my-company-name";
String usernmae = "my-username";

// Fetch an authoriztion token from server.  The SDK will cache the received token.
TransistorAuthorizationToken token = await
  TransistorAuthorizationToken.findOrCreate(orgname, username);

BackgroundGeolocation.ready(Config(
  transistorAuthorizationToken: token
))

ℹ️ See also:

Implementation

static Future<TransistorAuthorizationToken> findOrCreate(
    String orgname, String username,
    [String url = _DEFAULT_TRANSISTOR_URL]) async {
  Completer completer = new Completer<TransistorAuthorizationToken>();
  _methodChannel.invokeMethod(
      'getTransistorToken', [orgname, username, url]).then((dynamic data) {
    completer.complete(TransistorAuthorizationToken(data[FIELD_ACCESS_TOKEN],
        data[FIELD_REFRESH_TOKEN], data[FIELD_EXPIRES], url));
  }).catchError((error) {
    print("[TransistorAuthorizationToken findOrCreate] ERROR: $error");
    // (WO-064) Complete on every branch: the caller holds only completer.future, so a throw here
    // left it waiting forever.  Not every error is a PlatformException (MissingPluginException).
    if (error is PlatformException && error.code == "403") {
      completer.completeError(Error(error));
    } else {
      completer.complete(
          TransistorAuthorizationToken(_DUMMY_TOKEN, _DUMMY_TOKEN, -1, url));
    }
  });
  return completer.future as FutureOr<TransistorAuthorizationToken>;
}