Skip to content

Commit

Permalink
Pass profile to API when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingersGat committed Oct 22, 2023
1 parent a4b871a commit ecc147e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
3 changes: 3 additions & 0 deletions assets/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
---

- Add ability to scan in received items using supplier barcodes
- Store API token, rather than username:password
- Ensure that user will lose access if token is revoked by server


### 0.12.8 - September 2023
---
Expand Down
31 changes: 15 additions & 16 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,7 @@ class InvenTreeAPI {

Map<String, dynamic> _data = response.asMap();

if (_data is Map<String, dynamic>) {
serverInfo = {..._data};
}
serverInfo = {..._data};

if (serverVersion.isEmpty) {
showServerError(
Expand Down Expand Up @@ -505,7 +503,17 @@ class InvenTreeAPI {
Future<bool> _checkAuth() async {
debug("Checking user auth @ ${_URL_ME}");
final response = await get(_URL_ME);
return response.successful() && response.statusCode == 200;

if (response.successful() && response.statusCode == 200) {
return true;
} else {
debug("Auth request failed: Server returned status ${response.statusCode}");
if (response.data != null) {
debug("Server response: ${response.data.toString()}");
}

return false;
}
}

/*
Expand Down Expand Up @@ -588,7 +596,7 @@ class InvenTreeAPI {
// Save the token to the user profile
userProfile.token = (data["token"] ?? "") as String;

debug("Received token from server");
debug("Received token from server: ${userProfile.token}");

bool result = await UserProfileDBManager().updateProfile(userProfile);

Expand All @@ -610,24 +618,15 @@ class InvenTreeAPI {
_connectionStatusChanged();
}

/*
* Check if the selected profile has an API token
*/
Future<bool> checkHasToken() async {

final _prf = await UserProfileDBManager().getSelectedProfile();
return _prf?.hasToken ?? false;
}

/* Public facing connection function.
*/
Future<bool> connectToServer() async {
Future<bool> connectToServer(UserProfile prf) async {

// Ensure server is first disconnected
disconnectFromServer();

// Load selected profile
profile = await UserProfileDBManager().getSelectedProfile();
profile = prf;

if (profile == null) {
showSnackIcon(
Expand Down
11 changes: 7 additions & 4 deletions lib/settings/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import "dart:convert";

import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:font_awesome_flutter/font_awesome_flutter.dart";
import "package:inventree/app_colors.dart";
Expand All @@ -11,9 +10,6 @@ import "package:inventree/l10.dart";
import "package:inventree/api.dart";
import "package:inventree/widget/progress.dart";

/**
* clas
*/

class InvenTreeLoginWidget extends StatefulWidget {

Expand Down Expand Up @@ -46,6 +42,13 @@ class _InvenTreeLoginState extends State<InvenTreeLoginWidget> {

if (valid) {

// Dismiss the keyboard
FocusScopeNode currentFocus = FocusScope.of(context);

if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}

showLoadingOverlay(context);

// Attempt login
Expand Down
15 changes: 11 additions & 4 deletions lib/settings/select_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,22 @@ class _InvenTreeSelectServerState extends State<InvenTreeSelectServerWidget> {

await UserProfileDBManager().selectProfile(key);

UserProfile? prf = await UserProfileDBManager().getSelectedProfile();

if (prf == null) {
return;
}

// First check if the profile has an associate token
if (!await InvenTreeAPI().checkHasToken()) {
if (!prf.hasToken) {
// Redirect user to login screen
Navigator.push(context,
MaterialPageRoute(builder: (context) => InvenTreeLoginWidget(profile))
).then((value) async {
_reload();
if (await InvenTreeAPI().checkHasToken()) {
InvenTreeAPI().connectToServer().then((result) {
prf = await UserProfileDBManager().getSelectedProfile();
if (prf?.hasToken ?? false) {
InvenTreeAPI().connectToServer(prf!).then((result) {
_reload();
});
}
Expand All @@ -109,7 +116,7 @@ class _InvenTreeSelectServerState extends State<InvenTreeSelectServerWidget> {
_reload();

// Attempt server login (this will load the newly selected profile
InvenTreeAPI().connectToServer().then((result) {
InvenTreeAPI().connectToServer(prf).then((result) {
_reload();
});

Expand Down
2 changes: 1 addition & 1 deletion lib/widget/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> with BaseWidgetPr
if (!InvenTreeAPI().isConnected() && !InvenTreeAPI().isConnecting()) {

// Attempt server connection
InvenTreeAPI().connectToServer().then((result) {
InvenTreeAPI().connectToServer(_profile!).then((result) {
if (mounted) {
setState(() {});
}
Expand Down

0 comments on commit ecc147e

Please sign in to comment.