diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts index ea5786eca..5dac39fee 100644 --- a/src/core/classes/site.ts +++ b/src/core/classes/site.ts @@ -591,8 +591,7 @@ export class CoreSite { return response; } catch (error) { - if (error.errorcode == 'invalidtoken' || - (error.errorcode == 'accessexception' && error.message.indexOf('Invalid token - token expired') > -1)) { + if (CoreUtils.isExpiredTokenError(error)) { if (initialToken !== this.token && !retrying) { // Token has changed, retry with the new token. preSets.getFromCache = false; // Don't check cache now. Also, it will skip ongoingRequests. diff --git a/src/core/features/pushnotifications/services/pushnotifications.ts b/src/core/features/pushnotifications/services/pushnotifications.ts index ba07fa9d3..a4aadf0a7 100644 --- a/src/core/features/pushnotifications/services/pushnotifications.ts +++ b/src/core/features/pushnotifications/services/pushnotifications.ts @@ -524,7 +524,13 @@ export class CorePushNotificationsProvider { try { response = await site.write('core_user_remove_user_device', data); } catch (error) { - if (CoreUtils.isWebServiceError(error)) { + if (CoreUtils.isWebServiceError(error) || CoreUtils.isExpiredTokenError(error)) { + // Cannot unregister. Don't try again. + await CoreUtils.ignoreErrors(db.deleteRecords(PENDING_UNREGISTER_TABLE_NAME, { + token: site.getToken(), + siteid: site.getId(), + })); + throw error; } diff --git a/src/core/services/utils/utils.ts b/src/core/services/utils/utils.ts index 6d06cbf3d..938a48778 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -850,7 +850,7 @@ export class CoreUtilsProvider { } /** - * Given an error returned by a WS call, check if the error is generated by the app or it has been returned by the WebSwervice. + * Given an error returned by a WS call, check if the error is generated by the app or it has been returned by the WebService. * * @param error Error to check. * @return Whether the error was returned by the WebService. @@ -858,10 +858,22 @@ export class CoreUtilsProvider { // eslint-disable-next-line @typescript-eslint/no-explicit-any isWebServiceError(error: any): boolean { return error && (typeof error.warningcode != 'undefined' || (typeof error.errorcode != 'undefined' && - error.errorcode != 'invalidtoken' && error.errorcode != 'userdeleted' && error.errorcode != 'upgraderunning' && + error.errorcode != 'userdeleted' && error.errorcode != 'upgraderunning' && error.errorcode != 'forcepasswordchangenotice' && error.errorcode != 'usernotfullysetup' && error.errorcode != 'sitepolicynotagreed' && error.errorcode != 'sitemaintenance' && - (error.errorcode != 'accessexception' || error.message.indexOf('Invalid token - token expired') == -1))); + !this.isExpiredTokenError(error))); + } + + /** + * Given an error returned by a WS call, check if the error is a token expired error. + * + * @param error Error to check. + * @return Whether the error is a token expired error. + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + isExpiredTokenError(error: any): boolean { + return error.errorcode === 'invalidtoken' || + (error.errorcode === 'accessexception' && error.message.includes('Invalid token - token expired')); } /**