Tuesday 22 June 2021

Oracle APEX expire session customization

 

Customize expire session notification window in Oracle APEX

Oracle APEX "Your session has expired"


I noticed a few discussions on the OTN Forum about APEX expire session dialog window and its behavior. Since recently we had a client asking for the same behavior so I decided to write a blog about it as an alternative approach.

We all have seen and know the theory regarding these configuration settings:
They dictate how your applications will handle things when user session is about to expire or is already expired. 
 
What is interesting these settings have been changing over past few version of APEX too but people seem to still have issue related to how to handle it the best possible way. 

Normally if your session is about to expire you would see a message: "Would you like to extend it?"
 
Which allows users either to extend their session or not. So far so good. 
 
If they do not extend they get presented with another dialog screen: "Your session has expired"
 
This is where people tend to take different approaches. 
 
Most common is most people get by with it and do not change things. On the other hand why do we need a Cancel and Close button on this popup at all? Which completely resonates with me. 
 
Sometimes you do not want people to be able to cancel from here and see the data on the screen. If for no other reason maybe from security perspective this makes a lot of sense. 
 
I know you can argue yes they can click on Login Again but still......sometimes you just do not want to have them on at all. Perhaps this would make a nice to-do for APEX team to make this available as configuration option for us?

What can we do about it? 
 
Good thing is that APEX team has already done and gave us everything we need to handle this by ourselves. Let's touch on two most obvious options. 
 
Option A - we do not want to show Cancel and Close button at all

How would we do that? 

Since we are talking about popup dialogs we have a way of listening and catching its events in APEX for example when they are opened. 

On page 0 create on page load DA with execute JS code:

$(document).on("dialogopen", function() {  
  console.log('a dialog opened');
});

This gives us all we need to then influence our UI and probably remove these unwanted elements from the page. I am sure there is a CSS way to do this too but here I will explore only JS way. 
 
If you would to run the page now every dialog window would cause it to trigger this but with little tweak by replacing console.log call above with: 
if( $('button:contains("Login Again")').length > 0 )
  {  
  $( "#apex_session_alert_dlg" ).siblings( ".ui-dialog-buttonpane" ).find("button:first-child").hide();
    $( "#apex_session_alert_dlg" ).parent( ".ui-dialog" ).find(".ui-dialog-titlebar-close").hide();
  }
Now we made it so that it only triggers for session expired popup and only if button in there is called Login Again. 
 
Please bare in mind that sometimes people might change this too due to translations etc so adopt the code to it.

This would result that Expires session popup window would appear without Cancel and Close buttons. Yay one down and one to go....
 
Option B -  force a redirect to a login screen
 
Logic here is pretty much similar to before. This time we will leave both buttons to show. 
 
We will be using DA to listen for on close dialog. Let's create a custom DA with Custom Event: dialogclose

That execute JS code: 

if ( $('button:contains("Login Again")').length > 0 ) {
 apex.navigation.redirect ( "&LOGOUT_URL." );
}
Again here we have to check if the specific dialog contained a required button and proceed to avoid it from triggering on every dialog raised by the app.

You can see that if we have detected that we are on the right dialog we will simply redirect the app to the home page which will then ask users to login again.

Save and run the page. 

If session expires and you click Cancel it should redirect you to login screen.

A few more custom options on how to handle your APEX expired sessions.
 
Happy APEXing,
Lino 
 
Note: above code was tested in APEX 20.1 but with no/minimal changes it should work in other versions too

No comments:

Post a Comment