Trongate Docs
switch to dark modeswitch to dark mode
»
»
Fetching User Data From Tokens (PHP Friendly Version)

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

Did You Know?
The 'Trongate User ID' is the phrase that refers to the ID on the trongate_users table, that corresponds to the end user.

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();

Just To Let You Know
This method attempts to fetch a valid token from either session data or a cookie stored on the end user's device.  If a valid token is found then the returned value will be an integer, representing the user's Trongate User ID.  However, if no valid token is found then the method will return a boolean with the value of false.

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();

Just To Let You Know
This method attempts to fetch a valid token from either session data or a cookie stored on the end user's device.  If a valid token is found then the returned value will be an object, representing the user's information.  However, if no valid token is found then the method will return a boolean with the value of false.

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'

Just To Let You Know
If the token is not valid then the _get_user_level() method will return an empty string for the user level.


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×