Skip to content

Commit

Permalink
ResetPassword Implementation Started
Browse files Browse the repository at this point in the history
  • Loading branch information
polatefekaya committed Jun 6, 2024
1 parent ba2cc87 commit 331baef
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using RosanicSocial.Domain.Data.Identity;
using RosanicSocial.Domain.DTO.Request.Account;
using RosanicSocial.Domain.DTO.Request.Authentication;
using RosanicSocial.Domain.DTO.Request.Authentication.Password;
using RosanicSocial.Domain.DTO.Request.Verification.Email;
using RosanicSocial.Domain.DTO.Response.Authentication;
using RosanicSocial.Domain.DTO.Response.Authentication.Response;
using RosanicSocial.Domain.DTO.Response.Email;
using RosanicSocial.Domain.DTO.Response.Verification.Email;
using System;
Expand All @@ -21,5 +23,8 @@ public interface IAccountDbService {
Task<TwoFactorVerificationResponse?> VerifyTwoFactorToken(TwoFactorVerificationRequest request);
Task<EmailSendResponse?> SendConfirmationEmail();
Task<EmailConfirmResponse?> ConfirmEmail(EmailConfirmRequest request);
Task<EmailSendResponse?> ForgotPassword(ForgotPasswordRequest request);
Task<ResetForgottenPasswordResponse?> ResetForgottenPassword(ResetForgottenPasswordRequest request);
Task<ChangePasswordResponse?> ChangePassword(ChangePasswordRequest request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IEmailSenderService {
Task<EmailSendResponse?> SendEmail(EmailSendRequest request);
Task<EmailSendResponse?> SendVerificationEmail(EmailSendVerificationRequest request);
Task<EmailSendResponse?> SendTwoFactorEmail(EmailSendTwoFactorRequest request);
Task<EmailSendResponse?> SendResetPasswordEmail(EmailSendResetPasswordRequest request);
}
}
45 changes: 45 additions & 0 deletions RosanicSocial.Application/Services/DbServices/AccountDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
using RosanicSocial.Domain.Data.Identity;
using RosanicSocial.Domain.DTO.Request.Account;
using RosanicSocial.Domain.DTO.Request.Authentication;
using RosanicSocial.Domain.DTO.Request.Authentication.Password;
using RosanicSocial.Domain.DTO.Request.Email;
using RosanicSocial.Domain.DTO.Request.Info.Base;
using RosanicSocial.Domain.DTO.Request.Info.Detailed;
using RosanicSocial.Domain.DTO.Request.Verification.Email;
using RosanicSocial.Domain.DTO.Response.Authentication;
using RosanicSocial.Domain.DTO.Response.Authentication.Response;
using RosanicSocial.Domain.DTO.Response.Email;
using RosanicSocial.Domain.DTO.Response.Info.Base;
using RosanicSocial.Domain.DTO.Response.Info.Detailed;
Expand Down Expand Up @@ -306,5 +308,48 @@ private string FirstCharToUpperStringCreate(string? input) {
_logger.LogInformation($"{nameof(ManageTwoFactorLogIn)} in {nameof(AccountDbService)} is finished.");
return response;
}

public async Task<EmailSendResponse?> ForgotPassword(ForgotPasswordRequest request) {
_logger.LogDebug($"{nameof(ForgotPassword)} in {nameof(AccountDbService)} is started.");

//we have to send an email with a token
ApplicationUser? user = await _userManager.FindByNameAsync(request.UserName);
if (user is null) {
_logger.LogError($"There is no user with UserName:{request.UserName}");
return null;
}

string token = await _userManager.GeneratePasswordResetTokenAsync(user);
ActionContext actionContext = _actionContextAccessor.ActionContext;
IUrlHelper _urlHelper = _urlHelperFactory.GetUrlHelper(actionContext);

string? confirmationLink = UrlHelperExtensions.Action(_urlHelper, "ResetForgottenPassword", "Account", new { UserName = user.UserName, Token = token }, protocol: _context.HttpContext?.Request.Scheme);
if (confirmationLink is null) {
_logger.LogError("Confirmation link is null.");
return null;
}

EmailSendResetPasswordRequest emailSendResetPasswordRequest = new EmailSendResetPasswordRequest {
From = _configuration["EmailOptions:TwoFactorAuthSender"],
To = user.Email,
ConfirmationLink = confirmationLink
};

EmailSendResponse? response = await _emailSenderService.SendResetPasswordEmail(emailSendResetPasswordRequest);

_logger.LogInformation($"{nameof(ForgotPassword)} in {nameof(AccountDbService)} is finished.");
}

public Task<ResetForgottenPasswordResponse?> ResetForgottenPassword(ResetForgottenPasswordRequest request) {
_logger.LogDebug($"{nameof(ResetForgottenPassword)} in {nameof(AccountDbService)} is started.");
_logger.LogInformation($"{nameof(ResetForgottenPassword)} in {nameof(AccountDbService)} is finished.");
return null;
}

public Task<ChangePasswordResponse?> ChangePassword(ChangePasswordRequest request) {
_logger.LogDebug($"{nameof(ChangePassword)} in {nameof(AccountDbService)} is started.");
_logger.LogInformation($"{nameof(ChangePassword)} in {nameof(AccountDbService)} is finished.");
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace RosanicSocial.Domain.DTO.Request.Authentication.Password {
public class ChangePasswordRequest {
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current Password")]
public string OldPassword { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "New Password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm New Password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace RosanicSocial.Domain.DTO.Request.Authentication.Password {
public class ForgotPasswordRequest {
[Required]
public string UserName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace RosanicSocial.Domain.DTO.Request.Authentication.Password {
public class ResetForgottenPasswordRequest {
[EmailAddress]
public string Email { get; set; }

[DataType(DataType.Password)]
public string Password { get; set; }

[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Password and ConfirmPassword must be the same.")]
public string ConfirmPassword { get; set; }
public string Token { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace RosanicSocial.Domain.DTO.Request.Email {
public class EmailSendResetPasswordRequest {
public string From { get; set; }
public string To { get; set; }
public string ConfirmationLink { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;
using System.Collections.Generic;

namespace RosanicSocial.Domain.DTO.Response.Authentication.Response {
public class ChangePasswordResponse {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;
using System.Collections.Generic;

namespace RosanicSocial.Domain.DTO.Response.Authentication.Response {
public class ResetForgottenPasswordResponse {
}
}

0 comments on commit 331baef

Please sign in to comment.