Refactor authentication logic to use appsettings configuration. Add settings helpers, example config file, and dynamic API base URL support.

This commit is contained in:
Andre Beging
2025-12-11 18:44:00 +01:00
parent 67260ae450
commit 9f82cf491c
7 changed files with 152 additions and 27 deletions

View File

@@ -63,6 +63,7 @@ public static class AuthHelper
// Try to load CSRF token from file
var csrfToken = await LoadCsrfTokenAsync();
csrfToken = null;
// If no valid token found, call login endpoint to get a new one
if (string.IsNullOrWhiteSpace(csrfToken))

77
Helper/Settings.cs Normal file
View File

@@ -0,0 +1,77 @@
using Microsoft.Extensions.Configuration;
namespace FsTool.Helpers
{
/// <summary>
/// Represents the root configuration settings for the application, including API and credentials.
/// </summary>
public class AppSettings
{
/// <summary>
/// Gets the API-related settings.
/// </summary>
public ApiSettings Api { get; init; } = new();
/// <summary>
/// Gets the credentials settings for authentication.
/// </summary>
public CredentialsSettings Credentials { get; init; } = new();
}
/// <summary>
/// Contains settings related to the API endpoints.
/// </summary>
public class ApiSettings
{
/// <summary>
/// Gets the base URL for the API, without the /api path segment.
/// </summary>
public string BaseUrl { get; init; } = "https://beta.foodsharing.de";
}
/// <summary>
/// Contains credentials and authentication settings.
/// </summary>
public class CredentialsSettings
{
/// <summary>
/// Gets the email address used for login.
/// </summary>
public string Email { get; init; } = string.Empty;
/// <summary>
/// Gets the password used for login.
/// </summary>
public string Password { get; init; } = string.Empty;
/// <summary>
/// Gets a value indicating whether two-factor authentication is enabled.
/// </summary>
public bool TwoFactorEnabled { get; init; }
}
/// <summary>
/// Provides access to the current application settings loaded from configuration.
/// </summary>
public static class SettingsProvider
{
private static AppSettings? _current;
/// <summary>
/// Gets the current application settings instance.
/// </summary>
public static AppSettings Current => _current ??= Load();
private static AppSettings Load()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var settings = new AppSettings();
configuration.Bind(settings);
return settings;
}
}
}