findOrCreate static method
Returns a JSON Web Token (JWT) suitable for Authorization with the Transistor Software demo server at http://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: http://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 [http://tracker.transistorsoft.com]
The server to register with and receive authentication tokens from. Defaults to http://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.
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");
if (error.code != "403") {
completer.complete(
TransistorAuthorizationToken(_DUMMY_TOKEN, _DUMMY_TOKEN, -1, url));
} else {
throw Error(error);
}
});
return completer.future as FutureOr<TransistorAuthorizationToken>;
}