getAuthenticationToken static method

Future<MaybeError<String>> getAuthenticationToken({
  1. required String username,
  2. required String password,
  3. UriHelper uriHelper = uriHelperFolksonomyProd,
})

Authentication: provide username/password and get a bearer token in return.

username: Open Food Facts user_id (not email) password: user password (clear text, but HTTPS encrypted) token is returned, to be used in later requests with usual "Authorization: bearer token" headers

Implementation

static Future<MaybeError<String>> getAuthenticationToken({
  required final String username,
  required final String password,
  final UriHelper uriHelper = uriHelperFolksonomyProd,
}) async {
  final Uri uri = uriHelper.getUri(
    path: '/auth',
  );
  final Response response = await post(
    uri,
    body: <String, String>{
      'username': username,
      'password': password,
    },
  );
  if (response.statusCode == 200) {
    try {
      final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
      return MaybeError<String>.value(decodedResponse['access_token']);
    } catch (e) {
      //
    }
  }
  return MaybeError<String>.responseError(response);
}