diff --git a/Mod/API/Authentication.cs b/Mod/API/Authentication.cs
index c1460f3..b30c7e6 100644
--- a/Mod/API/Authentication.cs
+++ b/Mod/API/Authentication.cs
@@ -1,10 +1,45 @@
-using System;
+using ScoreTracker.Configuration;
+using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net.Http;
using System.Threading.Tasks;
namespace ScoreTracker.API
{
+ internal class AuthHelper
+ {
+ public bool IsLoggedIn;
+ public string FailReason = "";
+
+ ///
+ /// Ensure the user is logged in
+ ///
+ /// the task
+ public async Task EnsureLoggedIn()
+ {
+ if (Authentication.IsSignedIn() && await Authentication.ValidateAuthToken())
+ {
+ return; // Already logged in with a valid token
+ }
+
+ await Authentication.LoginUser(
+ token => {
+ IsLoggedIn = true;
+ Request.PersistHeaders(new Dictionary
+ {
+ { "Authorization", $"Bearer {token}" }
+ });
+ },
+ reason =>
+ {
+ FailReason = reason; // Store the reason for failure
+ Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers
+ }
+ );
+ }
+ }
+
internal class Authentication
{
private static bool _signedIn = false;
@@ -54,7 +89,7 @@ namespace ScoreTracker.API
}
Plugin.Log.Info("Logging in...");
- var request = await Request.PostJsonAsync($"{Consts.ApiUrl}/auth/login", new Dictionary