Next.js Discord

Discord Forum

How to terminate a NextAuth session when a user closes their browser?

Unanswered
Selkirk Rex posted this in #help-forum
Open in Discord
Selkirk RexOP
I'm looking for a way to ensure user sessions end upon browser closure.

4 Replies

WHY do you need this. There may be another solution that by purely focussing on sessions is preventing you from seeing. Tell me what you're trying to achieve
Selkirk RexOP
I am using a third party application which terminates user session when browser is closed. Consequently, I need to ensure that my Next.js application also logs out the user in this scenario.
@Selkirk Rex I am using a third party application which terminates user session when browser is closed. Consequently, I need to ensure that my Next.js application also logs out the user in this scenario.
A few ideas:

0. Most likely they're storing the logged in session in memory and not with a session cookie, or some sort of session storage

1. Use window.onbeforeunload, this may work for your uses to trigger a logout

2. Session cookie is hashed using something that exists in memory or localstorage. Gross version of 0. above

3. They might use a use a live websocket, and closing the browser closes the connection