🔥 Example of react application to access WordPress REST API
-
Clone this repo in
git clone https://github.com/imranhsayed/react-workshop
-
Run
npm install
A React App where you can login using the endpoint provided by
JWT Authentication for WP-API
WordPress Plugin. So you need to have this plugin installed on WordPress. The plugin's endpoint returns the user object and a jwt-token on success, which we can then store in localstorage and login the user on front React Application
- You need to install and activate JWT Authentication for WP REST API plugin on you WordPress site
- Then you need to configure it by adding these:
i. Add the last three lines in your WordPress .htaccess
file as shown:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>
ii. Add the following in your wp-config.php
Wordpress file. You can choose your own secret key.
define('JWT_AUTH_SECRET_KEY', '&BZd]N-ghz|hbH`=%~a5z(`mR=n%7#8-Iz@KoqtDhQ6(8h$og%-IbI#>N*T`s9Dg');
define('JWT_AUTH_CORS_ENABLE', true);
iii. Now you can make a request to /wp-json/jwt-auth/v1/token
REST API provided by the plugin. You need to pass
username and password and it returns a user object and token . You can save the token in localstorage and send it in the headers
of your protected route requests ( e.g. /wp-json/wp/v2/posts
)
iiv. So whenever you send a request to WordPress REST API for your protected routes, you send the token received in the headers of your request
{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer putTokenReceivedHere`
}
A React(front end) + Node(back end) application. It uses
jwt.sign()
( fromjwtwebtoken
npm package ) to generate a token using the username and password sent from front end( React ) and returns it as a response, which we then store in localstorage to login the user. This token received by frond end, will be sent with all further request for protected routes, which will then be verified in node route usingjwt.verify()
Besides generating the token, the end point in node also accesses the WordPress rest api to confirm the credentials and returns the user object or errors if any.
It also has functionality to create post where we make a request from front end along with token( React ) to a node end point. The node endpoint verifies the token and then makes a request to WordPress REST API endpoint to create the post and then returns the new post id, or error if any.
Contains features of jwt-verify-with-node and its an app ready to deploy to heroku
start
Runs node server for development ( in watch mode ). Theserver.js
sends all front end route request to index.html and then all front end route requests is handled by reach routerprod
Runs webpack in production mode