@@ -485,10 +485,13 @@ api.ajax.jsonrpc.request = function (pAPI_URL, pAPI_Method, pAPI_Params, callbac
485
485
// Simulate sync behaviour
486
486
if ( simulateSync )
487
487
api . spinner . start ( ) ;
488
+ // Extend the session if any
489
+ api . cookie . session . extend ( ) ;
488
490
489
491
// Make the Ajax call
490
492
return $ . ajax ( extendedAJAXParams ) ;
491
493
} catch ( error ) {
494
+ console . log ( error )
492
495
// Pop the exception in the Bootstrap Modal
493
496
api . modal . exception ( "An unhandled Ajax exception has occurred. Please try again." ) ;
494
497
return false ;
@@ -654,4 +657,128 @@ api.uri.getNoFooter = function () {
654
657
*/
655
658
api . uri . getBody = function ( ) {
656
659
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
+ } ;
0 commit comments