regenerateKPShopifyToken static method
Regenerates the Shopify multipass token for the already-verified user using cached verified-user data and gk access token.
Implementation
static Future<Map<String, dynamic>?> regenerateKPShopifyToken() async {
// Throws KwikpassHealthException when Kwikpass is unhealthy.
final healthResult = await ApiService.checkKwikpassHealth();
if (healthResult.isSuccess &&
healthResult.getDataOrThrow()?.isKwikpassHealthy != true) {
throw Exception('Kwikpass is unhealthy to make the api calls');
}
try {
final results = await Future.wait([
cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkVerifiedUserKey)!),
cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkAccessTokenKey)!),
cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkUserPhone)!),
cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkNotificationEnabled)!),
]);
final verifiedUserData = results[0];
final gkAccessToken = results[1];
final userPhone = results[2];
final notifications = results[3] == 'true';
if (verifiedUserData == null ||
verifiedUserData.isEmpty ||
gkAccessToken == null ||
gkAccessToken.isEmpty) {
throw Exception('No verified user data or access token found');
}
final userData = jsonDecode(verifiedUserData) as Map<String, dynamic>;
final phoneFromUser = userData['phone'];
final phone = (phoneFromUser != null && '$phoneFromUser'.isNotEmpty)
? phoneFromUser
: userPhone;
final email = userData['email'];
final id = userData['shopifyCustomerId'];
final gokwik = DioClient().getClient();
gokwik.options.headers[cdnConfigInstance.getHeader(APIHeaderKeys.gkAccessToken)!] =
gkAccessToken;
final response = await gokwik.post(
cdnConfigInstance.getEndpoint(APIEndpointKeys.shopifyMultipass)!,
data: {
'id': id ?? '',
'email': email,
'redirectUrl': '/',
'isMarketingEventSubscribed': notifications,
'skipEmailOtp': true,
},
);
final Map<String, dynamic>? responseData =
(response.data?['data'] as Map?)?.cast<String, dynamic>();
final resUserData = {
...?responseData,
'phone': phone,
'email': responseData?['email'],
'shopifyCustomerId': responseData?['shopifyCustomerId'],
};
await cacheInstance.setValue(
cdnConfigInstance.getKeys(StorageKeyKeys.gkVerifiedUserKey)!,
jsonEncode(resUserData),
);
await ApiService.saveUserPhone(phone);
return responseData;
} catch (error) {
throw ApiService.handleApiError(error);
}
}