JWT for WordPress

I am falling in love with using WordPress as a headless CMS. I just started to use WordPress APIs for adding content.

I stumbled upon JWT Authentication for the WP REST API plugin. It made the process easier to create JWT.

The plugin description is a great place to get started.

  • Install and activate the plugin
  • It adds the rewrite rule to .htaccess by itself
  • Add the keys to your wp-config.php
  • define('JWT_AUTH_SECRET_KEY', 'top-secret');
  • define('JWT_AUTH_CORS_ENABLE', true);
  • Then make a post request to http://your-site.com/wp-json/jwt-auth/v1/token?username=garfield&password=secret

The success response will look like this

{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9ibG9ja3MubG9jYWwiLCJpYXQiOjE2NjEwMTkxNzQsIm5iZiI6MTY2MTAxOTE3NCwiZXhwIjoxNjYxNjIzOTc0LCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxIn19fQ.tkrRrO37ed0jQzjMYVshe7V8grFLoRA7pn2T6sabx1w",
"user_email": "[email protected]",
"user_nicename": "garfield",
"user_display_name": "Garfield"
}

You can review the JWT token at https://jwt.io/
Paste the token and choose the algorithm (HS384)
You can check if the token is valid or not.

In case you want to try a third party service for authentication, try Auth0
Upto 7000 monthly active users, Auth0 offers free tier, and the best part is you can find code samples and SDKs for all the common programming languages.

For using Auth0 and WordPress, read this blog post or watch the video.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *