Back

[SOLVED] How to Listen to access token

  • 1
  • Self Hosted
  • Accounts
  • Web
yalondpsi
9 Sep, 2023, 07:20

(cloud version - web app) I gave the access token a one hour time period to be valid (at the appwrite dashboard). How to listen to it when it's revoked and give the user some notification, or to use a refresh token? Thank you

TL;DR
The user is seeking help on how to listen to an access token and receive notifications when it is revoked. They receive suggestions to check if a session exists before any user action and to set the session length in the Appwrite console. They also ask if they should register to the event with the server-sdk or use other methods like WorkManager in Android or BGAppRefreshTask in iOS. The user also asks which event happens when an access token is no longer valid. The solution is to set up Realtime to listen to the `users.*.sessions.*.delete` event or to use authentication events like `users.*.recovery.*
yalondpsi
9 Sep, 2023, 07:49

How to Listen to access token

darShan
9 Sep, 2023, 08:03

Whats the token here? TokenObject generated via Accounts API? Where is the token generated? Client side? Also, there are a few events like users.*.recovery.*, you could use those if they fall into your logic. https://appwrite.io/docs/events#authentication-events

yalondpsi
9 Sep, 2023, 08:12

@darShan The token is generated by the client after the user logged in via email/password or google

yalondpsi
9 Sep, 2023, 08:17

@darShan I will write down an example: The user logged in and was redirected to the dashboard of my app. He was idle for more than one hour. I want to exit it to the login screen (because the session is no longer valid) should I listen to "users..recovery." event ? which event happens when a access token is no longer valid?

yalondpsi
9 Sep, 2023, 08:18

or am I mistaken in my approach ?

yalondpsi
9 Sep, 2023, 08:22

users..sessions..delete ?

safwan
9 Sep, 2023, 09:00

users.*.sessions.*.delete could work if you setup Realtime to listen to that event

darShan
9 Sep, 2023, 09:05

There are multiple ways to handle this.

  1. As @safwan suggested,
  2. If the client side is a mobile app, use WorkManager on Android or BGAppRefreshTask in iOS or workmanager plugin on Flutter with the delay value set to the token's validity.
yalondpsi
9 Sep, 2023, 09:27

Thank you @darShan and @safwan The user.sessions..delete does not work - when I'm trying to register to it via the client api. Should I register to it with the server-sdk?

darShan
9 Sep, 2023, 09:36

user.sessions..delete

  1. You have an extra dot after sessions.
  2. This event is fired when a session is deleted, example: account.deleteSession('current') or account.deleteSessions()

From the example you gave above (sorry I missed that), I see you want to limit the session length [if I understand correctly]. This can be done on console side & its pretty easy! Go to appwrite console > Auth > Security > Session Length > 1 Hours.

yalondpsi
9 Sep, 2023, 09:39

@darShan Yes - this is what I did in the appwrite console. Anyway, I will add a check before any user action to see if a session is exists - I think this is solving my problem πŸ™‚ account.getSession('current')

darShan
9 Sep, 2023, 09:42

I don't think that's necessary, if the session is no more valid then none of the requests to console will be successful. Also, instead of fresh fetching the session object via account.getSession('current'), save the creation time to disk & use that to compare. would be much faster and that'd work even in low/bad network as well.

yalondpsi
9 Sep, 2023, 09:42

@darShan That's a great idea ! Thank you!

darShan
12 Sep, 2023, 09:33

I believe this thread can be marked as [SOLVED] now.

safwan
12 Sep, 2023, 11:30

[SOLVED] How to Listen to access token

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