feat(backend): email registration

This commit is contained in:
sam 2024-09-10 02:39:07 +02:00
parent c77ee660ca
commit 13a0cac663
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
15 changed files with 120 additions and 82 deletions

View file

@ -16,7 +16,7 @@ public class AuthService(IClock clock, DatabaseContext db, ISnowflakeGenerator s
/// Creates a new user with the given email address and password.
/// This method does <i>not</i> save the resulting user, the caller must still call <see cref="M:Microsoft.EntityFrameworkCore.DbContext.SaveChanges" />.
/// </summary>
public async Task<User> CreateUserWithPasswordAsync(string username, string email, string password)
public async Task<User> CreateUserWithPasswordAsync(string username, string email, string password, CancellationToken ct = default)
{
var user = new User
{
@ -31,7 +31,7 @@ public class AuthService(IClock clock, DatabaseContext db, ISnowflakeGenerator s
};
db.Add(user);
user.Password = await Task.Run(() => _passwordHasher.HashPassword(user, password));
user.Password = await Task.Run(() => _passwordHasher.HashPassword(user, password), ct);
return user;
}

View file

@ -30,15 +30,14 @@ public class KeyCacheService(DatabaseContext db, IClock clock, ILogger logger)
var value = await db.TemporaryKeys.FirstOrDefaultAsync(k => k.Key == key, ct);
if (value == null) return null;
if (delete)
{
await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(ct);
await db.SaveChangesAsync(ct);
}
if (delete) await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(ct);
return value.Value;
}
public async Task DeleteKeyAsync(string key, CancellationToken ct = default) =>
await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(ct);
public async Task DeleteExpiredKeysAsync(CancellationToken ct)
{
var count = await db.TemporaryKeys.Where(k => k.Expires < clock.GetCurrentInstant()).ExecuteDeleteAsync(ct);
@ -54,7 +53,8 @@ public class KeyCacheService(DatabaseContext db, IClock clock, ILogger logger)
await SetKeyAsync(key, value, expires, ct);
}
public async Task<T?> GetKeyAsync<T>(string key, bool delete = false, CancellationToken ct = default) where T : class
public async Task<T?> GetKeyAsync<T>(string key, bool delete = false, CancellationToken ct = default)
where T : class
{
var value = await GetKeyAsync(key, delete, ct);
return value == null ? default : JsonConvert.DeserializeObject<T>(value);

View file

@ -10,15 +10,22 @@ public class MailService(ILogger logger, IMailer mailer, IQueue queue, Config co
public void QueueAccountCreationEmail(string to, string code)
{
_logger.Debug("Sending account creation email to {ToEmail}", to);
queue.QueueAsyncTask(async () =>
{
await mailer.SendAsync(new AccountCreationMailable(config, new AccountCreationMailableView
_logger.Debug("Sending account creation email to {ToEmail}", to);
try
{
To = to,
Code = code
}));
await mailer.SendAsync(new AccountCreationMailable(config, new AccountCreationMailableView
{
BaseUrl = config.BaseUrl,
To = to,
Code = code
}));
}
catch (Exception exc)
{
_logger.Error(exc, "Sending account creation email");
}
});
}
}