Extend session expiry in phoenix and elixir

Can someone please guide me how to extend session expiry in a phoenix and elixir application by showing a pop up to the user to extend the session within a fixed time interval else timeout?

It is not up to the Phoenix, you need to do that on the client side. So you show “refresh session” modal with link to do any request so the browser will get new session token.

Any reference link for it hauleth? Can you please refer to some examples

Depends on how you store session and how you count expiration date for it.

We have the below configuration:-
user_inactive_expiry_minutes: 15

user_plug has the below module for session expiry:-

defp expiration_datetime do
minutes = Application.get_env(:my_future_now, :user_inactive_expiry_minutes)
Timex.now() |> Timex.shift(minutes: minutes) |> Timex.format!("{ISO:Extended}")
end

Now after the session expires after 15 mins of inactivity we want to show a modal to the user to continue the session further. Need guidance on implementing it.

image

WE want to implement something like the above screenshot when the session is about to expire for a user. I am not getting any reference to implement it.Can you please help.
Let me know if you need any more info.

Can someone help with any reference to implement the above. Thanks

I don’t think anyone will be able to really help you until you better define the situation you are in.

Some questions which might allow people to help you more:

  • how are you currently handling sessions? The code you posted returns an ISO formatted string, which doesn’t tell us anything about how you are actually implementing sessions now.

  • do you currently have any process handling for existing sessions?

  • are you already or open to using Phoenix LiveView?

  • do you understand how GenServers work? If not, I would make that my aim. And once you’re up to speed on how they work, then I would take a look at Process.send_after/3 as means to firing a message after a certain amount of time has passed.

1 Like

Thanks Darian for replying.
I am completely new to phoenix and elixir. I have no idea about GenServers.
We use plug for session handling.

I can paste the complete code for plug if you want to have a look.

Ok, so it seems you’re using Cookies for session handling.

You want to display a modal client-side when the cookie is about to expire, but you don’t have great knowledge of the BEAM, process model or LiveView yet…so for an “easy” solution, you could handle this client-side with JavaScript.

The problem is that JavaScript does not have access to the expiration date of a cookie, unless you put the expiration date in the contents of said cookie yourself.

The dev flow would look something like this:

  1. Adapt Plug’s session cookie to store the expiration date as part of the contents or issue two cookies.
  2. Write some client-side JavaScript to poll the cookie for the expiration date (say, every 30s).
  3. The JS poller checks the data in the cookie & when the JS recognises the cookie is about to expire, then it displays a modal asking the user to refresh their session.
  4. Create a normal Phoenix endpoint which issues a new cookie with a new expiration date, provided the current cookie is still valid.
  5. The “Continue Session” button on the modal should POST to that endpoint.
  6. The other “End session” button on the modal should simply point to the logout view.

This is a pretty raw client side solution without leaning on the BEAM too much.

Hope it helps.

2 Likes

Thanks a lot Darian for your help.
I will try to work on the steps.

1 Like