regenerate()
public function regenerate(): void
Description
Regenerates a token with a new expiration date. It validates the input format of the old token and the expiration date before proceeding. If the input format is invalid or the old token does not exist, appropriate HTTP response codes are set to indicate the failure reason.
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| This method does not take any parameters. | ||||
Return Value
| Type | Description |
|---|---|
| void | This method does not return any value. |
Example Usage
The JavaScript code sample below illustrates extending a user's login session by regenerating their token using an HTTP GET request. This approach involves appending the existing token to the target URL string. While exposing tokens in URLs is generally considered poor practice due to security concerns, in this specific scenario, any validated tokens passed via the URL are immediately invalidated and removed from the database, rendering them useless.
<script>
const baseUrl = '<?= BASE_URL ?>';
let token = 'user-token';
function regenerateToken() {
const expiryDate = new Date();
expiryDate.setHours(expiryDate.getHours() + 4);
const expiryTimestamp = Date.parse(expiryDate) / 1000;
const targetUrl = baseUrl + 'trongate_tokens/regenerate/' + token + '/' + expiryTimestamp;
fetch(targetUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (response.ok) {
return response.text();
} else {
throw new Error('Network response was not ok');
}
})
.then(data => {
token = data;
alert("Token successfully renewed!");
})
.catch(error => {
alert("Token renewal failed: " + error.message);
});
}
regenerateToken();
</script>