Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring
Browse files Browse the repository at this point in the history
hishamco committed May 18, 2024
1 parent dfeb493 commit 903a901
Showing 1 changed file with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -99,9 +99,8 @@ public async Task<IActionResult> RequestCode()
var settings = (await SiteService.GetSiteSettingsAsync()).As<EmailAuthenticatorLoginSettings>();

var to = await UserManager.GetEmailAsync(user);
var subject = await GetSubjectAsync(settings, user, code);
var body = await GetBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subject, body);
var subjectAndBody = await GetSubjectAndBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subjectAndBody.Subject, subjectAndBody.Body);

if (!result.Succeeded)
{
@@ -167,9 +166,8 @@ public async Task<IActionResult> SendCode()
var code = await UserManager.GenerateTwoFactorTokenAsync(user, TokenOptions.DefaultEmailProvider);

var to = await UserManager.GetEmailAsync(user);
var subject = await GetSubjectAsync(settings, user, code);
var body = await GetBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subject, body);
var subjectAndBody = await GetSubjectAndBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subjectAndBody.Subject, subjectAndBody.Body);

return Ok(new
{
@@ -179,36 +177,31 @@ public async Task<IActionResult> SendCode()
});
}

private Task<string> GetSubjectAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
private async Task<(string Subject, string Body)> GetSubjectAndBodyAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
{
var message = string.IsNullOrWhiteSpace(settings.Subject)
? EmailAuthenticatorLoginSettings.DefaultSubject
: settings.Subject;
var subject = string.IsNullOrWhiteSpace(settings.Subject)
? EmailAuthenticatorLoginSettings.DefaultSubject
: settings.Subject;

return GetContentAsync(message, user, code);
}
var body = string.IsNullOrWhiteSpace(settings.Body)
? EmailAuthenticatorLoginSettings.DefaultBody
: settings.Body;

private Task<string> GetBodyAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
{
var message = string.IsNullOrWhiteSpace(settings.Body)
? EmailAuthenticatorLoginSettings.DefaultBody
: settings.Body;
return (await GetContentAsync(subject), await GetContentAsync(body));

return GetContentAsync(message, user, code);
}

private async Task<string> GetContentAsync(string message, IUser user, string code)
{
var result = await _liquidTemplateManager.RenderHtmlContentAsync(message, _htmlEncoder, null,
new Dictionary<string, FluidValue>()
{
["User"] = new ObjectValue(user),
["Code"] = new StringValue(code),
});
async Task<string> GetContentAsync(string message)
{
var result = await _liquidTemplateManager.RenderHtmlContentAsync(message, _htmlEncoder, null,
new Dictionary<string, FluidValue>()
{
["User"] = new ObjectValue(user),
["Code"] = new StringValue(code),
});

using var writer = new StringWriter();
result.WriteTo(writer, _htmlEncoder);
using var writer = new StringWriter();
result.WriteTo(writer, _htmlEncoder);

return writer.ToString();
return writer.ToString();
}
}
}

0 comments on commit 903a901

Please sign in to comment.