Skip to content

Commit

Permalink
Merge pull request #150 from DoWhile-UOM/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Isuranga-2001 authored Jul 10, 2024
2 parents 115beb9 + 14ffe5a commit d565b0a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
21 changes: 20 additions & 1 deletion FirstStep/Services/EmailSevices/EmailService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure;
using Azure.Communication.Email;
using FirstStep.Models;
using FirstStep.Models.DTOs;
using FirstStep.Models.ServiceModels;
using FirstStep.Template;
Expand Down Expand Up @@ -222,7 +223,7 @@ public async Task SendEmailInterviewBookConfirm(string email, string advertismen

builder.HtmlBody = EmailTemplates.InterviewBookConfirm;
request.To = email;
request.Subject = "Schedule Your Interview";
request.Subject = "Interview Confirmation";
builder.HtmlBody = builder.HtmlBody.Replace("[Job Position]", advertismentTitle);
builder.HtmlBody = builder.HtmlBody.Replace("{Company Name}", company_name);
builder.HtmlBody = builder.HtmlBody.Replace("[Interview Date]", date);
Expand All @@ -232,6 +233,24 @@ public async Task SendEmailInterviewBookConfirm(string email, string advertismen
await SendEmail(request);
}

public async Task SendPasswordReset(string email, string token)
{
// Registration Email
string baselink = "https://polite-forest-041105700.5.azurestaticapps.net";
var bookingLink = $"{baselink}/ResetPassword?id={token}";

EmailModel request = new();
var builder = new BodyBuilder();

builder.HtmlBody = EmailTemplates.SendPassWordResetLink;
request.To = email;
request.Subject = "Password Reset Request";
builder.HtmlBody = builder.HtmlBody.Replace("{booking_link}", bookingLink);
request.Body = builder.HtmlBody;

await SendEmail(request);
}

public async Task<string> CARegIsSuccessfull(string email, string firstName, string lastName)
{
EmailModel emailBody = new();
Expand Down
3 changes: 3 additions & 0 deletions FirstStep/Services/EmailSevices/IEmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface IEmailService
Task SendEmailInterviewBook(string email, string advertismentTitle, string company_name, int userid, int advertismentid, string comment);

Task SendEmailInterviewBookConfirm(string email, string advertismentTitle, string company_name, string date, string time);

Task SendPasswordReset(string email, string token);

Task<string> CARegIsSuccessfull(string email, string firstName, string lastName);
}
}
15 changes: 11 additions & 4 deletions FirstStep/Services/UserServices/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UserService: IUserService
private readonly IMapper _mapper;
private readonly IEmailService _emailService;

private readonly Dictionary<string, int> _passwordResetTokens = new Dictionary<string, int>();
private static readonly Dictionary<string, int> _passwordResetTokens = new Dictionary<string, int>();
private static readonly Random random = new Random();

public UserService(DataContext context, IMapper mapper, IEmailService emailService)
Expand Down Expand Up @@ -82,8 +82,9 @@ public async Task<AuthenticationResult> ResetPasswordRequest(string userEmail)


//Call Email service to send reset password email
var result = await _emailService.CARegIsSuccessfull(user.email, token, "test");
Console.WriteLine(token);
//var result = await _emailService.CARegIsSuccessfull(user.email, token, "test");
await _emailService.SendPasswordReset(user.email, token);
Console.WriteLine(token+" "+user.user_id);

return new AuthenticationResult { IsSuccessful = true};
}
Expand All @@ -97,13 +98,17 @@ private static string GenerateRandomString(int length)

public async Task<AuthenticationResult> ResetPassword(PasswordResetDto userObj)
{

if (userObj.token == null)
{
throw new Exception("Token is null.");
}

if (_passwordResetTokens.TryGetValue(userObj.token, out var userId))
// check whether the token is valid
if (_passwordResetTokens.ContainsKey(userObj.token))
{
int userId = _passwordResetTokens[userObj.token];

var user = await _context.Users.FirstOrDefaultAsync(x => x.user_id == userId);

if (user == null)
Expand All @@ -121,11 +126,13 @@ public async Task<AuthenticationResult> ResetPassword(PasswordResetDto userObj)
_passwordResetTokens.Remove(userObj.token);

await _context.SaveChangesAsync();
_passwordResetTokens.Clear();

return new AuthenticationResult { IsSuccessful = true };
}
else
{
_passwordResetTokens.Clear();
throw new Exception("Invalid Token.");
}
}
Expand Down
23 changes: 23 additions & 0 deletions FirstStep/Template/EmailTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,28 @@ public static class EmailTemplates
</div>
</body>
</html>";

public static string SendPassWordResetLink =
@"<!DOCTYPE html>
<html>
<head>
<meta charset=""utf-8"" />
<title>Password Reset Request</title>
</head>
<body>
<div style=""font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;"">
<h2 style=""color: #333;"">Password Reset Request</h2>
<div style=""background-color: #f4f4f4; padding: 10px; border-radius: 5px;"">
<h3 style=""color: #333;"">Dear User,</h3>
<p>We received a request to reset your password for your account. Please use the link below to reset your password.</p>
<p>Please <a href={booking_link} style=""color: #007bff; text-decoration: none;"">click here</a> to reset your password.</p>
<p>If you did not request a password reset, please ignore this email or contact support if you have any questions.</p>
</div>
<p style=""color: #333; margin-top: 20px;"">Thank you for choosing FirstStep.</p>
</div>
</body>
</html>";
}
}

0 comments on commit d565b0a

Please sign in to comment.