Skip to content

Commit

Permalink
Merge pull request #234 from Chrisincr/Session-keep-alive
Browse files Browse the repository at this point in the history
added Alert that triggers ajax request to extend session if clicked
  • Loading branch information
jhandel authored Oct 29, 2024
2 parents 89e4bef + 677586d commit 14b6414
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@
* You can remove these routes once you've connected the
* routes you want in your application.
*/

$builder->fallbacks();
});
$routes->connect('/keepalive', ['controller' => 'Sessions', 'action' => 'keepalive']);
$routes->scope('/images', function ($routes) {
$routes->registerMiddleware('glide', new \ADmad\Glide\Middleware\GlideMiddleware([
// Run this middleware only for URLs starting with specified string. Default null.
Expand Down Expand Up @@ -157,4 +159,4 @@
* });
* ```
*/
};
};
25 changes: 25 additions & 0 deletions app/src/Controller/SessionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Controller;

use Cake\Http\ServerRequest;
use Cake\Http\Response;

class SessionsController extends AppController
{
public function keepalive()
{

// $session = $this->request->getSession();
// if ($session->check('count')) {
// $session->write(['count' => $session->read('count') + 1]);
// } else {
// $session->write(['count' => 1]);
// }
$response = $this->response;
$response = $response->withStringBody('My Body');
$response = $response->withType('application/json')
->withStringBody(json_encode(['response' => 'Session extended']));
return $response;
}
}
15 changes: 11 additions & 4 deletions app/templates/layout/TwitterBootstrap/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
?>

<body <?= $this->fetch("tb_body_attrs") ?>>
<?php
echo $this->Html->script('sessionExtender')
?>
<script>
url = <?= json_encode((empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]" . "/keepalive") ?>;
extendSesh(url)
</script>
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<div class="navbar-brand col-md-3 col-lg-2 me-0 px-3">
<?= $this->Html->image($this->KMP->getAppSetting("KMP.BannerLogo", "badge.png"), [
Expand All @@ -46,9 +53,9 @@
$css = $parts[1];
}
?>
<li class="nav-item text-nowrap mx-1">
<a class="btn btn-outline-secondary <?= $css ?>" href="<?= $url ?>"><?= $key ?></a>
</li>
<li class="nav-item text-nowrap mx-1">
<a class="btn btn-outline-secondary <?= $css ?>" href="<?= $url ?>"><?= $key ?></a>
</li>
<?php endforeach; ?>
<li class="nav-item text-nowrap mx-1">
<?= $this->Html->link(
Expand Down Expand Up @@ -98,4 +105,4 @@
$this->KMP->endBlock();
}
echo $this->fetch("content");
echo $this->element('copyrightFooter', []);
echo $this->element('copyrightFooter', []);
36 changes: 32 additions & 4 deletions app/templates/layout/TwitterBootstrap/view_record.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
?>

<body <?= $this->fetch("tb_body_attrs") ?>>
<?php
echo $this->Html->script('sessionExtender')
?>
<script>
url = <?= json_encode((empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]" . "/keepalive") ?>;
extendSesh(url)
</script>
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<div class="navbar-brand col-md-3 col-lg-2 me-0 px-3">
<?= $this->Html->image($this->KMP->getAppSetting("KMP.BannerLogo", "badge.png"), [
Expand All @@ -45,10 +52,31 @@
$css = $parts[1];
}
?>
<li class="nav-item text-nowrap mx-1">
<a class="btn btn-outline-secondary <?= $css ?>" href="<?= $url ?>"><?= $key ?></a>
</li>
<li class="nav-item text-nowrap mx-1">
<a class="btn btn-outline-secondary <?= $css ?>" href="<?= $url ?>"><?= $key ?></a>
</li>
<?php endforeach; ?>
<!-- <script>
function extendSesh() {
console.log('start session')
setTimeout(function() {
alert('Session Expiring! Click ok to extend session.');
fetch(
<?= json_encode((empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]" . "/keepalive") ?>
)
.then(res => {
return res.json()
})
.then(data => {
console.log(data.response)
extendSesh()
})

}, 45000)

}
extendSesh()
</script> -->
<li class="nav-item text-nowrap mx-1">
<?= $this->Html->link(
__("Sign out"),
Expand Down Expand Up @@ -146,4 +174,4 @@
}

echo $this->fetch("content");
echo $this->element('copyrightFooter', []);
echo $this->element('copyrightFooter', []);
15 changes: 15 additions & 0 deletions app/webroot/js/sessionExtender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function extendSesh(url) {
setTimeout(function () {
alert('Session Expiring! Click ok to extend session.');
fetch(url)
.then(res => {
return res.json()
})
.then(data => {
console.log(data.response)
extendSesh(url)
})
//minutes * 60000 miliseconds per minute
}, 25 * 60000)

}

0 comments on commit 14b6414

Please sign in to comment.