-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Fetch: Expose nonce on created middleware function (#13451)
- Loading branch information
Showing
8 changed files
with
46 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
'wp-rich-text', | ||
), | ||
'wp-api-fetch' => array( | ||
'wp-hooks', | ||
'wp-i18n', | ||
'wp-url', | ||
), | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { addAction } from '@wordpress/hooks'; | ||
function createNonceMiddleware( nonce ) { | ||
function middleware( options, next ) { | ||
const { headers = {} } = options; | ||
|
||
const createNonceMiddleware = ( nonce ) => { | ||
let usedNonce = nonce; | ||
|
||
/** | ||
* This is not ideal but it's fine for now. | ||
* | ||
* Configure heartbeat to refresh the wp-api nonce, keeping the editor | ||
* authorization intact. | ||
*/ | ||
addAction( 'heartbeat.tick', 'core/api-fetch/create-nonce-middleware', ( response ) => { | ||
if ( response[ 'rest-nonce' ] ) { | ||
usedNonce = response[ 'rest-nonce' ]; | ||
} | ||
} ); | ||
|
||
return function( options, next ) { | ||
let headers = options.headers || {}; | ||
// If an 'X-WP-Nonce' header (or any case-insensitive variation | ||
// thereof) was specified, no need to add a nonce header. | ||
let addNonceHeader = true; | ||
for ( const headerName in headers ) { | ||
if ( headers.hasOwnProperty( headerName ) ) { | ||
if ( headerName.toLowerCase() === 'x-wp-nonce' ) { | ||
addNonceHeader = false; | ||
break; | ||
} | ||
if ( headerName.toLowerCase() === 'x-wp-nonce' ) { | ||
return next( options ); | ||
} | ||
} | ||
|
||
if ( addNonceHeader ) { | ||
// Do not mutate the original headers object, if any. | ||
headers = { | ||
...headers, | ||
'X-WP-Nonce': usedNonce, | ||
}; | ||
} | ||
|
||
return next( { | ||
...options, | ||
headers, | ||
headers: { | ||
...headers, | ||
'X-WP-Nonce': middleware.nonce, | ||
}, | ||
} ); | ||
}; | ||
}; | ||
} | ||
|
||
middleware.nonce = nonce; | ||
|
||
return middleware; | ||
} | ||
|
||
export default createNonceMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters