trOnGAtE

Fetching User Data From Tokens (PHP Friendly Version)
The Trongate_tokens class, which is inside the trongate_tokens module, contains two methods for fetching user data.
- _get_user_id() - which attempts to return the Trongate User ID
- _get_user_obj() - which attempts to return the Trongate User Object
Fetching The Trongate User ID
To fetch the Trongate User ID, load up the trongate_tokens module and invoke the _get_user_id() method. For example,
$this->module('trongate_tokens');
$trongate_user_id = $this->trongate_tokens->_get_user_id();
Fetching The Trongate User Object
To fetch the Trongate User Object, load up the trongate_tokens module and invoke the _get_user_obj() method. For example,
$this->module('trongate_tokens');
$trongate_user_obj = $this->trongate_tokens->_get_user_obj();
Understanding The User Object
The user object that gets returned after successfully invoking _get_user_obj(), contains the following properties:
- trongate_user_code - the value for this property corresponds with the 'code' column on the 'trongate_users' table.
- user_level_id - this property contains a value that matches the 'id' column, for the user, from the 'trongate_user_levels' table.
- user_level - this property contains the value, for the user, that's stored on the 'level_title' column of the 'trongate_user_levels' table.
- token - the 'token' property contains the user's validated token. This corresponds with the value, for the user, that's stored on the 'token' column on the 'trongate_tokens' table.
- trongate_user_id - this property contains the user's 'Trongate User ID'. This is a numeric value, for the user, that corresponds with the 'id' column of the 'trongate_users' table.
- expiry_date - finally, we have an expiry_date property. This contains a Unix timestamp that represents the date and time when the token will expire.
Below shows an example of the kind of data that we can expect to receive from the _get_user_obj() method, represented as a JSON string for clarity:
{
"trongate_user_code":"dytcGEc47h6VCmYmrJLwH5CPx8GMWcKa",
"user_level_id":"1",
"user_level":"admin",
"token":"r8Ex_25FoYFSsrJTn3wezn6wzW8XRLkf",
"trongate_user_id":"1",
"expiry_date":"1629508409"
}
Fetching A User Level From A Token
Sometimes it's useful to fetch a user level from a token. As a reminder, a user level is a user's assigned level, as recorded on the trongate_users_levels table, on the 'user_level' column.
Assuming you know what the value of a user's token is, you can fetch the user's role (i.e., the user level) by calling upon the trongate_tokens module. Once the trongate_tokens module has been loaded, you can fetch the user_level by invoking the _get_user_level() method. This method requires one argument:
- $token - the value of the token
EXAMPLE
$this->module('trongate_tokens');
$user_level = $this->trongate_tokens->_get_user_level($token);
echo $user_level; //outputs (something like) 'admin'
HELP & SUPPORT
If you have a question or a comment relating to anything you've see here, please goto the Help Bar.