Skip to content

fix: Optional chaining for nonce#1058

Merged
subodhr258 merged 1 commit into
mainfrom
hotfix/likes-comments-rtmedia
Sep 8, 2025
Merged

fix: Optional chaining for nonce#1058
subodhr258 merged 1 commit into
mainfrom
hotfix/likes-comments-rtmedia

Conversation

@subodhr258
Copy link
Copy Markdown
Collaborator

@subodhr258 subodhr258 commented Sep 8, 2025

Added Optional Chaining to nonce, following best practices.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds optional chaining to prevent potential errors when accessing the nonce property from nonceData objects. The change ensures the application gracefully handles cases where nonceData might be undefined or null.

  • Applies optional chaining (?.) to nonceData.nonce access across four API call locations
  • Prevents potential TypeError exceptions when nonceData is undefined

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

like_status: likeStatus,
};
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
Copy link

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional chaining will pass undefined to createNonceMiddleware when nonceData is null/undefined, which may cause the middleware to fail silently or behave unexpectedly. Consider adding explicit validation and error handling instead of relying on optional chaining alone.

Suggested change
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
if ( nonceData && typeof nonceData.nonce === 'string' && nonceData.nonce.length > 0 ) {
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
} else {
console.error( 'Nonce data is missing or invalid. API requests may fail.' );
}

Copilot uses AI. Check for mistakes.
comment_type: commentType,
};
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
Copy link

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional chaining will pass undefined to createNonceMiddleware when nonceData is null/undefined, which may cause the middleware to fail silently or behave unexpectedly. Consider adding explicit validation and error handling instead of relying on optional chaining alone.

Suggested change
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
if ( !nonceData || !nonceData.nonce ) {
setIsSending( false );
storeObj.dispatch.errorHappened( __( 'Security error: missing nonce.', 'godam' ) );
return;
}
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );

Copilot uses AI. Check for mistakes.
};

apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
Copy link

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional chaining will pass undefined to createNonceMiddleware when nonceData is null/undefined, which may cause the middleware to fail silently or behave unexpectedly. Consider adding explicit validation and error handling instead of relying on optional chaining alone.

Suggested change
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
if ( !nonceData || !nonceData.nonce ) {
console.error( 'Nonce data is missing. Cannot delete comment securely.' );
setIsDeleting( false );
return;
}
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );

Copilot uses AI. Check for mistakes.
guest_user_email: guestEmail,
};
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
Copy link

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optional chaining will pass undefined to createNonceMiddleware when nonceData is null/undefined, which may cause the middleware to fail silently or behave unexpectedly. Consider adding explicit validation and error handling instead of relying on optional chaining alone.

Suggested change
apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) );
if ( nonceData && typeof nonceData.nonce === 'string' && nonceData.nonce.length > 0 ) {
apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) );
} else {
console.error( 'Nonce data is missing or invalid. Guest login aborted.' );
setLoginProgress( false );
return;
}

Copilot uses AI. Check for mistakes.
@subodhr258 subodhr258 merged commit 6b771d8 into main Sep 8, 2025
5 checks passed
@subodhr258 subodhr258 deleted the hotfix/likes-comments-rtmedia branch September 8, 2025 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants