Logout User when browser or tab closed

1,651 views
Skip to first unread message

abhishek agarwal

unread,
Dec 1, 2022, 11:36:17 PM12/1/22
to Angular and AngularJS discussion
I want to call logout api when user close the browser or tab so the user login session clear from the backend i try to use onbeforeunload but this function also calls on reload i only want to call api when  user close browser.

Manjari Singh

unread,
Dec 2, 2022, 12:25:31 AM12/2/22
to ang...@googlegroups.com
Perhaps 
You can do timeout
Or check JavaScript inbuilt objects
While maintaining the session 
Hope that helps 

On Fri, 2 Dec 2022 at 10:06 AM, abhishek agarwal <abhishe...@gmail.com> wrote:
I want to call logout api when user close the browser or tab so the user login session clear from the backend i try to use onbeforeunload but this function also calls on reload i only want to call api when  user close browser.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/407eada2-7e6c-40a2-8162-0730bbc7bfb7n%40googlegroups.com.
--
Sent from Gmail Mobile

bastien lemaire

unread,
Dec 2, 2022, 3:06:05 AM12/2/22
to ang...@googlegroups.com
Unfortunately, there is no guaranteed way to do that. You need to implement a heartbeat mechanism or ensure the app logout on start (strange but ensures the user has to login everytime)

Bastien Lemaire


On Fri, 2 Dec 2022 at 05:36, abhishek agarwal <abhishe...@gmail.com> wrote:
I want to call logout api when user close the browser or tab so the user login session clear from the backend i try to use onbeforeunload but this function also calls on reload i only want to call api when  user close browser.

--

jawahar chellaswami

unread,
Dec 2, 2022, 12:17:27 PM12/2/22
to Angular and AngularJS discussion
In java script we have  window.onbeforeunload  event you can use that

bastien lemaire

unread,
Dec 2, 2022, 7:38:02 PM12/2/22
to ang...@googlegroups.com
Trust me window.onbeforeunload is not the correct way to do such task. As specified on the usage notes, the event is not guaranteed to fire.
There is in fact not guaranteed way to do what you require appart from using a heartbeat mechanism...



abhishek agarwal

unread,
Dec 2, 2022, 11:30:51 PM12/2/22
to ang...@googlegroups.com
This method is also called on refresh I don't want to logout the user when he refresh the page I  only want to logout the user when he close the browser or tab. Can you please explain what is heartbeat mechanism?

Reply all
Reply to author
Forward
0 new messages