diff --git a/src/BreCalClient/MainWindow.xaml.cs b/src/BreCalClient/MainWindow.xaml.cs index 61f6fe5..5bb87dd 100644 --- a/src/BreCalClient/MainWindow.xaml.cs +++ b/src/BreCalClient/MainWindow.xaml.cs @@ -14,6 +14,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; +using log4net; using static BreCalClient.Extensions; namespace BreCalClient @@ -23,12 +24,15 @@ namespace BreCalClient /// public partial class MainWindow : Window { + private ILog _log = LogManager.GetLogger(typeof(MainWindow)); private const int SHIPCALL_UPDATE_INTERVAL_SECONDS = 30; #region Fields - + private Timer _timer; + Credentials _credentials; + private readonly Dictionary _allShipcallsDict = new(); private readonly Dictionary _allShipCallsControlDict = new(); @@ -109,12 +113,11 @@ namespace BreCalClient return; } - Credentials credentials = new(username: textUsername.Text.Trim(), - password: textPassword.Password.Trim()); + _credentials = new(username: textUsername.Text.Trim(), password: textPassword.Password.Trim()); try { - _loginResult = await _api.LoginPostAsync(credentials); + _loginResult = await _api.LoginPostAsync(_credentials); if (_loginResult != null) { if (_loginResult.Id > 0) @@ -122,7 +125,8 @@ namespace BreCalClient this.busyIndicator.IsBusy = false; this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token; this.LoadStaticLists(); - this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; + this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; + _timer = new Timer(RefreshToken, null, 4000000, Timeout.Infinite); } } labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; @@ -139,6 +143,29 @@ namespace BreCalClient } } + private void RefreshToken(object? state) + { + try + { + _loginResult = _api.LoginPost(_credentials); + if (_loginResult != null) + { + if (_loginResult.Id > 0) + { + this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token; + } + } + else + { + _log.Error("Token refresh: Renewed login returned empty login result"); + } + } + catch (Exception ex) + { + _log.ErrorFormat("Error refreshing token: {0}", ex.Message); + } + } + private void buttonExit_Click(object sender, RoutedEventArgs e) { this.Close(); diff --git a/src/server/BreCal/impl/login.py b/src/server/BreCal/impl/login.py index cef9e4c..c8c7e8d 100644 --- a/src/server/BreCal/impl/login.py +++ b/src/server/BreCal/impl/login.py @@ -28,7 +28,7 @@ def GetUser(options): "user_name": data[0].user_name, "user_phone": data[0].user_phone } - token = jwt_handler.generate_jwt(payload=result, lifetime=60) # generate token valid 60 mins + token = jwt_handler.generate_jwt(payload=result, lifetime=120) # generate token valid 60 mins result["token"] = token # add token to user data return json.dumps(result), 200, {'Content-Type': 'application/json; charset=utf-8'}