setToken method

Future<LoginResponseStatus> setToken({
  1. required String instanceUrl,
  2. required Map<String, dynamic> userObject,
  3. required Map<String, dynamic> tokenObject,
  4. Dio? dioTestClient,
})

Implementation

Future<LoginResponseStatus> setToken(
    {required String instanceUrl,
    required Map<String, dynamic> userObject,
    required Map<String, dynamic> tokenObject,
    Dio? dioTestClient}) async {
  final uri = Uri.parse(instanceUrl).host;
  final String databaseName = '$uri';

  d2Instance.databaseInstance = await d2Instance.setDatabase(
      databaseName: databaseName,
      inMemory: d2Instance.inMemory,
      databaseFactory: d2Instance.databaseFactory,
      sharedPreferenceInstance: d2Instance.sharedPreferenceInstance);

  AuthToken token = AuthToken.fromJson(tokenObject);

  userObject['token'] = token.accessToken;
  userObject['tokenType'] = token.tokenType;
  userObject['tokenExpiry'] = token.expiresIn;
  userObject['refreshToken'] = token.refreshToken;
  userObject['isLoggedIn'] = true;
  userObject['dirty'] = true;
  userObject['baseUrl'] = instanceUrl;
  userObject['authType'] = "token";
  userObject['tokenExpiresAt'] = DateTime.now()
      .add(Duration(seconds: token.expiresIn))
      .toIso8601String();
  User user = User.fromApi(userObject);

  await d2Instance.userModule.user.setData(user).save();

  await d2Instance.userModule.userOrganisationUnit
      .setData(user.organisationUnits)
      .save();

  return LoginResponseStatus.ONLINE_LOGIN_SUCCESS;
}