getCurrentUser() REST API request
Description
Hi,
I'm using WordPress JavaScript packages to get current user infos from a custom settings page with the code below.
All scripts are loaded with the right dependencies and settings by default.
For example wp-api-fetch :
Issue identified : The sent request to the REST API does'nt contain the Root URL mentioned in those settings.
Image shows the request sent to the Settings API but I have the same error with User API, it uses care.local/wp/… instead of care.local/wp-json/wp/… at the Root URL level.
Any idea where this problem comes from ?
Step-by-step reproduction instructions
Example of code to get Current User infos :
import { store as coreStore } from "@wordpress/core-data";
import { useSelect } from "@wordpress/data";
function MySettingsPage() {
const currentUser = useSelect( ( select ) => select( coreStore ).getCurrentUser(), [] );
console.log( { currentUser } );
return ( <p>Example page</p> );
}
export default MySettingsPage;
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Is your build process setup to utilize the wp globals with the DependencyExtractionPlugin instead of pulling in the actual source files?
I use the current settings in my webpack config :
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { DefinePlugin } = require( 'webpack' );
const path = require( 'path' );
module.exports = {
...defaultConfig,
...{
resolve: {
alias: {
react: path.resolve( './node_modules/react' ),
},
},
plugins: [
new DefinePlugin( {
// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
'process.env.GUTENBERG_PHASE': JSON.stringify(
parseInt( process.env.npm_package_config_GUTENBERG_PHASE, 10 ) || 1
),
} ),
]
},
};
I don't know if it helps
That looks to be the issue. See https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#provide-your-own-webpack-config for how to use a custom webpack config. You need to include the existing definitions for plugins for instance.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { DefinePlugin } = require( 'webpack' );
const path = require( 'path' );
module.exports = {
...defaultConfig,
...{
resolve: {
alias: {
react: path.resolve( './node_modules/react' ),
},
},
plugins: [
...defaultConfig.plugins,
new DefinePlugin( {
// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
'process.env.GUTENBERG_PHASE': JSON.stringify(
parseInt( process.env.npm_package_config_GUTENBERG_PHASE, 10 ) || 1
),
} ),
]
},
};
You also shouldn't need react stop be setup as an alias once you have the plugins properly configured.
It resolves the problem, thanks you very much!