Back

Instant? logout user after deleted session

  • 0
  • Users
  • Accounts
  • General
  • Web
Hasira 🥃🪴
22 Nov, 2023, 15:14

Hello, I'm kinda new here and I'm curious.. is there any easy and BETTER way how to check if session was deleted?

I'll give an example what I want to do:

I connected 2 browsers into 1 account I deleted session one of them I want to check deleted session and (instant?) log out user I did checking if session exist every minute (authContext) - it's working and logout user without session, but I'm not sure if it's the best way to make it, every 60s pinging API for response.

TypeScript
useEffect(() => {
  const cookieFallback = localStorage.getItem('cookieFallback')
  if (
    cookieFallback === '[]' ||
    cookieFallback === null ||
    cookieFallback === undefined
  ) {
    navigate('/sign-in')
  }

  checkAuthUser()

  if (isAuthenticated) {
    const interval = setInterval(() => {
      getCurrentSession().then(() => {
        setUser(INITIAL_USER)
        setIsAuthenticated(false)
        navigate('/sign-in')
        console.log('error')
      })
    }, 60000)

    return () => clearInterval(interval)
  }
}, [isAuthenticated])
TL;DR
The user wants to instantly logout a user after deleting a session, without requiring any user interaction. They are currently pinging the API every 60 seconds to check if the session exists, but they are looking for a better solution. One suggestion is to use interceptors to redirect the user to the login page if the session is not available on the next request. Another suggestion is to open a Realtime socket to a random empty Collection and check the token when the socket gets cut off. If the session is deleted, the token will fail and the user can be logged out. There is no clear solution provided in the support thread.
ideclon
22 Nov, 2023, 16:02

If you don’t do any of this, the next time you try to make a request, it’ll fail. You could just have on request failing with no user, do the logout

Hasira 🥃🪴
22 Nov, 2023, 16:12

errors show when I do some action like reload home components, but I would like to logout without user interaction

ideclon
22 Nov, 2023, 16:13

This is after a reload?

Hasira 🥃🪴
22 Nov, 2023, 16:13
Hasira 🥃🪴
22 Nov, 2023, 16:13

forced f5

ideclon
22 Nov, 2023, 16:14

So when does this show?

Hasira 🥃🪴
22 Nov, 2023, 16:14

right after delete session and when I change from home page to profile, explore ect any other route that collect userdata

ideclon
22 Nov, 2023, 16:15

So you could have on that first account.get() (which I expect you have everywhere anyway already?), catch to the logout function

Hasira 🥃🪴
22 Nov, 2023, 16:17

hmm I'll see

ideclon
22 Nov, 2023, 16:18

If you really do want that “no interaction”, so the user is just looking at the page and without touching anything it redirects to the login, I guess you could open a Realtime socket to some random empty Collection, and then if the socket gets cut off, check the token (I’m not really sure how a Realtime socket behaves when the session is killed).

Hasira 🥃🪴
22 Nov, 2023, 16:23

I tried realtime on session.delete but it gives me console log only for still log in sessions ;_;

ideclon
22 Nov, 2023, 16:57

Do you mean you tried to subscribe to session.delete? What was the Event you used?

ideclon
22 Nov, 2023, 16:59

You should be able to subscribe to users.[LOGGED_IN_USER_ID].sessions.[CURRENT_SESSION_ID].delete

Hasira 🥃🪴
22 Nov, 2023, 16:59
TypeScript
client.subscribe('account', (response) => {
    if (response.events.includes('users.*.sessions.*.delete')) {
        //     const payload = response.payload as SessionPayload
        //     const deletedSessionId = payload.$id
        //     // promise.then(
        //     //     function (response) {
        //     //         console.log(response.total) // Success
        //     //     },
        //     //     function (error) {
        //     //         console.log(error) // Failure
        //     //     },
        //     // )
        //     // console.log('test')
        //     // getAllSessions().then((sessions) => {
        //     //     if (sessions) {
        //     //         window.dispatchEvent(new CustomEvent('authSessionDeleted'))
        //     //     }
        //     // })
        //     getCurrentSessionId()
        //         .then((sessionId) => {
        //             if (deletedSessionId === sessionId) {
        //                 account.deleteSession(deletedSessionId)
        //                 window.dispatchEvent(new CustomEvent('authSessionDeleted'))
        //             }
        //         })
        //         .catch((error) => {
        //             Navigate({ to: '/sign-up' })
        //         })
    }

    if (response.events.includes('users.*.sessions.*.create')) {
        console.log('someone logged into your account')
        console.log(response)
    }
})
ideclon
22 Nov, 2023, 17:00

account isn’t a valid Event. Here’s a list of available Events - https://appwrite.io/docs/advanced/platform/events

Hasira 🥃🪴
22 Nov, 2023, 17:01

oh okay, well I think I'll try later, I need a break from this, rn I'll let know about any change

Sachin chaurasiya
22 Nov, 2023, 18:34

@Hasira 🥃🪴 , you can use interceptors. On the next request, if the session is not available, you can redirect the user to the login page.

ideclon
22 Nov, 2023, 19:10

They specifically wanted it to log out without user interaction

Hasira 🥃🪴
22 Nov, 2023, 20:39

anyway

ideclon
22 Nov, 2023, 23:47

Ah, I see there are two different things - Channels and Events. It's not very clear there.

Well yes, once your session has ended, you won't receive any realtime events. I was suggesting above you run the logout when you detect the Realtime connection close (assuming Appwrite closes the connection when the session is deleted).

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more