PHP: How to use token object to read user data

Find the token folder in the generated PHP API

The token.php configuration file stores all the settings for your JWT token, including the expiration datetime and issuer.

The Generate.php file is used to create a token when the "Token Generation" endpoint is called with a valid username and password.

The `Validatetoken.php` file is used to validate the token for each request.

open "Validatetoken.php" file

Search for $decodedJWTData

(This variable will be globally accessible to all API endpoints.)

If the token is valid $decodedJWTData variable will have data.

API project folder

Let's attempt to modify the business endpoint to retrieve all businesses based on the logged-in user.

Open the business folder

The business folder contains all the generated endpoints.

Lets open read.php which return GET ALL business.

read.php file to get all business.

Create the object of Business class.

Assign different properties: Retrieve the user ID of the logged-in user from `$decodedJWTData` and set it to the business object before making the database call.

Database function call.

If you want to know what data is stored in your token you can use var_dump to print. 

Here, I am assigning the owner ID to the logged-in user ID.

Let's examine the changes needed to read the database call.

Open "objects" folder for database functions.

Open business.php file

Search for read() function

In the SQL query to select all.

Add a `WHERE` clause to filter by `owner_id`.

Add the binding parameter

Save the file

In the same file business.php search for total_record_count function

Add a `WHERE` clause to filter by `owner_id`.

Binding parameter

Save the file.

In Postman I have generated token for user with id 1.

Open Business GET ALL request. 

Click Send

We got total of 1 record.

With owner id as provided by token. (user id =1)

To verify the data, you can execute a similar query directly on the database.

You successfully completed the guide!