Back

[SOLVED] Listen to account (session) changes, and logging in when offline.

  • 1
  • Flutter
  • Realtime
  • Accounts
OppositeDragon
19 Jun, 2023, 00:27

Hello, I am struggling to find a way to find out in real time whether a account(session) is active or not, mainly to redirect users automatically to login screen, or allow access to other modules of the app.

I am using go_router for navigation, and I would like to have it take caree to redirect, by listening to changes on the account(session).

On the other hand I need to allow access to the app to users, even when the app is offline. The optimal thing would be that if the user was already authenticated before going offline, then just keep that session, and allow access. If the user has never accessed the app before going offline, the guest login option should be available to allow access.

Is any of this possible, what would be the way to use the sdk to achieve it? The app would be used on all platforms available for Flutter. Thanks in advance.

TL;DR
The user is asking how to listen to account/session changes in real time and redirect users to the login screen. They also want to allow access to the app when offline. The solution is to call `account.get()` on app start and handle exceptions to determine if the user has a valid session or not. If the device is offline, the user can store an offline flag in shared preferences to remember if they have logged in already.
Drake
19 Jun, 2023, 01:53

The way we typically handle this is to call account.get() on app start. If that doesn't throw an exception, the user has a valid session and can proceed. If there is an exception, redirect the user to the login page.

For offline, you'll have to maintain some sort of offline flag. For example, you can store in shared preferences a true or false for if they logged in already or not

OppositeDragon
19 Jun, 2023, 02:54

Thanks @Steven. Will the account.get() work if the device is offline?

Drake
19 Jun, 2023, 03:57

No, my second paragraph was for offline

OppositeDragon
19 Jun, 2023, 21:09

I understand, but then will I be able to get the logged in user if the connection drops?

Drake
19 Jun, 2023, 21:10

Of course not

OppositeDragon
19 Jun, 2023, 21:11

thank you @Steven

Guille
19 Jun, 2023, 21:17

@OppositeDragon can we set this issue as solved?

OppositeDragon
19 Jun, 2023, 21:18

yes, thank you!

Guille
20 Jun, 2023, 22:11

[SOLVED] Listen to account (session) changes, and logging in when offline.

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