Skip to content

Commit d565b0a

Browse files
Merge pull request #150 from DoWhile-UOM/dev
Dev
2 parents 115beb9 + 14ffe5a commit d565b0a

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

FirstStep/Services/EmailSevices/EmailService.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Azure;
22
using Azure.Communication.Email;
3+
using FirstStep.Models;
34
using FirstStep.Models.DTOs;
45
using FirstStep.Models.ServiceModels;
56
using FirstStep.Template;
@@ -222,7 +223,7 @@ public async Task SendEmailInterviewBookConfirm(string email, string advertismen
222223

223224
builder.HtmlBody = EmailTemplates.InterviewBookConfirm;
224225
request.To = email;
225-
request.Subject = "Schedule Your Interview";
226+
request.Subject = "Interview Confirmation";
226227
builder.HtmlBody = builder.HtmlBody.Replace("[Job Position]", advertismentTitle);
227228
builder.HtmlBody = builder.HtmlBody.Replace("{Company Name}", company_name);
228229
builder.HtmlBody = builder.HtmlBody.Replace("[Interview Date]", date);
@@ -232,6 +233,24 @@ public async Task SendEmailInterviewBookConfirm(string email, string advertismen
232233
await SendEmail(request);
233234
}
234235

236+
public async Task SendPasswordReset(string email, string token)
237+
{
238+
// Registration Email
239+
string baselink = "https://polite-forest-041105700.5.azurestaticapps.net";
240+
var bookingLink = $"{baselink}/ResetPassword?id={token}";
241+
242+
EmailModel request = new();
243+
var builder = new BodyBuilder();
244+
245+
builder.HtmlBody = EmailTemplates.SendPassWordResetLink;
246+
request.To = email;
247+
request.Subject = "Password Reset Request";
248+
builder.HtmlBody = builder.HtmlBody.Replace("{booking_link}", bookingLink);
249+
request.Body = builder.HtmlBody;
250+
251+
await SendEmail(request);
252+
}
253+
235254
public async Task<string> CARegIsSuccessfull(string email, string firstName, string lastName)
236255
{
237256
EmailModel emailBody = new();

FirstStep/Services/EmailSevices/IEmailService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public interface IEmailService
2222
Task SendEmailInterviewBook(string email, string advertismentTitle, string company_name, int userid, int advertismentid, string comment);
2323

2424
Task SendEmailInterviewBookConfirm(string email, string advertismentTitle, string company_name, string date, string time);
25+
26+
Task SendPasswordReset(string email, string token);
27+
2528
Task<string> CARegIsSuccessfull(string email, string firstName, string lastName);
2629
}
2730
}

FirstStep/Services/UserServices/UserService.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class UserService: IUserService
1919
private readonly IMapper _mapper;
2020
private readonly IEmailService _emailService;
2121

22-
private readonly Dictionary<string, int> _passwordResetTokens = new Dictionary<string, int>();
22+
private static readonly Dictionary<string, int> _passwordResetTokens = new Dictionary<string, int>();
2323
private static readonly Random random = new Random();
2424

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

8383

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

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

9899
public async Task<AuthenticationResult> ResetPassword(PasswordResetDto userObj)
99100
{
101+
100102
if (userObj.token == null)
101103
{
102104
throw new Exception("Token is null.");
103105
}
104106

105-
if (_passwordResetTokens.TryGetValue(userObj.token, out var userId))
107+
// check whether the token is valid
108+
if (_passwordResetTokens.ContainsKey(userObj.token))
106109
{
110+
int userId = _passwordResetTokens[userObj.token];
111+
107112
var user = await _context.Users.FirstOrDefaultAsync(x => x.user_id == userId);
108113

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

123128
await _context.SaveChangesAsync();
129+
_passwordResetTokens.Clear();
124130

125131
return new AuthenticationResult { IsSuccessful = true };
126132
}
127133
else
128134
{
135+
_passwordResetTokens.Clear();
129136
throw new Exception("Invalid Token.");
130137
}
131138
}

FirstStep/Template/EmailTemplates.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,28 @@ public static class EmailTemplates
181181
</div>
182182
</body>
183183
</html>";
184+
185+
public static string SendPassWordResetLink =
186+
@"<!DOCTYPE html>
187+
<html>
188+
<head>
189+
<meta charset=""utf-8"" />
190+
<title>Password Reset Request</title>
191+
</head>
192+
<body>
193+
<div style=""font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;"">
194+
<h2 style=""color: #333;"">Password Reset Request</h2>
195+
196+
197+
<div style=""background-color: #f4f4f4; padding: 10px; border-radius: 5px;"">
198+
<h3 style=""color: #333;"">Dear User,</h3>
199+
<p>We received a request to reset your password for your account. Please use the link below to reset your password.</p>
200+
<p>Please <a href={booking_link} style=""color: #007bff; text-decoration: none;"">click here</a> to reset your password.</p>
201+
<p>If you did not request a password reset, please ignore this email or contact support if you have any questions.</p>
202+
</div>
203+
<p style=""color: #333; margin-top: 20px;"">Thank you for choosing FirstStep.</p>
204+
</div>
205+
</body>
206+
</html>";
184207
}
185208
}

0 commit comments

Comments
 (0)