Skip to content

small cleanups & clarifications #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions demos/django-todolist/lib/powersync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
/// Get a token to authenticate against the PowerSync instance.
@override
Future<PowerSyncCredentials?> fetchCredentials() async {
final prefs = await SharedPreferences.getInstance();
final userId = prefs.getString('id');
final userId = await getUserId();
if (userId == null) {
throw Exception('User does not have session');
}
Expand Down Expand Up @@ -88,21 +87,23 @@ late final PowerSyncDatabase db;
// Hacky flag to ensure the database is only initialized once, better to do this with listeners
bool _dbInitialized = false;

/// id of the user currently logged in
Future<String?> getUserId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('id');
}

Future<bool> isLoggedIn() async {
final prefs =
await SharedPreferences.getInstance(); // Initialize SharedPreferences
final userId = prefs.getString('id');
if (userId != null) {
return true;
}
return false;
final userId = await getUserId();
return userId != null;
}

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

// opens the database and connects if logged in
Future<void> openDatabase() async {
// Open the local database
if (!_dbInitialized) {
Expand Down Expand Up @@ -130,9 +131,3 @@ Future<void> openDatabase() async {
Future<void> logout() async {
await db.disconnectAndClear();
}

/// id of the user currently logged in
Future<String?> getUserId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('id');
}