Refactor authentication logic to use appsettings configuration. Add settings helpers, example config file, and dynamic API base URL support.
This commit is contained in:
@@ -9,32 +9,50 @@ namespace FsTool.Tasks
|
||||
public static class UserTasks
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs a login request using two-factor authentication and returns the CSRF token from the response.
|
||||
/// Performs a login request using configured credentials and optional two-factor authentication.
|
||||
/// </summary>
|
||||
/// <param name="httpClient">The HTTP client used to send the request.</param>
|
||||
/// <returns>The CSRF token when login succeeds; otherwise, <c>null</c>.</returns>
|
||||
public static async Task<string?> CallLoginEndpointAsync(HttpClient httpClient)
|
||||
{
|
||||
string? csrfToken = null;
|
||||
var credentials = SettingsProvider.Current.Credentials;
|
||||
|
||||
// prompt for 2FA code
|
||||
Console.Write("Enter 2FA code: ");
|
||||
var authCode = Console.ReadLine()?.Trim();
|
||||
|
||||
// validate input
|
||||
if (string.IsNullOrWhiteSpace(authCode))
|
||||
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
||||
{
|
||||
Console.WriteLine("A valid 2FA code is required.");
|
||||
Console.WriteLine("Email and password must be configured in appsettings.json.");
|
||||
return null;
|
||||
}
|
||||
|
||||
var payload = new
|
||||
string? authCode = null;
|
||||
if (credentials.TwoFactorEnabled)
|
||||
{
|
||||
email = "foodsharing@beging.de",
|
||||
password = "z+hc@Ox9Zu4~MXzkB:Z@O.-S1AvsT&mc!oyQA?NK1jckl1Dzi2^-)+.H.AJKBLoi",
|
||||
code = authCode,
|
||||
remember_me = true
|
||||
};
|
||||
Console.Write("Enter 2FA code: ");
|
||||
authCode = Console.ReadLine()?.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(authCode))
|
||||
{
|
||||
Console.WriteLine("A valid 2FA code is required when two-factor authentication is enabled.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
string? csrfToken = null;
|
||||
|
||||
object payload = credentials.TwoFactorEnabled
|
||||
? new
|
||||
{
|
||||
email = credentials.Email,
|
||||
password = credentials.Password,
|
||||
code = authCode,
|
||||
remember_me = true
|
||||
}
|
||||
: new
|
||||
{
|
||||
email = credentials.Email,
|
||||
password = credentials.Password,
|
||||
remember_me = true
|
||||
};
|
||||
|
||||
var response = await httpClient.PostAsJsonAsync(Endpoints.UserLogin, payload);
|
||||
|
||||
// handle unsuccessful response
|
||||
|
||||
Reference in New Issue
Block a user