pocketbase / dart-sdk

PocketBase Dart SDK
https://pub.dev/packages/pocketbase
MIT License
511 stars 51 forks source link

Supporting auth through app cycles #13

Closed nikolajjsj closed 2 years ago

nikolajjsj commented 2 years ago

I couldn't find a way to keep the authenticated state, through app cycles (closening/reopening the app), when authenticated through OAuth. Does the SDK support this, or how would i go about this?

ganigeorgiev commented 2 years ago

The default AuthStore doesn't persist the auth data anywhere and its up to the developers to choose what storage mechanism to use.

In general, you have 2 options:

  1. Create your own store by extending the default one - https://github.com/pocketbase/dart-sdk#authstore _You can find the AuthStore implementation in https://github.com/pocketbase/dart-sdk/blob/master/lib/src/auth_store.dart_

  2. Maintain your own store instance and keep it in sync with the default AuthStore by using the client.authStore.onChange stream.

nikolajjsj commented 2 years ago

Is this a planned feature for the SDK?

If it is i could look into it, saving the token and model using some encrypted storage?

ganigeorgiev commented 2 years ago

It is too user-land specific to be part of the library.

The SDK is intended to be a generic Dart package, not just for Flutter and I'm not sure that there is a solution that will fit all.

Additionally, there are different ways to handle application storage (using something like localstore, sharedpreferences, some key-value file based db, etc.) and it is better to leave the developers decide what is best for their app.

Depending on how many users will want this, eventually I can implement some of those stores inside the library (they all could use the AuthStore as base class), but at least for now I don't have plans for this.

nikolajjsj commented 2 years ago

Okay, i will close this issue, thanks.

Let me know if you need help at some time in the future 👍

ganigeorgiev commented 1 year ago

Just for info in case someone stumble here, v0.13.0 comes with AsyncAuthStore helper that could be used with any external async persistent layer (shared preferences, hive, local file, etc.):

final prefs = await SharedPreferences.getInstance();

final store = AsyncAuthStore(
 save:    (String data) async => prefs.setString('pb_auth', data),
 initial: prefs.getString('pb_auth'),
);

final pb = PocketBase('http://example.com', authStore: store);