diff --git a/lib/api.dart b/lib/api.dart index adfe7f64..abfe568a 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -196,12 +196,9 @@ class InvenTreeAPI { static const _URL_ROLES = "user/roles/"; static const _URL_ME = "user/me/"; - // Base URL for InvenTree API e.g. http://192.168.120.10:8000 - String _BASE_URL = ""; - // Accessors for various url endpoints String get baseUrl { - String url = _BASE_URL; + String url = profile?.server ?? ""; if (!url.endsWith("/")) { url += "/"; @@ -385,8 +382,6 @@ class InvenTreeAPI { */ Future _connectToServer() async { - debug("Connecting to server..."); - if (!await _checkServer()) { return false; } @@ -441,9 +436,6 @@ class InvenTreeAPI { address = address + "/"; } - // Save the base URL - _BASE_URL = address; - // Cache the "strictHttps" setting, so we can use it later without async requirement _strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool; @@ -522,7 +514,9 @@ class InvenTreeAPI { */ Future fetchToken(UserProfile userProfile, String authHeader) async { - debug("Fetching user token"); + debug("Fetching user token from ${userProfile.server}"); + + profile = userProfile; // Form a name to request the token with String platform_name = "inventree-mobile-app"; diff --git a/lib/settings/login.dart b/lib/settings/login.dart index 041a7097..77fffa23 100644 --- a/lib/settings/login.dart +++ b/lib/settings/login.dart @@ -13,7 +13,7 @@ import "package:inventree/widget/progress.dart"; class InvenTreeLoginWidget extends StatefulWidget { - InvenTreeLoginWidget(this.profile); + InvenTreeLoginWidget(this.profile) : super(); UserProfile profile; diff --git a/lib/settings/select_server.dart b/lib/settings/select_server.dart index 9eac5ac1..371e4397 100644 --- a/lib/settings/select_server.dart +++ b/lib/settings/select_server.dart @@ -84,7 +84,7 @@ class _InvenTreeSelectServerState extends State { await UserProfileDBManager().selectProfile(key); - UserProfile? prf = await UserProfileDBManager().getSelectedProfile(); + UserProfile? prf = await UserProfileDBManager().getProfileByKey(key); if (prf == null) { return; @@ -97,7 +97,8 @@ class _InvenTreeSelectServerState extends State { MaterialPageRoute(builder: (context) => InvenTreeLoginWidget(profile)) ).then((value) async { _reload(); - prf = await UserProfileDBManager().getSelectedProfile(); + // Reload profile + prf = await UserProfileDBManager().getProfileByKey(key); if (prf?.hasToken ?? false) { InvenTreeAPI().connectToServer(prf!).then((result) { _reload(); diff --git a/lib/user_profile.dart b/lib/user_profile.dart index a0234a77..9d17b065 100644 --- a/lib/user_profile.dart +++ b/lib/user_profile.dart @@ -212,6 +212,26 @@ class UserProfileDBManager { return profileList; } + + /* + * Retrieve a profile by key (or null if no match exists) + */ + Future getProfileByKey(int key) async { + final profiles = await getAllProfiles(); + + UserProfile? prf; + + for (UserProfile profile in profiles) { + if (profile.key == key) { + prf = profile; + break; + } + } + + return prf; + } + + /* * Retrieve a profile by name (or null if no match exists) */