-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement email sending with sengrid #20
feat: customize email sending of identity #20
- Loading branch information
1 parent
2984aad
commit 554cd82
Showing
26 changed files
with
706 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Logging; | ||
using Place.Notification; | ||
using Place.Notification.Email; | ||
using Place.Notification.Email.SMTP; | ||
|
||
namespace Core.Identity; | ||
|
||
/// <inheritdoc/> | ||
public class IdentityEmailSender<TUser> : IEmailSender<TUser> | ||
where TUser : class | ||
{ | ||
private readonly ILogger<IdentityEmailSender<TUser>> _logger; | ||
private readonly IEmailService _emailService; | ||
|
||
/// <exception cref="ArgumentNullException"></exception> | ||
public IdentityEmailSender( | ||
ILogger<IdentityEmailSender<TUser>> logger, | ||
IEmailService emailService | ||
) | ||
{ | ||
_logger = logger; | ||
_emailService = emailService; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task SendConfirmationLinkAsync(TUser user, string email, string confirmationLink) | ||
{ | ||
string subject = "Confirmez votre adresse email"; | ||
string htmlContent = GetConfirmationEmailTemplate(confirmationLink); | ||
|
||
await SendEmailAsync(email, subject, htmlContent); | ||
} | ||
|
||
public async Task SendPasswordResetLinkAsync(TUser user, string email, string resetLink) | ||
{ | ||
string subject = "Réinitialisation de votre mot de passe"; | ||
string htmlContent = GetPasswordResetTemplate(resetLink); | ||
|
||
await SendEmailAsync(email, subject, htmlContent); | ||
} | ||
|
||
public async Task SendPasswordResetCodeAsync(TUser user, string email, string resetCode) | ||
{ | ||
string subject = "Votre code de réinitialisation de mot de passe"; | ||
string htmlContent = GetPasswordResetTemplate(resetCode); | ||
|
||
await SendEmailAsync(email, subject, htmlContent); | ||
} | ||
|
||
protected virtual async Task SendEmailAsync(string to, string subject, string htmlContent) | ||
{ | ||
EmailMessage email = new(to, subject, htmlContent); | ||
|
||
try | ||
{ | ||
await _emailService.SendAsync(email); | ||
|
||
_logger.LogInformation("Email sent successfully to {Email}", email.To); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error sending email to {Email}", email.To); | ||
throw; | ||
} | ||
} | ||
|
||
private string GetConfirmationEmailTemplate(string confirmationLink) | ||
{ | ||
return $@" | ||
<html> | ||
<body> | ||
<h2>Confirmez votre adresse email</h2> | ||
<p>Pour confirmer votre compte, veuillez cliquer sur le lien ci-dessous :</p> | ||
<p><a href='{confirmationLink}'>Confirmer mon compte</a></p> | ||
<p>Si le lien ne fonctionne pas, copiez et collez cette URL dans votre navigateur :</p> | ||
<p>{confirmationLink}</p> | ||
</body> | ||
</html>"; | ||
} | ||
|
||
private string GetPasswordResetTemplate(string resetLink) | ||
{ | ||
return $@" | ||
<html> | ||
<body> | ||
<h2>Réinitialisation de votre mot de passe</h2> | ||
<p>Pour réinitialiser votre mot de passe, cliquez sur le lien ci-dessous :</p> | ||
<p><a href='{resetLink}'>Réinitialiser mon mot de passe</a></p> | ||
<p>Si le lien ne fonctionne pas, copiez et collez cette URL dans votre navigateur :</p> | ||
<p>{resetLink}</p> | ||
</body> | ||
</html>"; | ||
} | ||
|
||
private string GetPasswordResetCodeTemplate(string resetCode) | ||
{ | ||
return $@" | ||
<html> | ||
<body> | ||
<h2>Code de réinitialisation de mot de passe</h2> | ||
<p>Voici votre code de réinitialisation de mot de passe :</p> | ||
<h3>{resetCode}</h3> | ||
<p>Ce code est valable pendant une durée limitée.</p> | ||
</body> | ||
</html>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,80 @@ | |
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"app": { | ||
"name": "Place Profile Api" | ||
}, | ||
"Serilog": { | ||
"applicationName": "identity-service", | ||
"excludePaths": ["/ping", "/metrics"], | ||
"level": "information", | ||
"console": { | ||
"enabled": true | ||
}, | ||
"file": { | ||
"enabled": true, | ||
"path": "logs/logs.txt", | ||
"interval": "day" | ||
}, | ||
"seq": { | ||
"enabled": true, | ||
"url": "http://localhost:5341", | ||
"token": "secret" | ||
} | ||
}, | ||
"Swagger": { | ||
"Title": "Place Profile Api", | ||
"Description": "Place Profile Api documentation", | ||
"Version": "v1", | ||
"EnableBearerAuth": true, | ||
"SecuritySchemaName": "Bearer", | ||
"SecurityScheme": "JWT", | ||
"SecurityDescription": "Utiliser le format: Bearer {votre_token}", | ||
"EnableVersioning": true, | ||
"RoutePrefix": "swagger" | ||
}, | ||
"ApiVersioning": { | ||
"DefaultApiVersionMajor": 1, | ||
"DefaultApiVersionMinor": 0, | ||
"AssumeDefaultVersionWhenUnspecified": true, | ||
"ReportApiVersions": true, | ||
"ApiVersionReaderType": "Combine", | ||
"ReaderOptions": { | ||
"HeaderName": "x-api-version", | ||
"QueryStringParam": "api-version", | ||
"MediaTypeParam": "v" | ||
}, | ||
"GroupNameFormat": "'v'VVV", | ||
"SubstituteApiVersionInUrl": true, | ||
"DeprecatedVersionOptions": { | ||
"DeprecationMessage": "Cette version de l'API est obsolète. Veuillez migrer vers la version la plus récente.", | ||
"SunsetDate": "2025-12-31", | ||
"DocumentationUrl": "https://api.monsite.com/deprecation-policy" | ||
}, | ||
"ApiExplorerOptions": { | ||
"GroupNameFormat": "'v'VVV", | ||
"SubstituteApiVersionInUrl": true, | ||
"UrlFormat": "v{version:apiVersion}", | ||
"AddApiVersionParametersWhenVersionNeutral": true | ||
} | ||
}, | ||
"ConnectionStrings": { | ||
"PlaceDb": "Server=localhost;Port=5499;Database=place_db;User Id=postgres;Password=postgres;Include Error Detail=true" | ||
}, | ||
"Email": { | ||
"Provider": "SendGrid", | ||
"From": "[email protected]", | ||
"FromName": "Support Place", | ||
"Smtp": { | ||
"Host": "smtp.sendgrid.net", | ||
"Port": 587, | ||
"Username": "[email protected]", | ||
"Password": "Pass0rd123!" | ||
}, | ||
"SendGrid": { | ||
"ApiKey": null | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.