PHP: How to use token object to read user data

Watch the guide

38 steps

6 months ago

1
Find the token folder in the generated PHP API
Find the token folder in the generated PHP API
2
The token.php configuration file stores all the settings for your JWT token, including the expiration datetime and issuer.
The token.php configuration file stores all the settings for your JWT token, including the expiration datetime and issuer.
3
The Generate.php file is used to create a token when the "Token Generation" endpoint is called with a valid username and password.
The Generate.php file is used to create a token when the "Token Generation" endpoint is called with a valid username and password.
4
The `Validatetoken.php` file is used to validate the token for each request.
The `Validatetoken.php` file is used to validate the token for each request.
5
6
open "Validatetoken.php" file
open "Validatetoken.php" file
7
Search for $decodedJWTData
(This variable will be globally accessible to all API endpoints.)
Search for $decodedJWTData<div>(This variable will be globally accessible to all API endpoints.)</div>
8
If the token is valid $decodedJWTData variable will have data.
If the token is valid $decodedJWTData variable will have data.
9
API project folder
API project folder
10
Let's attempt to modify the business endpoint to retrieve all businesses based on the logged-in user.
Let's attempt to modify the business endpoint to retrieve all businesses based on the logged-in user.
11
Open the business folder
Open the business folder
12
The business folder contains all the generated endpoints.
The business folder contains all the generated endpoints.
13
Lets open read.php which return GET ALL business.
Lets open read.php which return GET ALL business.
14
read.php file to get all business.
read.php file to get all business.
15
Create the object of Business class.
Create the object of Business class.
16
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.
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.
17
Database function call.
Database function call.
18
If you want to know what data is stored in your token you can use var_dump to print. 
If you want to know what data is stored in your token you can use var_dump to print.&nbsp;
19
Here, I am assigning the owner ID to the logged-in user ID.
Here, I am assigning the owner ID to the logged-in user ID.
20
Let's examine the changes needed to read the database call.
Let's examine the changes needed to read the database call.
21
Open "objects" folder for database functions.
Open "objects" folder for database functions.
22
23
Open business.php file
Open business.php file
24
25
Search for read() function
Search for read() function
26
In the SQL query to select all.
Add a `WHERE` clause to filter by `owner_id`.
In the SQL query to select all.<div>Add a `WHERE` clause to filter by `owner_id`.</div>
27
Add the binding parameter
Add the binding parameter
28
Save the file
Save the file
29
In the same file business.php search for total_record_count function
In the same file business.php search for total_record_count function
30
Add a `WHERE` clause to filter by `owner_id`.
Add a `WHERE` clause to filter by `owner_id`.
31
Binding parameter
Binding parameter
32
Save the file.
Save the file.
33
In Postman I have generated token for user with id 1.
In Postman I have generated token for user with id 1.
34
Open Business GET ALL request. 
Open Business GET ALL request.&nbsp;
35
Click Send
Click Send
36
We got total of 1 record.
We got total of 1 record.
37
With owner id as provided by token. (user id =1)
With owner id as provided by token. (user id =1)
38
To verify the data, you can execute a similar query directly on the database.
To verify the data, you can execute a similar query directly on the database.