Skip to content

Commit 470c268

Browse files
committed
feat: Authorize AJAX with application passwords
Include authorization header in AJAX requets, as we do not have cookies to send in the mobile app environment.
1 parent cf3b282 commit 470c268

File tree

3 files changed

+79
-126
lines changed

3 files changed

+79
-126
lines changed

src/utils/ajax.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { getGBKit } from './bridge';
5+
import { warn, debug } from './logger';
6+
7+
/**
8+
* GutenbergKit lacks authentication cookies required for AJAX requests.
9+
* This configures a root URL and authentication header for AJAX requests.
10+
*
11+
* @return {void}
12+
*/
13+
export function initializeAjax() {
14+
window.wp = window.wp || {};
15+
window.wp.ajax = window.wp.ajax || {};
16+
window.wp.ajax.settings = window.wp.ajax.settings || {};
17+
18+
const { siteURL, authHeader } = getGBKit();
19+
configureAjaxUrl( siteURL );
20+
configureAjaxAuth( authHeader );
21+
}
22+
23+
function configureAjaxUrl( siteURL ) {
24+
if ( ! siteURL ) {
25+
warn( 'Unable to configure AJAX URL without siteURL' );
26+
return;
27+
}
28+
29+
window.wp.ajax.settings.url = `${ siteURL }/wp-admin/admin-ajax.php`;
30+
31+
debug( 'AJAX URL configured' );
32+
}
33+
34+
function configureAjaxAuth( authHeader ) {
35+
if ( ! authHeader ) {
36+
warn( 'Unable to configure AJAX auth without authHeader' );
37+
return;
38+
}
39+
40+
window.jQuery?.ajaxSetup( {
41+
headers: {
42+
Authorization: authHeader,
43+
},
44+
} );
45+
46+
const originalSend = window.wp.ajax.send;
47+
window.wp.ajax.send = function ( options ) {
48+
const originalBeforeSend = options.beforeSend;
49+
50+
options.beforeSend = function ( xhr ) {
51+
xhr.setRequestHeader( 'Authorization', authHeader );
52+
53+
if ( typeof originalBeforeSend === 'function' ) {
54+
originalBeforeSend( xhr );
55+
}
56+
};
57+
58+
return originalSend.call( this, options );
59+
};
60+
61+
const originalPost = window.wp.ajax.post;
62+
window.wp.ajax.post = function ( options ) {
63+
const originalBeforeSend = options.beforeSend;
64+
65+
options.beforeSend = function ( xhr ) {
66+
xhr.setRequestHeader( 'Authorization', authHeader );
67+
68+
if ( typeof originalBeforeSend === 'function' ) {
69+
originalBeforeSend( xhr );
70+
}
71+
};
72+
73+
return originalPost.call( this, options );
74+
};
75+
76+
debug( 'AJAX auth configured' );
77+
}

src/utils/remote-editor.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { awaitGBKitGlobal } from './bridge';
55
import { loadEditorAssets } from './editor-loader';
6-
import { initializeVideoPressAjaxBridge } from './videopress-bridge';
6+
import { initializeAjax } from './ajax';
77
import { error, warn } from './logger';
88
import { isDevMode } from './dev-mode';
99
import './editor-styles.js';
@@ -71,7 +71,7 @@ function initializeApiFetch( assetsResult ) {
7171
}
7272

7373
function initializeEditor( assetsResult ) {
74-
initializeVideoPressAjaxBridge();
74+
initializeAjax();
7575

7676
const { allowedBlockTypes } = assetsResult;
7777
return import( './editor' ).then(

src/utils/videopress-bridge.js

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)