Added email helper method to send token for user account update.

This commit is contained in:
DESKTOP-GENO133\IvanPlex
2024-02-28 14:33:10 -07:00
parent a9d7ab0193
commit acc3d2f6d0
2 changed files with 50 additions and 15 deletions

View File

@@ -25,6 +25,7 @@ namespace CarCareTracker.Logic
bool CheckIfUserIsValid(int userId);
bool CreateRootUserCredentials(LoginModel credentials);
bool DeleteRootUserCredentials();
bool GenerateTokenForEmailAddress(string emailAddress, bool isPasswordReset);
List<UserData> GetAllUsers();
List<Token> GetAllTokens();
@@ -196,21 +197,7 @@ namespace CarCareTracker.Logic
if (existingUser.Id != default)
{
//user exists, generate a token and send email.
//check to see if there is an existing token sent to the user.
var existingToken = _tokenData.GetTokenRecordByEmailAddress(existingUser.EmailAddress);
if (existingToken.Id == default)
{
var token = new Token()
{
Body = NewToken(),
EmailAddress = existingUser.EmailAddress
};
var result = _tokenData.CreateNewToken(token);
if (result)
{
result = _mailHelper.NotifyUserForPasswordReset(existingUser.EmailAddress, token.Body).Success;
}
}
GenerateTokenForEmailAddress(existingUser.EmailAddress, true);
}
//for security purposes we want to always return true for this method.
//otherwise someone can spam the reset password method to sniff out users.
@@ -460,5 +447,30 @@ namespace CarCareTracker.Logic
{
return Guid.NewGuid().ToString().Substring(0, 8);
}
public bool GenerateTokenForEmailAddress(string emailAddress, bool isPasswordReset)
{
bool result = false;
//check if there is already a token tied to this email address.
var existingToken = _tokenData.GetTokenRecordByEmailAddress(emailAddress);
if (existingToken.Id == default)
{
//no token, generate one and send.
var token = new Token()
{
Body = NewToken(),
EmailAddress = emailAddress
};
result = _tokenData.CreateNewToken(token);
if (result)
{
result = isPasswordReset ? _mailHelper.NotifyUserForPasswordReset(emailAddress, token.Body).Success : _mailHelper.NotifyUserForAccountUpdate(emailAddress, token.Body).Success;
}
} else
{
//token exists, send it again.
result = isPasswordReset ? _mailHelper.NotifyUserForPasswordReset(emailAddress, existingToken.Body).Success : _mailHelper.NotifyUserForAccountUpdate(emailAddress, existingToken.Body).Success;
}
return result;
}
}
}