mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(email): add email verification html templates
This commit is contained in:
@@ -230,11 +230,10 @@ namespace SharpbinV3.Server.Services
|
||||
string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength);
|
||||
string tokenHash = Utilities.ComputeSha256(token);
|
||||
|
||||
await _email.SendAsync(
|
||||
user.Email,
|
||||
"Verify Your Email",
|
||||
$"{(_appSettings.Https ? "https" : "http")}://{_appSettings.Domain}/verify-email?token={token}"
|
||||
);
|
||||
string verificationUrl = $"{(_appSettings.Https ? "https" : "http")}://{_appSettings.Domain}/verify-email?token={token}";
|
||||
string emailBody = EmailTemplates.VerifyEmail(verificationUrl, user.Username);
|
||||
|
||||
await _email.SendAsync(user.Email, "Verify Your Email - Sharpbin", emailBody);
|
||||
var verificationToken = new EmailVerificationToken
|
||||
{
|
||||
TokenHash = tokenHash,
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public static class EmailTemplates
|
||||
{
|
||||
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
||||
private static readonly string BaseTemplateName = "SharpbinV3.Server.EmailTemplates.Base.html";
|
||||
|
||||
private static string LoadEmbeddedResource(string resourceName)
|
||||
{
|
||||
using var stream = Assembly.GetManifestResourceStream(resourceName);
|
||||
if (stream == null)
|
||||
throw new FileNotFoundException($"Embedded resource not found: {resourceName}");
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
private static string RenderTemplate(string title, string contentTemplate, Dictionary<string, string> replacements)
|
||||
{
|
||||
var baseTemplate = LoadEmbeddedResource(BaseTemplateName);
|
||||
var content = contentTemplate;
|
||||
|
||||
foreach (var (key, value) in replacements)
|
||||
{
|
||||
content = content.Replace($"{{{{{key}}}}}", value);
|
||||
}
|
||||
|
||||
return baseTemplate.Replace("{{TITLE}}", title).Replace("{{CONTENT}}", content);
|
||||
}
|
||||
|
||||
public static string VerifyEmail(string verificationUrl, string username)
|
||||
{
|
||||
var contentTemplate = LoadEmbeddedResource("SharpbinV3.Server.EmailTemplates.VerifyEmail.html");
|
||||
var replacements = new Dictionary<string, string> { { "USERNAME", username }, { "VERIFICATION_URL", verificationUrl } };
|
||||
|
||||
return RenderTemplate("Verify Your Email", contentTemplate, replacements);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user