@@ -32,8 +32,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
3232 /// Get a token to authenticate against the PowerSync instance.
3333 @override
3434 Future <PowerSyncCredentials ?> fetchCredentials () async {
35- final prefs = await SharedPreferences .getInstance ();
36- final userId = prefs.getString ('id' );
35+ final userId = await getUserId ();
3736 if (userId == null ) {
3837 throw Exception ('User does not have session' );
3938 }
@@ -88,21 +87,23 @@ late final PowerSyncDatabase db;
8887// Hacky flag to ensure the database is only initialized once, better to do this with listeners
8988bool _dbInitialized = false ;
9089
90+ /// id of the user currently logged in
91+ Future <String ?> getUserId () async {
92+ final prefs = await SharedPreferences .getInstance ();
93+ return prefs.getString ('id' );
94+ }
95+
9196Future <bool > isLoggedIn () async {
92- final prefs =
93- await SharedPreferences .getInstance (); // Initialize SharedPreferences
94- final userId = prefs.getString ('id' );
95- if (userId != null ) {
96- return true ;
97- }
98- return false ;
97+ final userId = await getUserId ();
98+ return userId != null ;
9999}
100100
101101Future <String > getDatabasePath () async {
102102 final dir = await getApplicationSupportDirectory ();
103103 return join (dir.path, 'powersync-demo.db' );
104104}
105105
106+ // opens the database and connects if logged in
106107Future <void > openDatabase () async {
107108 // Open the local database
108109 if (! _dbInitialized) {
@@ -130,9 +131,3 @@ Future<void> openDatabase() async {
130131Future <void > logout () async {
131132 await db.disconnectAndClear ();
132133}
133-
134- /// id of the user currently logged in
135- Future <String ?> getUserId () async {
136- final prefs = await SharedPreferences .getInstance ();
137- return prefs.getString ('id' );
138- }
0 commit comments