Logan
Logan

Reputation: 2505

How to check if Liferay session has timed out in Javascript?

We're using Liferay for management of portlets and session, and rest of the application is built using angular. The portlets within themselves are single page applications. The problem that we're facing is that liferay doesn't redirect the user when session times out and an ajax request is performed. If the user navigates to some other portlet or reloads the page, liferay does identify that session has timed out, but if the user clicks on some link on the page, which doesn't result in page reload, liferay doesn't identify that session has timed out.

To fix this issue, I am trying to identify using Javascript code, if the liferay session is valid or not. I am able to access Liferay.Session Object in my JS, but am not sure what function or field I can use to identify if session is valid or not.

Can someone help me with this?

Below is the code using which we're getting access to the Lieray session.

AUI().use('liferay-session', function(A) {
  //Liferay.Session
  //some logic
}

We're using Liferay 6.0.2.

Upvotes: 0

Views: 3238

Answers (5)

Vladimir Mansurov
Vladimir Mansurov

Reputation: 1

It works for me. I just put this code in a script for my theme. My theme is used for the entire site. https://liferay.dev/blogs/-/blogs/custom-liferay-session-on-expiration

Upvotes: 0

Varun Chawla
Varun Chawla

Reputation: 323

You can set a hook which will get triggered on session timeout.

As mention here https://stackoverflow.com/a/28302153/2834053

Hope this helps.

Upvotes: 0

Lifehack
Lifehack

Reputation: 2151

Since you have access to Liferay.Session object you should be able to do this to get the state of the session:

Liferay.Session.get('sessionState');

It will return "active" if the session is still active.

Upvotes: 1

Jorge B.
Jorge B.

Reputation: 51

You can poll through AJAX call and retrieve the status of the session from Java Code or create a hidden field with the time that remains in each page load. Then in Javascript do a substraction from current time and in the case that the user is out-of-time you can perform any action that you want.

Is a sticky approach but could be useful for your issue.

Upvotes: 0

Daniele Baggio
Daniele Baggio

Reputation: 2257

Consider you can setup the portal to automatic autoextend session time. The session will never expired.

Setup a portal-ext.properties file with this custom value

session.timeout.warning=0
session.timeout.auto.extend=true

It could be a real scenario for your app?

Upvotes: 0

Related Questions