fetchLoggedInUserId method
Fetches already authenticated user, using Jwt
Associated endpoint : /wp-json/wp/v2/users/me
Implementation
Future<int?> fetchLoggedInUserId() async {
_authToken = await _localDbService.getSecurityToken();
_urlHeader['Authorization'] = 'Bearer ' + _authToken!;
final response = await http.get(Uri.parse(this.baseUrl + URL_USER_ME),
headers: _urlHeader);
if (response.statusCode >= 200 && response.statusCode < 300) {
final jsonStr = json.decode(response.body);
if (jsonStr.length == 0)
throw new WooCommerceError(
code: 'wp_empty_user',
message: "No user found or you dont have permission");
_printToLog('account user fetch : ' + jsonStr.toString());
return jsonStr['id'];
} else {
WooCommerceError err =
WooCommerceError.fromJson(json.decode(response.body));
throw err;
}
}