login static method

Future<Session> login(
  1. String username,
  2. String password, {
  3. ScHttpClient? http,
  4. String endpoint = defaultEndpoint,
  5. String appVersion = defaultAppVersion,
  6. String osVersion = defaultOsVersion,
  7. String bundleId = defaultBundleId,
  8. String previewEndpoint = defaultPreviewEndpoint,
})

Tries using the given username and password to log in.

Makes an Auth request (https://github.com/Ampless/Adsignificamus#auth) to the endpoint using http with appVersion, osVersion and bundleId encoded in the request. previewEndpoint is passed onto the raw constructor.

Implementation

static Future<Session> login(
  String username,
  String password, {
  ScHttpClient? http,
  String endpoint = defaultEndpoint,
  String appVersion = defaultAppVersion,
  String osVersion = defaultOsVersion,
  String bundleId = defaultBundleId,
  String previewEndpoint = defaultPreviewEndpoint,
  // TODO: support `pushid`
}) async {
  http ??= ScHttpClient();
  final tkn = await http
      .get(
          // TODO: consider using package:uri here and otherwhere
          '$endpoint/authid'
          '?bundleid=${Uri.encodeComponent(bundleId)}'
          '&appversion=${Uri.encodeComponent(appVersion)}'
          '&osversion=${Uri.encodeComponent(osVersion)}'
          '&pushid'
          '&user=${Uri.encodeComponent(username)}'
          '&password=${Uri.encodeComponent(password)}',
          ttl: Duration(days: 30))
      .then((tkn) {
    final json = jsonDecode(tkn);
    if (json is Map && json.containsKey('Message')) {
      throw DsbException(json['Message']);
    } else if (json == '') {
      throw AuthenticationException();
    }
    return json;
  });
  return Session(tkn,
      endpoint: endpoint, http: http, previewEndpoint: previewEndpoint);
}