fetchAuthUserData method

  1. @override
Future<Either<Failure, User>> fetchAuthUserData()
override

Implementation

@override
Future<Either<Failure, User>> fetchAuthUserData() async {
  return wrapAndHandleHttpBaseRequest(
    () async {
      if (config.fetchUserInformationAPIendpoint == null) {
        throw Exception(
            "fetchUserInformationAPIendpoint configuration not provided");
      }
      final authDataRes = await getAuthenticatedSession();
      final authData = authDataRes.getOrElse(() => null);
      if (authData == null) {
        throw Exception(
            "Cannot fetch user information since there is no session stored");
      }
      final url = config.fetchUserInformationAPIendpoint!(authData);

      return Request("GET", url);
    },
    onResponse: (response, left, right) async {
      final Map<String, dynamic> decoded = jsonDecode(response.body);
      final Map<String, dynamic> userData = Map<String, dynamic>.of(
        await config.customFetchUserInformationResponseMapper?.call(
              Map<String, dynamic>.of(decoded),
            ) ??
            decoded["data"],
      );
      final user = await (config.customUserMapper?.call(userData) ??
          _defaultUserParser(userData));
      // let's update stored user with new data
      final authDataRes = await getAuthenticatedSession();
      authDataRes.map((authData) {
        if (authData != null) {
          storeAuthDataSession(authData.copyWith(user: user!));
        }
      });
      return right(user!);
    },
  );
}