-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
335 additions
and
206 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
HalceraAPI.Common/AppsettingsOptions/EmailSenderOptions.cs
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,11 @@ | ||
namespace HalceraAPI.Common.AppsettingsOptions | ||
{ | ||
public class EmailSenderOptions | ||
{ | ||
public string? SendGridKey { get; set; } | ||
|
||
public string? SendGridUser { get; set; } | ||
|
||
public string? SendGridEmail { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HalceraAPI.Common.Utilities | ||
{ | ||
public static class EmailConstants | ||
{ | ||
public const string ForgotPasswordSubject = "Request to change password"; | ||
|
||
public static string ForgotPasswordPlainTextMessage(string userPasswordResetToken) | ||
{ | ||
return $"Request to change password {userPasswordResetToken}"; | ||
} | ||
public const string ForgotPasswordHtmlMessage = "Request to change password"; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -40,7 +40,6 @@ public void Initialize() | |
|
||
private List<Roles> LoadApplicationRoles() | ||
{ | ||
// Create all roles required | ||
List<Roles> applicationRoles = new() | ||
{ | ||
new() { | ||
|
@@ -63,7 +62,7 @@ private void LoadAdminUser(Roles? roleId) | |
{ | ||
Name = "Admin", | ||
Email = "[email protected]", | ||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("MASTER123*"), | ||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("string"), | ||
Roles = roleId != null ? new List<Roles>() { roleId } : null | ||
}; | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HalceraAPI.Services.Contract | ||
{ | ||
public interface IEmailSenderOperation | ||
{ | ||
Task SendEmailAsync(string receiverEmail, string subject, string plainTextMessage, string htmlMessage); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
HalceraAPI.Services/Dtos/Identity/ResetUserPasswordRequest.cs
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,25 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HalceraAPI.Services.Dtos.Identity | ||
{ | ||
public class ResetUserPasswordRequest | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
public required string Email { get; set; } | ||
|
||
[Required] | ||
public required string OTP { get; set; } | ||
|
||
[Required] | ||
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)] | ||
[DataType(DataType.Password)] | ||
public required string Password { get; set; } | ||
|
||
[Required] | ||
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)] | ||
[DataType(DataType.Password)] | ||
[Compare(nameof(Password))] | ||
public required string ConfirmPassword { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
| ||
using HalceraAPI.Common.AppsettingsOptions; | ||
using HalceraAPI.Services.Contract; | ||
using Microsoft.Extensions.Options; | ||
using SendGrid; | ||
using SendGrid.Helpers.Mail; | ||
|
||
namespace HalceraAPI.Services.Operations | ||
{ | ||
public class EmailSenderOperation : IEmailSenderOperation | ||
{ | ||
private readonly EmailSenderOptions _emailOptions; | ||
|
||
public EmailSenderOperation(IOptions<EmailSenderOptions> options) | ||
{ | ||
_emailOptions = options.Value; | ||
} | ||
|
||
public async Task SendEmailAsync(string receiverEmail, string subject, string plainTextMessage, string htmlMessage) | ||
{ | ||
await Execute(receiverEmail, subject, plainTextMessage, htmlMessage); | ||
} | ||
|
||
private async Task Execute(string receiverEmail, string subject, string plainTextMessage, string htmlMessage) | ||
{ | ||
var client = new SendGridClient(_emailOptions.SendGridKey); | ||
var from = new EmailAddress(_emailOptions.SendGridEmail, _emailOptions.SendGridUser); | ||
var to = new EmailAddress(receiverEmail); | ||
|
||
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextMessage, htmlMessage); | ||
await client.SendEmailAsync(msg); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using HalceraAPI.Models; | ||
using HalceraAPI.Services.Contract; | ||
using HalceraAPI.Services.Dtos.ApplicationUser; | ||
using HalceraAPI.Services.Dtos.Identity; | ||
using HalceraAPI.Services.Dtos.RefreshToken; | ||
using HalceraAPI.Services.Dtos.Role; | ||
using HalceraAPI.Services.Token; | ||
|
@@ -20,17 +21,20 @@ public class IdentityOperation : IIdentityOperation | |
private readonly IMapper _mapper; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly JWTOptions jwtOptions; | ||
private readonly IEmailSenderOperation _emailSenderOperation; | ||
|
||
public IdentityOperation( | ||
IUnitOfWork unitOfWork, | ||
IMapper mapper, | ||
IHttpContextAccessor httpContextAccessor, | ||
IOptions<JWTOptions> options) | ||
IOptions<JWTOptions> options, | ||
IEmailSenderOperation emailSenderOperation) | ||
{ | ||
_unitOfWork = unitOfWork; | ||
_mapper = mapper; | ||
_httpContextAccessor = httpContextAccessor; | ||
jwtOptions = options.Value; | ||
_emailSenderOperation = emailSenderOperation; | ||
} | ||
|
||
public async Task<UserAuthResponse> Register(RegisterRequest registerRequest) | ||
|
@@ -153,5 +157,53 @@ private async Task SetUserRole(IEnumerable<RoleRequest>? rolesId, ApplicationUse | |
} | ||
} | ||
} | ||
|
||
public async Task ForgotPassword(string email) | ||
{ | ||
var user = await _unitOfWork.ApplicationUser | ||
.GetFirstOrDefault(user => user.Email.Trim().ToLower().Equals(email.Trim().ToLower())); | ||
|
||
if (user != null) | ||
{ | ||
user.PasswordResetToken = CreateRandomToken(); | ||
user.ResetTokenExpires = DateTime.UtcNow.AddHours(1); | ||
await _unitOfWork.SaveAsync(); | ||
|
||
await _emailSenderOperation.SendEmailAsync( | ||
"[email protected]", //user.Email, | ||
EmailConstants.ForgotPasswordSubject, | ||
EmailConstants.ForgotPasswordPlainTextMessage(user.PasswordResetToken), | ||
EmailConstants.ForgotPasswordHtmlMessage); | ||
} | ||
} | ||
|
||
public async Task ResetUserPassword(ResetUserPasswordRequest resetUserPasswordRequest) | ||
{ | ||
var user = await _unitOfWork.ApplicationUser | ||
.GetFirstOrDefault(user => user.Email.Trim().ToLower().Equals(resetUserPasswordRequest.Email.Trim().ToLower())); | ||
|
||
if (user != null && user.PasswordResetToken != null && DateTime.UtcNow < user.ResetTokenExpires) | ||
{ | ||
if (resetUserPasswordRequest.OTP.Trim().Equals(user.PasswordResetToken.Trim())) | ||
{ | ||
user.ResetPassword(resetUserPasswordRequest.Password); | ||
await _unitOfWork.SaveAsync(); | ||
|
||
return; | ||
} | ||
} | ||
|
||
throw new Exception("Invalid Token"); | ||
} | ||
|
||
public void Logout() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static string CreateRandomToken() | ||
{ | ||
return Guid.NewGuid().ToString()[..5]; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.