Skip to content

Commit 79aaecf

Browse files
small cleanups & clarifications (#162)
Co-authored-by: Dominic Gunther Bauer <46312751+DominicGBauer@users.noreply.github.com>
1 parent 6fd9c20 commit 79aaecf

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

demos/django-todolist/lib/powersync.dart

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8988
bool _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+
9196
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;
9999
}
100100

101101
Future<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
106107
Future<void> openDatabase() async {
107108
// Open the local database
108109
if (!_dbInitialized) {
@@ -130,9 +131,3 @@ Future<void> openDatabase() async {
130131
Future<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

Comments
 (0)