get_user_obj()
public function get_user_obj(?string $token = null): object|bool
Description
Retrieves the full user object associated with a token. If no token is provided, checks session, cookie, and HTTP headers automatically.
The user object contains all columns from the user record (typically from trongate_users or your custom users table).
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| $token | string|null | A 64‑character hexadecimal token. If null, the method searches session, cookie, and HTTP headers. | null | No |
Return Value
| Type | Description |
|---|---|
| object|bool |
|
Example Usage
PHP
// Get current user object
$user = $this->trongate_tokens->get_user_obj();
if ($user !== false) {
echo "Welcome, " . $user->username;
echo "Email: " . $user->email;
}
// Get user object from specific token
$token = post('token', true);
$user = $this->trongate_tokens->get_user_obj($token);
// Use in member area controller
public function dashboard() {
$user = $this->trongate_tokens->get_user_obj();
if ($user === false) {
redirect('members/login');
}
$data['user'] = $user;
$this->view('dashboard', $data);
}
// API endpoint returning user profile
public function me() {
$user = $this->trongate_tokens->get_user_obj();
if ($user === false) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
die();
}
// Remove sensitive fields
unset($user->password);
echo json_encode($user);
}Notes
- Calls
$this->model->get_user_obj($token)which validates token expiration. - Token search order when
$tokenis null: HTTP header → cookie → session. - The object contains all database columns; be careful not to expose passwords or other sensitive data.
- Returns
falseif token is expired, invalid, or not found. - User table name is determined by the
users_tableproperty in the model (default:trongate_users).