What’s the best way to deal with the nonce expiration?
-
I have a page with JS code that sends/requests data from a custom WP endpoint.
When the page that contains my JS is accessed, I create a nonce.
Then, when I have to send/receive data from the endpoint I set the ‘X-WP-Nonce’ header.
At the endpoint level, in the function that manages the request, I verify the validity of the nonce by passing the nonce created when the page was accessed: if the nonce is not valid I return:
new WP_Error('nonce_error', 'Invalid nonce', array( 'status' => 404 ));
Now: the user that performs the action to request data must be logged in (he can neither request data if not: the page is visible only to logged in user and the endpoint is callable only from logged in users, since I added a permission_callback. You can’t neither directly access the page, I added on topo of it
if (!defined('ABSPATH')) exit;
).But, I suppose that the following can happen:
1. the user has a nonce started 11.57 hours ago;
2. the user starts using the app with that nonce;
3. when he submits data to the enpoint, the nonce has expired and he receives the error;In that case, the user is logged in but something wrong happens: something the user can’t control and can’t understand. For example, what alert could I display? “You’re not logged in, please login again”? The user is actually logged in!
So, what’s the best way to deal with the nonce expiration?
Any suggestion is appreciated
- The topic ‘What’s the best way to deal with the nonce expiration?’ is closed to new replies.