fix: normalize Resend API key configuration
This commit is contained in:
@@ -20,18 +20,24 @@ public class ResendEmailSender : IEmailSender
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
_options = options.Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_options.ApiKey))
|
||||
string apiKey = NormalizeApiKey(_options.ApiKey);
|
||||
string fromEmail = _options.FromEmail?.Trim() ?? string.Empty;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(apiKey))
|
||||
{
|
||||
throw new InvalidOperationException("Emailer:ApiKey is required when using Resend email delivery.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_options.FromEmail))
|
||||
if (string.IsNullOrWhiteSpace(fromEmail))
|
||||
{
|
||||
throw new InvalidOperationException("Emailer:FromEmail is required when using Resend email delivery.");
|
||||
}
|
||||
|
||||
_options.ApiKey = apiKey;
|
||||
_options.FromEmail = fromEmail;
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", _options.ApiKey);
|
||||
new AuthenticationHeaderValue("Bearer", apiKey);
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
@@ -53,4 +59,16 @@ public class ResendEmailSender : IEmailSender
|
||||
$"Resend email failed: {response.StatusCode} - {body}");
|
||||
}
|
||||
}
|
||||
|
||||
private static string NormalizeApiKey(string? apiKey)
|
||||
{
|
||||
string normalized = apiKey?.Trim().Trim('"', '\'') ?? string.Empty;
|
||||
const string bearerPrefix = "Bearer ";
|
||||
if (normalized.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
normalized = normalized[bearerPrefix.Length..].Trim();
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user