mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
figure out django/powersync url's from authProvider
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
class AppConfig {
|
||||
// TODO: base this off of base URL returned by auth provider
|
||||
static const String djangoUrl = 'http://192.168.1.195:8000';
|
||||
static const String powersyncUrl = 'http://192.168.1.195:8080';
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import 'package:path_provider/path_provider.dart';
|
||||
import 'package:powersync/powersync.dart';
|
||||
import 'package:wger/api_client.dart';
|
||||
|
||||
import './app_config.dart';
|
||||
import './models/schema.dart';
|
||||
|
||||
final log = Logger('powersync-django');
|
||||
@@ -24,10 +23,13 @@ final List<RegExp> fatalResponseCodes = [
|
||||
|
||||
class DjangoConnector extends PowerSyncBackendConnector {
|
||||
PowerSyncDatabase db;
|
||||
String baseUrl;
|
||||
String powersyncUrl;
|
||||
late ApiClient apiClient;
|
||||
|
||||
DjangoConnector(this.db);
|
||||
|
||||
final ApiClient apiClient = const ApiClient(AppConfig.djangoUrl);
|
||||
DjangoConnector(this.db, this.baseUrl, this.powersyncUrl) {
|
||||
apiClient = ApiClient(baseUrl);
|
||||
}
|
||||
|
||||
/// Get a token to authenticate against the PowerSync instance.
|
||||
@override
|
||||
@@ -37,7 +39,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
|
||||
// final wgerSession = await apiClient.getWgerJWTToken();
|
||||
final session = await apiClient.getPowersyncToken();
|
||||
// note: we don't set userId and expires property here. not sure if needed
|
||||
return PowerSyncCredentials(endpoint: AppConfig.powersyncUrl, token: session['token']);
|
||||
return PowerSyncCredentials(endpoint: this.powersyncUrl, token: session['token']);
|
||||
}
|
||||
|
||||
// Upload pending changes to Postgres via Django backend
|
||||
@@ -92,7 +94,7 @@ Future<String> getDatabasePath() async {
|
||||
}
|
||||
|
||||
// opens the database and connects if logged in
|
||||
Future<void> openDatabase(bool connect) async {
|
||||
Future<void> openDatabase(bool connect, String baseUrl, String powersyncUrl) async {
|
||||
// Open the local database
|
||||
if (!_dbInitialized) {
|
||||
db = PowerSyncDatabase(schema: schema, path: await getDatabasePath(), logger: attachedLogger);
|
||||
@@ -103,7 +105,8 @@ Future<void> openDatabase(bool connect) async {
|
||||
if (connect) {
|
||||
// If the user is already logged in, connect immediately.
|
||||
// Otherwise, connect once logged in.
|
||||
final currentConnector = DjangoConnector(db);
|
||||
|
||||
final currentConnector = DjangoConnector(db, baseUrl, powersyncUrl);
|
||||
db.connect(connector: currentConnector);
|
||||
|
||||
// TODO: should we respond to login state changing? like here:
|
||||
|
||||
@@ -77,10 +77,12 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
|
||||
|
||||
Future<void> _setupPowersync() async {
|
||||
final authProvider = context.read<AuthProvider>();
|
||||
print('auth provider says surverurl is ${authProvider.serverUrl}');
|
||||
await openDatabase(false);
|
||||
final baseUrl = authProvider.serverUrl!;
|
||||
final powerSyncUrl = baseUrl.replaceAll(':8000', ':8080');
|
||||
|
||||
final connector = DjangoConnector(db);
|
||||
await openDatabase(false, baseUrl, powerSyncUrl);
|
||||
|
||||
final connector = DjangoConnector(db, baseUrl, powerSyncUrl);
|
||||
try {
|
||||
// TODO: should we cache these credentials? that's what their demo does?
|
||||
// we could maybe get the initial token from the /api/v2/login call
|
||||
@@ -88,7 +90,7 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
|
||||
print('----------');
|
||||
print(credentials);
|
||||
print('----------');
|
||||
await openDatabase(true);
|
||||
await openDatabase(true, baseUrl, powerSyncUrl);
|
||||
} catch (e) {
|
||||
print('fail' + e.toString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user