Back

[SOLVED] How to subscribe account events as guest?

  • 0
  • Web
  • Accounts
  • Realtime
Richard Trudel
28 Feb, 2023, 21:01

I need to "listen" to account events responsively to update my UI when user sign in and out (so, when a session is created and deleted), but if i subscribe as guest, it seems events are never fired. Do you know another way to achieve to listen to login/logout events??

TL;DR
Title: [SOLVED] How to subscribe account events as guest? Solution: Instead of using the Appwrite JavaScript library, it is recommended to use state management and call account.get() after creating a session. This will allow you to listen to login/logout events responsively and update your UI accordingly.
Richard Trudel
28 Feb, 2023, 21:14

The best example of what I'm trying to achieve is to reproduce onAuthStateChanged behavior from Firebase

Drake
28 Feb, 2023, 21:20

Honestly, don't try to reproduce this from firebase. Build an app as if that wasn't a thing

Richard Trudel
28 Feb, 2023, 21:22

I guess I will need to use some state management to avoid to call account.get() in setInterval or something very ugly...

Drake
28 Feb, 2023, 21:26

instead of that, i would call account.get() after creating a session and store that in your state management. that's it

Richard Trudel
28 Feb, 2023, 21:27

Ok, I'll try this.

And just to be sure, that also mean this code cannot be used until the user is signed in?

TypeScript
appwrite.subscribe('account', async response => {
  // do something
});
Drake
28 Feb, 2023, 21:27

i wouldn't do that.

after deleting the session, clear the user from your state management. that's it

Richard Trudel
28 Feb, 2023, 21:29

Ok, that make sense, but I wanted to give a try without using a store, only using Appwrite Javascript 🙂

Drake
28 Feb, 2023, 21:30

that's fine too

Richard Trudel
28 Feb, 2023, 21:31

Thank you 🙂

Drake
28 Feb, 2023, 21:32

[SOLVED] How to subscribe account events as guest?

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