Mod
@ -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 = "";
|
||||
|
||||
/// <summary>
|
||||
/// Ensure the user is logged in
|
||||
/// </summary>
|
||||
/// <returns>the task</returns>
|
||||
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<string, string>
|
||||
{
|
||||
{ "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<object, object> {
|
||||
var request = await Request.PostJsonAsync($"{PluginConfig.Instance.ApiUrl}/auth/login", new Dictionary<object, object> {
|
||||
{ "ticket", ticket }
|
||||
}, false);
|
||||
if (request.IsSuccessStatusCode)
|
||||
@ -88,7 +123,7 @@ namespace ScoreTracker.API
|
||||
return false;
|
||||
}
|
||||
|
||||
var request = await Request.PostJsonAsync($"{Consts.ApiUrl}/auth/validate", new Dictionary<object, object> {
|
||||
var request = await Request.PostJsonAsync($"{PluginConfig.Instance.ApiUrl}/auth/validate", new Dictionary<object, object> {
|
||||
{ "token", _authToken }
|
||||
}, false);
|
||||
|
||||
|
@ -9,36 +9,7 @@ namespace ScoreTracker.API
|
||||
{
|
||||
internal class Request
|
||||
{
|
||||
private static readonly HttpClient client = new HttpClient();
|
||||
|
||||
private class AuthHelper
|
||||
{
|
||||
public bool IsLoggedIn;
|
||||
public string FailReason = "";
|
||||
|
||||
public async Task EnsureLoggedIn()
|
||||
{
|
||||
if (Authentication.IsSignedIn() && await Authentication.ValidateAuthToken())
|
||||
{
|
||||
return; // Already logged in with a valid token
|
||||
}
|
||||
|
||||
await Authentication.LoginUser(
|
||||
token => {
|
||||
IsLoggedIn = true;
|
||||
PersistHeaders(new Dictionary<string, string>
|
||||
{
|
||||
{ "Authorization", $"Bearer {token}" }
|
||||
});
|
||||
},
|
||||
reason =>
|
||||
{
|
||||
FailReason = reason; // Store the reason for failure
|
||||
client.DefaultRequestHeaders.Clear(); // Clear headers
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
internal static readonly HttpClient HttpClient = new HttpClient();
|
||||
|
||||
/// <summary>
|
||||
/// Persist the given headers for all future requests
|
||||
@ -46,10 +17,10 @@ namespace ScoreTracker.API
|
||||
/// <param name="headers">the headers to persist</param>
|
||||
public static void PersistHeaders(Dictionary<string, string> headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Clear(); // Clear existing headers
|
||||
HttpClient.DefaultRequestHeaders.Clear(); // Clear existing headers
|
||||
foreach (var header in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
HttpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +46,7 @@ namespace ScoreTracker.API
|
||||
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
|
||||
|
||||
// Send the POST request
|
||||
var response = await client.PostAsync(url, content);
|
||||
var response = await HttpClient.PostAsync(url, content);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user