fix: Optional chaining for nonce#1058
Conversation
There was a problem hiding this comment.
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 (
?.) tononceData.nonceaccess across four API call locations - Prevents potential TypeError exceptions when
nonceDatais 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 ) ); |
There was a problem hiding this comment.
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.
| 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.' ); | |
| } |
| comment_type: commentType, | ||
| }; | ||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) ); | ||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) ); |
There was a problem hiding this comment.
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.
| 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 ) ); |
| }; | ||
|
|
||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) ); | ||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) ); |
There was a problem hiding this comment.
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.
| 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 ) ); |
| guest_user_email: guestEmail, | ||
| }; | ||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData.nonce ) ); | ||
| apiFetch.use( apiFetch.createNonceMiddleware( nonceData?.nonce ) ); |
There was a problem hiding this comment.
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.
| 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; | |
| } |
Added Optional Chaining to nonce, following best practices.