onResponse method
Clears and recreates contact when a 401 (Unauthorized), 403 (Forbidden) or 404 (Not found) response is returned from chatwoot public client api
Implementation
@override
Future<void> onResponse(
Response response, ResponseInterceptorHandler handler) async {
await responseLock.synchronized(() async {
if (response.statusCode == 401 ||
response.statusCode == 403 ||
response.statusCode == 404) {
await _localStorage.clear(clearChatwootUserStorage: false);
// create new contact from user if unauthorized,forbidden or not found
final contact = _localStorage.contactDao.getContact()!;
final conversation = await _authService.createNewConversation(
_inboxIdentifier, contact.contactIdentifier!);
await _localStorage.contactDao.saveContact(contact);
await _localStorage.conversationDao.saveConversation(conversation);
RequestOptions newOptions = response.requestOptions;
newOptions.path = newOptions.path.replaceAll(
INTERCEPTOR_INBOX_IDENTIFIER_PLACEHOLDER, _inboxIdentifier);
newOptions.path = newOptions.path.replaceAll(
INTERCEPTOR_CONTACT_IDENTIFIER_PLACEHOLDER,
contact.contactIdentifier!);
newOptions.path = newOptions.path.replaceAll(
INTERCEPTOR_CONVERSATION_IDENTIFIER_PLACEHOLDER,
"${conversation.id}");
//use authservice's dio without the interceptor for subsequent call
handler.next(await _authService.dio.fetch(newOptions));
} else {
// if response is not unauthorized, forbidden or not found forward response
handler.next(response);
}
});
}