Skip to content

Commit c166799

Browse files
committed
small cleanups & clarifications
1 parent de5b7dd commit c166799

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

demos/django-todolist/lib/powersync.dart

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
3434
/// Get a token to authenticate against the PowerSync instance.
3535
@override
3636
Future<PowerSyncCredentials?> fetchCredentials() async {
37-
final prefs = await SharedPreferences.getInstance();
38-
final userId = prefs.getString('id');
37+
final userId = await getUserId();
3938
if (userId == null) {
4039
throw Exception('User does not have session');
4140
}
@@ -90,21 +89,23 @@ late final PowerSyncDatabase db;
9089
// Hacky flag to ensure the database is only initialized once, better to do this with listeners
9190
bool _dbInitialized = false;
9291

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+
9398
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;
101101
}
102102

103103
Future<String> getDatabasePath() async {
104104
final dir = await getApplicationSupportDirectory();
105105
return join(dir.path, 'powersync-demo.db');
106106
}
107107

108+
// opens the database and connects if logged in
108109
Future<void> openDatabase() async {
109110
// Open the local database
110111
if (!_dbInitialized) {
@@ -122,7 +123,7 @@ Future<void> openDatabase() async {
122123

123124
if (await isLoggedIn()) {
124125
// If the user is already logged in, connect immediately.
125-
// Otherwise, connect once logged in.
126+
// Otherwise, the LoginPage will connect once logged in.
126127
currentConnector = DjangoConnector(db);
127128
db.connect(connector: currentConnector);
128129
}
@@ -132,9 +133,3 @@ Future<void> openDatabase() async {
132133
Future<void> logout() async {
133134
await db.disconnectAndClear();
134135
}
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

Comments
 (0)