authenticateViaJWT method

Future authenticateViaJWT({
  1. String? username,
  2. String? password,
})

Authenticates the user using WordPress JWT authentication and returns the access _token string.

Associated endpoint : yourwebsite.com/wp-json/jwt-auth/v1/token

Implementation

Future authenticateViaJWT({String? username, String? password}) async {
  final body = {
    'username': username,
    'password': password,
  };

  final response = await http.post(
    Uri.parse(
      this.baseUrl + URL_JWT_TOKEN,
    ),
    body: body,
  );

  if (response.statusCode >= 200 && response.statusCode < 300) {
    WooJWTResponse authResponse =
        WooJWTResponse.fromJson(json.decode(response.body));
    _authToken = authResponse.token;
    _localDbService.updateSecurityToken(_authToken);
    _urlHeader['Authorization'] = 'Bearer ${authResponse.token}';
    return _authToken;
  } else {
    throw new WooCommerceError.fromJson(json.decode(response.body));
  }
}