You shouldn’t pass your nonce to your JavaScript to verify it, since client side scripts can be easily manipulated. Instead, you should get the nonce from your front-end content, and then pass it to server to verify it.
After verification, you should decide to output content by server, not by your JavaScript file.
Something like this:
if ( is_user_logged_in() ) {
if ( wp_verify_nonce($_REQUEST['X-WP-Nonce'], 'wp_rest') {
// Nonce is correct!
} else {
// Don't send the data, it's a trap!
}
}
As a side note, REST API offers its own method to fetch the passed queries. So, you can get it this way in your callback function:
function foobar( \WP_REST_Request $request ) {
$nonce = $request['X-WP-Nonce'];
}
Related Posts:
- Can’t GET draft posts via REST API from headless frontend
- WP REST API – Nonce passes wp_verify_nonce even after logout
- Rest API: wp_verify_nonce() fails despite receiving correct nonce value
- Log in user using WordPress REST API
- wp_nonce vs jwt
- Register rest field authentication with REST API
- How to: Make JWT-authenticated requests to the WordPress API
- WordPress Rest API: How do we validate with our custom API key?
- WordPress REST API call generates nonce twice on every call
- How to Authenticate WP REST API with JWT Authentication using Fetch API
- authentication issue with rest api – rest_cannot_create
- Can I authenticate with both WooCommerce consumer key and JWT?
- How to login to WordPress site using basic authentication HTTP headers?
- Can we access the REST request parameters from within the permission_callback to enforce a 401 by returning false?
- WordPress REST API “rest_authentication_errors” doesn’t work external queries?
- Create Session with JWT
- Full page NGINX (or Cloudflare) caching and WordPress nonces
- WordPress REST API, Expired Nonce from Cache results in 403 forbidden
- Passing a borrowed nonce through Postman fails
- how to send Ajax request in wordpress backend
- permission_callback has no effect
- WP REST API GET Requests require authentication
- current_user_can(‘administrator’) returns false when I’m logged in
- Authenticating with REST API
- Make authorization mandatory on custom routes
- How to force JWT auth for default GET endpoints of WordPress rest api?
- REST API: best place to set current user for JWT auth?
- WordPress + REST API v2 and private pages Load by slug
- REST API authentication for a plugin
- PHP: authenticate for a REST request?
- Rest API basic auth not working
- Authenticate current user to REST API
- Getting 401 from ajax using an application password
- How to connect android app with WordPress website?
- WordPress REST API calls that depend on the WordPress User
- Backbone with custom rest endpoints
- WordPress HTTP API NTLM Authentication
- Advanced Access Manager: RESTful endpoint to refresh token
- Best Authetication between REST API and Mobile App
- How to verify which WordPress user requested the API in ASP .NET Core?
- Secure WordPress API, how?
- register/login api
- How can I secure my custom rest api endpoint or add under a already existing rest group
- REST API Integration without user account?
- WP REST API with Basic Auth at target website
- Cant POST with REST API on WordPress
- REST API – Authentication/Logon security
- Rest API nonce is being cached
- custom REST endpoints and application passwords
- wordpress rest api authentication failed
- How to add additional http header to a wp_error rest response
- Nonce validation in REST API
- Accessing an auth protected custom WP API enpoint from remote origin
- Is the WordPress REST API installed and enabled in a vanilla WordPress 4.7 installation?
- Does something like is_rest() exist
- How to use OAuth authentication with REST API via CURL commands?
- REST API purpose?
- Get post count in wp rest API v2 and get all categories
- WP REST API — How to change HTTP Response status code?
- wp_get_current_user() function not working in Rest API callback function
- How to use WP-REST API to login user and get user data for Android app?
- Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script
- WP REST API Is it rather easy to rename the default wp-json uri part?
- Search WP API using the post title
- check the requesting url
- How would I add custom tables/endpoints to the WP REST API?
- WP REST API Require Password for GET Endpoint
- Displaying a page built with Elementor using the REST API [closed]
- Extend WordPress (4.x) session and nonce
- Getting user meta data from WP REST API
- Understanding SHORTINIT with WordPress 5
- How to use _embed when using _fields?
- WordPress REST API – Permission Callbacks
- WP REST API V2 – Retrieve sub page by full slug (URL/Path)
- WP REST API create post authentication issue
- How do I create a user using the new JSON api in 4.7?
- Verify nonce in REST API?
- Why is my custom API endpoint not working?
- WordPress REST API validation
- Are there server performance benefits to fetching only specific fields when querying the REST API?
- How to define a query parameter with REST API?
- Filter posts by multiple custom taxonomy terms using AND operator in REST API v2 (WordPress)
- WP REST API returns blank response if post is too long
- How do I correctly setup an AJAX nonce for WordPress REST API?
- how to authenticate for the REST API from a plugin and from command line
- How to check WordPress website username and password is correct
- Increase per_page limit in REST API
- Does pre_get_posts affect REST API responses?
- How to feed a HTML5’s EventSource with a REST API custom endpoint?
- How do I use the WP REST API plugin and the OAuth Server plugin to allow for registration and login?
- Adding WordPress API Endpoint With Multiple Parameters
- How to authenticate custom API endpoint in WooCommerce [closed]
- WordPress “Link has expired” error on updating posts
- Retrieve CSS and JS From the REST API
- Using the REST API (v2) javascript client on a private namespaced route
- WP REST API core major changes
- WordPress 4.7 REST API endpoints
- How to get all posts from parent and children categories?
- wordpress wp-json prefix issue
- How to build a plugin that supports authenticated POST requests to the REST API from external servers?