@@ -32,8 +32,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
32
32
/// Get a token to authenticate against the PowerSync instance.
33
33
@override
34
34
Future <PowerSyncCredentials ?> fetchCredentials () async {
35
- final prefs = await SharedPreferences .getInstance ();
36
- final userId = prefs.getString ('id' );
35
+ final userId = await getUserId ();
37
36
if (userId == null ) {
38
37
throw Exception ('User does not have session' );
39
38
}
@@ -88,21 +87,23 @@ late final PowerSyncDatabase db;
88
87
// Hacky flag to ensure the database is only initialized once, better to do this with listeners
89
88
bool _dbInitialized = false ;
90
89
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
+
91
96
Future <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 ;
99
99
}
100
100
101
101
Future <String > getDatabasePath () async {
102
102
final dir = await getApplicationSupportDirectory ();
103
103
return join (dir.path, 'powersync-demo.db' );
104
104
}
105
105
106
+ // opens the database and connects if logged in
106
107
Future <void > openDatabase () async {
107
108
// Open the local database
108
109
if (! _dbInitialized) {
@@ -130,9 +131,3 @@ Future<void> openDatabase() async {
130
131
Future <void > logout () async {
131
132
await db.disconnectAndClear ();
132
133
}
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