function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephen KennicuttStephen Kennicutt 

Need to handle user logout from custom Lightning component

I've got a requirement across my custom lightning applications and components to throw a javascript alert if the user has logged out during the application lifecyle (for instance, the user's session times out) and if the user attempts to perform any function in the component (e.g. click a button, load data, etc). As of Winter '18 (v 41.0), Salesforce does not rederict to the authentication page and just reports a 500 error to the console. I really need to give direction to my users and let them know that they need to log into thier Salesforce instance again.

I see that there's an application event called "aura:invalidSession", but because the event's accessibility isn't global, I can't even handle that in any of my custom components. I've tried multiple ways to trap the 500 error, but from what I can see from the console, the error is already being handled inside of the lightning production code and all I'm seeing is the result of the console.log() statement when the error is internally handled.

Are there any known ways to check to see if the user's session is still valid inside a custom lightning component? If it's not possible, this is something that REALLY needs to be implemented in the next release of Lightning for Salesforce.

Thanks!
Rajiv Penagonda 12Rajiv Penagonda 12
UserInfo class has a getSessionID method that should return null for guest users (API version 32 or later). You may access that in your apex controller and pass as a boolean value (example, isLoggedIn) back to the lightning component.

Hope this helps.
-Rajiv
Stephen KennicuttStephen Kennicutt
Rajiv,

I'd thought of a similar method, but I was hoping to avoid spamming the apex server everytime a user executes a purely client side operation. Unfortuantely, it's looking like this may be the only way to handle the check for now.