@@ -34,8 +34,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
34
34
/// Get a token to authenticate against the PowerSync instance.
35
35
@override
36
36
Future <PowerSyncCredentials ?> fetchCredentials () async {
37
- final prefs = await SharedPreferences .getInstance ();
38
- final userId = prefs.getString ('id' );
37
+ final userId = await getUserId ();
39
38
if (userId == null ) {
40
39
throw Exception ('User does not have session' );
41
40
}
@@ -90,21 +89,23 @@ late final PowerSyncDatabase db;
90
89
// Hacky flag to ensure the database is only initialized once, better to do this with listeners
91
90
bool _dbInitialized = false ;
92
91
92
+ /// id of the user currently logged in
93
+ Future <String ?> getUserId () async {
94
+ final prefs = await SharedPreferences .getInstance ();
95
+ return prefs.getString ('id' );
96
+ }
97
+
93
98
Future <bool > isLoggedIn () async {
94
- final prefs =
95
- await SharedPreferences .getInstance (); // Initialize SharedPreferences
96
- final userId = prefs.getString ('id' );
97
- if (userId != null ) {
98
- return true ;
99
- }
100
- return false ;
99
+ final userId = await getUserId ();
100
+ return userId != null ;
101
101
}
102
102
103
103
Future <String > getDatabasePath () async {
104
104
final dir = await getApplicationSupportDirectory ();
105
105
return join (dir.path, 'powersync-demo.db' );
106
106
}
107
107
108
+ // opens the database and connects if logged in
108
109
Future <void > openDatabase () async {
109
110
// Open the local database
110
111
if (! _dbInitialized) {
@@ -122,7 +123,7 @@ Future<void> openDatabase() async {
122
123
123
124
if (await isLoggedIn ()) {
124
125
// If the user is already logged in, connect immediately.
125
- // Otherwise, connect once logged in.
126
+ // Otherwise, the LoginPage will connect once logged in.
126
127
currentConnector = DjangoConnector (db);
127
128
db.connect (connector: currentConnector);
128
129
}
@@ -132,9 +133,3 @@ Future<void> openDatabase() async {
132
133
Future <void > logout () async {
133
134
await db.disconnectAndClear ();
134
135
}
135
-
136
- /// id of the user currently logged in
137
- Future <String ?> getUserId () async {
138
- final prefs = await SharedPreferences .getInstance ();
139
- return prefs.getString ('id' );
140
- }
0 commit comments