Skip to content

Commit ce89998

Browse files
committed
Constant C_API_AJAX_SUCCESS added. Replace hardcoded "success" Ajax responses accordingly.
API Cookie Session handler implemented. See following new methods for reference: >> api.cookie.session.start >> api.cookie.session.extended >> api.cookie.session.ends >> api.cookie.session.intervalRoutine >> api.cookie.session.confirmExtension (virtual method to be extended at the application level)
1 parent 207b48c commit ce89998

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed

src/js/api.constant.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************
22
API - Constant
33
*******************************************************************************/
4-
const C_API_VERSION = "4.1.1";
4+
const C_API_VERSION = "4.2.0";
55

66
/*******************************************************************************
77
API - Constant - URI
@@ -12,3 +12,13 @@ const C_API_URI_NONAVBAR = "nonavbar";
1212
const C_API_URI_NOFOOTER = "nofooter";
1313
// Used as GET parameter to load a body
1414
const C_API_URI_BODY = "body";
15+
16+
/*******************************************************************************
17+
API - Constant - Cookie
18+
*******************************************************************************/
19+
const C_API_COOKIE_SESSION = "session";
20+
21+
/*******************************************************************************
22+
API - Constant - AJAX
23+
*******************************************************************************/
24+
const C_API_AJAX_SUCCESS = "success";

src/js/api.library.js

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,13 @@ api.ajax.jsonrpc.request = function (pAPI_URL, pAPI_Method, pAPI_Params, callbac
485485
// Simulate sync behaviour
486486
if (simulateSync)
487487
api.spinner.start();
488+
// Extend the session if any
489+
api.cookie.session.extend();
488490

489491
// Make the Ajax call
490492
return $.ajax(extendedAJAXParams);
491493
} catch (error) {
494+
console.log(error)
492495
// Pop the exception in the Bootstrap Modal
493496
api.modal.exception("An unhandled Ajax exception has occurred. Please try again.");
494497
return false;
@@ -654,4 +657,128 @@ api.uri.getNoFooter = function () {
654657
*/
655658
api.uri.getBody = function () {
656659
return api.uri.getParam(C_API_URI_BODY);
657-
};
660+
};
661+
662+
/*******************************************************************************
663+
API - Library - Cookie
664+
*******************************************************************************/
665+
api.cookie = {};
666+
api.cookie.session = {};
667+
api.cookie.session.data = {
668+
length: null,
669+
expiry: null,
670+
logoutEndpoint: null,
671+
logoutMethod: null,
672+
}
673+
api.cookie.session.options = {
674+
path: "/",
675+
secure: "true",
676+
sameSite: "strict"
677+
}
678+
679+
/**
680+
* Virtual method to confirm the extension of the Session Cookie
681+
* Override this method in the local application
682+
*/
683+
api.cookie.session.confirmExtension = function () { };
684+
685+
/**
686+
* Start the Session Cookie
687+
*/
688+
api.cookie.session.start = function (pLength, pLogoutEnpoint, pLogoutMethod) {
689+
// Get unix timestamp to deal with numbers rather than dates
690+
var timestamp = Math.round(new Date().getTime() / 1000);
691+
692+
// Set the Session Cookie
693+
Cookies.set(
694+
C_API_COOKIE_SESSION,
695+
$.extend(true, {}, api.cookie.session.data,
696+
{
697+
length: pLength,
698+
expiry: timestamp + pLength,
699+
logoutEndpoint: pLogoutEnpoint,
700+
logoutMethod: pLogoutMethod
701+
}),
702+
api.cookie.session.options);
703+
704+
// Run the routine every second
705+
window.setInterval(api.cookie.session.intervalRoutine, 1000);
706+
};
707+
708+
/**
709+
* Extend the Session Cookie
710+
*/
711+
api.cookie.session.extend = function () {
712+
// Get the session cookie if any
713+
var data = Cookies.getJSON(C_API_COOKIE_SESSION);
714+
715+
if (data) {
716+
// Get unix timestamp to deal with numbers rather than dates
717+
var timestamp = Math.round(new Date().getTime() / 1000);
718+
// Extend Session Cookie
719+
Cookies.set(
720+
C_API_COOKIE_SESSION,
721+
$.extend(true, {}, data,
722+
{
723+
length: data.length,
724+
expiry: timestamp + data.length,
725+
}),
726+
api.cookie.session.options);
727+
}
728+
729+
};
730+
731+
/**
732+
* End the Session Cookie
733+
*/
734+
api.cookie.session.end = function (logoutEndpoint, logoutMethod) {
735+
logoutEndpoint = logoutEndpoint || null;
736+
logoutMethod = logoutMethod || null;
737+
var session = Cookies.getJSON(C_API_COOKIE_SESSION);
738+
// Run the Logout API
739+
api.ajax.jsonrpc.request(
740+
logoutEndpoint || session.logoutEndpoint,
741+
logoutMethod || session.logoutMethod,
742+
null,
743+
"api.cookie.session.endCallbak"
744+
);
745+
// Remove Session Cookie
746+
Cookies.remove(C_API_COOKIE_SESSION);
747+
};
748+
749+
/**
750+
* End the Session Cookie callback
751+
*/
752+
api.cookie.session.endCallbak = function (data) {
753+
if (data == C_API_AJAX_SUCCESS) {
754+
// Force the reload of the application
755+
window.location.href = window.location.pathname;
756+
}
757+
else {
758+
api.modal.exception("An unexpected error has occurred. Please try again.");
759+
}
760+
};
761+
762+
/**
763+
* Routine to run at each interval
764+
*/
765+
api.cookie.session.intervalRoutine = function () {
766+
// Get the session cookie if any
767+
var data = Cookies.getJSON(C_API_COOKIE_SESSION);
768+
if (!data || $.active) {
769+
// If no session cookie or any running Ajax, then do nothing
770+
return;
771+
}
772+
773+
// Get unix timestamp to deal with numbers rather than dates
774+
var timestamp = Math.round(new Date().getTime() / 1000);
775+
if (timestamp > data.expiry) {
776+
// The session has expired, force the logout
777+
api.cookie.session.end();
778+
} else if (timestamp > data.expiry - 60) {
779+
// The session is valid but about to expire (1 minute earlier), confirm the extension
780+
api.cookie.session.confirmExtension();
781+
} else {
782+
// The session is valid, do nothing
783+
}
784+
};

test/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/osano/cookieconsent@3.1.1/build/cookieconsent.min.css">
7979
<script src="https://cdn.jsdelivr.net/gh/osano/cookieconsent@3.1.1/build/cookieconsent.min.js"></script>
8080

81+
<!-- JS-Cookie - https://github.com/js-cookie/js-cookie -->
82+
<script src="https://cdn.jsdelivr.net/gh/js-cookie/js-cookie@v2.2.1/src/js.cookie.min.js"></script>
83+
8184
<!-- **************************************************************************** -->
8285
<!-- Resurces - App - Optional -->
8386
<!-- **************************************************************************** -->
@@ -145,11 +148,6 @@
145148
<script src="https://cdn.jsdelivr.net/gh/select2/select2@4.0.11/dist/js/select2.min.js"></script>
146149
-->
147150

148-
<!-- JS-Cookie - https://github.com/js-cookie/js-cookie -->
149-
<!--
150-
<script src="https://cdn.jsdelivr.net/gh/js-cookie/js-cookie@v2.2.1/src/js.cookie.min.js"></script>
151-
-->
152-
153151
<!-- Datatables - https://datatables.net/ powered by ClooudFlare -->
154152
<!--
155153
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>

0 commit comments

Comments
 (0)