User Data Fields For JWT Authentication

Description

WordPress is Good Way To Build Websites , But It Will Be Better If Build Android and Ios Apps For Your Websites
Now With Rest Api You Can Create Posts , Edit Posts , View Posts , Users , Create Users , Upload Media etc..

But one of the things you may need that Wp Rest Api doesn’t provide is the Authentication System ( Login User )
You Have To Use Jwt Plugin

And This Plugin Is Useful If You Don’t Want To Make Another Request To Fetch User Data Like Id etc..
Because You Have To Get The Id Of The User To Get All Data On External Apps Like Mobile Apps
And You Should Make Requests To https://example.com/wp-json/wp/v2/users?search=Limit results to those matching a string
But With This Plugin It Make More Easy And Faster
It Really Simple Plugin
It Writed With 50 Lines Of Code , The Size Of The Plugin Is 3Kb

Note : This Is Plugin For
https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/

So If Jwt Authentication Plugin Is Not Installed
This Plugin Will Not Add The Fields To This EndPoint
wp-json/jwt-auth/v1/token

And Fields Will Added Only If Getted Token Successfully

Example Request (Before Install The Plugin):
{
    “token”: “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xOTIuMTY4LjAuMTgyIiwiaWF0IjoxNjE0MDg3NDQ2LCJuYmYiOjE2MTQwODc0NDYsImV4cCI6MTYxNDY5MjI0NiwiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMSJ9fX0.KeAUc7PiTne0_PThsSAWo1ruvl2Ocu-fbCn2jG7zkoE”,
    “user_email”: “example@example.com”,
    “user_nicename”: “admin”,
    “user_display_name”: “admin”
}

Example Response (After Installed The Plugin):
{
    “token”: “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xOTIuMTY4LjAuMTgyIiwiaWF0IjoxNjE0MDg2NjQyLCJuYmYiOjE2MTQwODY2NDIsImV4cCI6MTYxNDY5MTQ0MiwiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMSJ9fX0.nyDOICdQcZKbWZo2kQRp_eLBkuxjnK_rpxs-HJREyCg”,
    “user_email”: “example@example.com”,
    “user_nicename”: “admin”,
    “user_display_name”: “admin”,
    “user_id”: 1,
    “user_login”: “admin”,
    “user_first_name”: “sfd”,
    “user_last_name”: “”,
    “user_roles”: [
        “administrator”
    ],
    “user_role”: “administrator”,
    “user_registered”: “2021-02-17 09:21:29”,
    “user_url”: “http://localhost”,
    “user_status”: “0”,
    “user_avatar_url”: “https://secure.gravatar.com/avatar/22feea4605ac5b7163eac439b5241034?s=96&d=mm&r=g”,
    “user_activation_key”: “”
}

You Can Add The Code Of This Plugin To Your Function.php
But This Not Recommended Because When You Change The Theme Or Update It
The Code Will Removed

Don’t Forget To Setup Jwt Plugin
https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/

Can Add User Data Fields To Validate Token Endpoint ?

This Plugin Also Can Add User Data Fields To Validate Token Endpoint
Unfortunately, the jwt extension does not support using the filter for validate_token function,
So You Will Need To Modify Some Codes Inside Jwt Plugin Codes
Go To \wp-content\plugins\jwt-authentication-for-wp-rest-api\public\class-jwt-auth-public.php
And At The End Of Function That Named validate_token
In Line 302

Edit This Code
From :
/** If the output is true return an answer to the request to show it */
return array(
‘code’ => ‘jwt_auth_valid_token’,
‘data’ => array(
‘status’ => 200,
),
);
To :
$value = array(
‘code’ => ‘jwt_auth_valid_token’,
‘data’ => array(
‘status’ => 200,
)
);
/** If the output is true return an answer to the request to show it */
return apply_filters(‘jwt_auth_validate_token’, $value, $token);

Now You Have Done
The Plugin Will Able To Add New Values by adding a filter
Note :
If You Updated The Jwt Plugin
You Should Again Edit The Code Because It Will Removed

REQUIREMENTS

JWT Authentication for WP REST API

Screenshots

  • This is When Get Token Response
  • This Is When Get Validate Token Response

FAQ

How To Use This Plugin ?

Just Install It To Your WordPress Site , And No Another Action Required , But If You Want Add The User Fields To Validate Token Please Make Sure That You Have Edit Required Code In Jwt Plugin , Please Read Description For More Information

Reviews

སྤྱི་ཟླ་བརྒྱད་པ། 24, 2021
I just wanted to thank the author for making such great plugins 😊
Read all 3 reviews

Contributors & Developers

“User Data Fields For JWT Authentication” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

.