refreshToken method

Future<String> refreshToken({
  1. required String username,
  2. required String password,
})

Refresh the user's token returns the newly generated token

Implementation

Future<String> refreshToken({
  required String username,
  required String password,
}) async {
  final uri = Uri.https(authority, '/users/refresh/token');
  final auth = 'Basic ${base64Encode(utf8.encode("$username:$password"))}';
  _dio.options.headers['authorization'] = auth;
  late Response<Map<String, dynamic>> res;
  Map data;

  try {
    res = await _dio.getUri(uri);
    data = res.data as Map<String, dynamic>;
  } on DioError catch (e, s) {
    throw PindoError(
      message: (e.response?.data as Map)['message'],
      statusCode: (e.response?.data as Map)['status'] ?? res.statusCode,
      type: e.type.valueToString,
      stackTrace: s,
    );
  } on TypeError {
    throw PindoCastingError();
  } on Exception {
    // If the exception is none of the two above, just rethrow it
    rethrow;
  }

  if (data.containsKey('token')) {
    return data['token'];
  }
  throw PindoUnexpectedResponseError(expected: 'token', received: data);
}