58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using OFBButte.Application.Codes;
|
|
using OFBButte.Application.Database;
|
|
using OFBButte.Application.Email;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OFBButte.Application.Users
|
|
{
|
|
public class SendEmailValidation
|
|
{
|
|
private readonly IOFBContext context;
|
|
private readonly IEmailSender emailSender;
|
|
private readonly ICodeGenerator codes;
|
|
public SendEmailValidation(IOFBContext context, IEmailSender emailSender, ICodeGenerator codes)
|
|
{
|
|
this.context = context;
|
|
this.emailSender = emailSender;
|
|
this.codes = codes;
|
|
}
|
|
|
|
public async Task Handle(string email)
|
|
{
|
|
var user = context.Users.Include(u => u.EmailVerificationCode).FirstOrDefault(u => u.Email == email);
|
|
if (user == null)
|
|
throw new Exception($"Could not find user with email: {email}");
|
|
|
|
if (user.EmailVerificationCode != null)
|
|
{
|
|
context.EmailVerificationCodes.Remove(user.EmailVerificationCode);
|
|
}
|
|
|
|
// Generate verification code
|
|
user.EmailVerificationCode = new Entities.EmailVerificationCode()
|
|
{
|
|
Code = codes.Generate(),
|
|
CreatedDate = DateTime.Now
|
|
};
|
|
|
|
context.SaveChanges();
|
|
|
|
// Create the email
|
|
var emailMessage = new VerifyEmailMessage()
|
|
{
|
|
To = email,
|
|
From = "donotreply@ofbbutte.com",
|
|
VerificationUrl = $"https://ofbbutte.com/user/emailverification?c={user.EmailVerificationCode.Id}&code={user.EmailVerificationCode.Code}"
|
|
};
|
|
|
|
await emailSender.Send(emailMessage);
|
|
}
|
|
}
|
|
}
|