feat(backend): add add email address endpoint

This commit is contained in:
sam 2024-10-02 00:52:49 +02:00
parent 7f971e8549
commit 5b17c716cb
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
6 changed files with 114 additions and 3 deletions

View file

@ -129,6 +129,30 @@ public class AuthService(IClock clock, DatabaseContext db, ISnowflakeGenerator s
return (user, EmailAuthenticationResult.AuthSuccessful);
}
/// <summary>
/// Validates a user's password outside an authentication context, for when a password is required for changing
/// a setting, such as adding a new email address or changing passwords.
/// </summary>
public async Task<bool> ValidatePasswordAsync(
User user,
string password,
CancellationToken ct = default
)
{
if (user.Password == null)
{
throw new FoxnounsError("Password for user supplied to ValidatePasswordAsync was null");
}
var pwResult = await Task.Run(
() => _passwordHasher.VerifyHashedPassword(user, user.Password!, password),
ct
);
return pwResult
is PasswordVerificationResult.SuccessRehashNeeded
or PasswordVerificationResult.Success;
}
public enum EmailAuthenticationResult
{
AuthSuccessful,

View file

@ -33,4 +33,31 @@ public class MailService(ILogger logger, IMailer mailer, IQueue queue, Config co
}
});
}
public void QueueAddEmailAddressEmail(string to, string code, string username)
{
_logger.Debug("Sending add email address email to {ToEmail}", to);
queue.QueueAsyncTask(async () =>
{
try
{
await mailer.SendAsync(
new AddEmailMailable(
config,
new AddEmailMailableView
{
BaseUrl = config.BaseUrl,
To = to,
Code = code,
Username = username,
}
)
);
}
catch (Exception exc)
{
_logger.Error(exc, "Sending add email address email");
}
});
}
}