added ability to notify user that they have a registration token.
This commit is contained in:
@@ -24,11 +24,9 @@ namespace CarCareTracker.Controllers
|
||||
};
|
||||
return View(viewModel);
|
||||
}
|
||||
public IActionResult GenerateNewToken(string emailAddress)
|
||||
public IActionResult GenerateNewToken(string emailAddress, bool autoNotify)
|
||||
{
|
||||
var result = _loginLogic.GenerateUserToken(emailAddress);
|
||||
//send an email test block.
|
||||
SendEmail(emailAddress);
|
||||
var result = _loginLogic.GenerateUserToken(emailAddress, autoNotify);
|
||||
return Json(result);
|
||||
}
|
||||
public IActionResult DeleteToken(int tokenId)
|
||||
@@ -36,31 +34,5 @@ namespace CarCareTracker.Controllers
|
||||
var result = _loginLogic.DeleteUserToken(tokenId);
|
||||
return Json(result);
|
||||
}
|
||||
private bool SendEmail(string emailAddress)
|
||||
{
|
||||
var mailConfig = new MailConfig();
|
||||
string to = emailAddress;
|
||||
string from = mailConfig.EmailFrom;
|
||||
var server = mailConfig.EmailServer;
|
||||
MailMessage message = new MailMessage(from, to);
|
||||
message.Subject = "Using the new SMTP client.";
|
||||
message.Body = @"Using this new feature, you can send an email message from an application very easily.";
|
||||
SmtpClient client = new SmtpClient(server);
|
||||
client.EnableSsl = mailConfig.UseSSL;
|
||||
client.Port = mailConfig.Port;
|
||||
client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password);
|
||||
|
||||
try
|
||||
{
|
||||
client.Send(message);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
|
||||
ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
77
Helper/MailHelper.cs
Normal file
77
Helper/MailHelper.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using CarCareTracker.Models;
|
||||
using System.Net.Mail;
|
||||
using System.Net;
|
||||
|
||||
namespace CarCareTracker.Helper
|
||||
{
|
||||
public interface IMailHelper
|
||||
{
|
||||
OperationResponse NotifyUserForRegistration(string emailAddress, string token);
|
||||
OperationResponse NotifyUserForPasswordReset(string emailAddress, string token);
|
||||
}
|
||||
public class MailHelper : IMailHelper
|
||||
{
|
||||
private readonly MailConfig mailConfig;
|
||||
public MailHelper(
|
||||
IConfiguration config
|
||||
) {
|
||||
//load mailConfig from Configuration
|
||||
mailConfig = config.GetSection("MailConfig").Get<MailConfig>();
|
||||
}
|
||||
public OperationResponse NotifyUserForRegistration(string emailAddress, string token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) {
|
||||
return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" };
|
||||
}
|
||||
string emailSubject = "Your Registration Token for LubeLogger";
|
||||
string emailBody = $"A token has been generated on your behalf, please complete your registration for LubeLogger using the token: {token}";
|
||||
var result = SendEmail(emailAddress, emailSubject, emailBody);
|
||||
if (result)
|
||||
{
|
||||
return new OperationResponse { Success = true, Message = "Email Sent!" };
|
||||
} else
|
||||
{
|
||||
return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage };
|
||||
}
|
||||
}
|
||||
public OperationResponse NotifyUserForPasswordReset(string emailAddress, string token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" };
|
||||
}
|
||||
string emailSubject = "Your Password Reset Token for LubeLogger";
|
||||
string emailBody = $"A token has been generated on your behalf, please reset your password for LubeLogger using the token: {token}";
|
||||
var result = SendEmail(emailAddress, emailSubject, emailBody);
|
||||
if (result)
|
||||
{
|
||||
return new OperationResponse { Success = true, Message = "Email Sent!" };
|
||||
}
|
||||
else
|
||||
{
|
||||
return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage };
|
||||
}
|
||||
}
|
||||
private bool SendEmail(string emailTo, string emailSubject, string emailBody) {
|
||||
string to = emailTo;
|
||||
string from = mailConfig.EmailFrom;
|
||||
var server = mailConfig.EmailServer;
|
||||
MailMessage message = new MailMessage(from, to);
|
||||
message.Subject = emailSubject;
|
||||
message.Body = emailBody;
|
||||
SmtpClient client = new SmtpClient(server);
|
||||
client.EnableSsl = mailConfig.UseSSL;
|
||||
client.Port = mailConfig.Port;
|
||||
client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password);
|
||||
try
|
||||
{
|
||||
client.Send(message);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
{
|
||||
public static string DbName = "data/cartracker.db";
|
||||
public static string UserConfigPath = "config/userConfig.json";
|
||||
public static string GenericErrorMessage = "An error occurred, please try again later";
|
||||
|
||||
public static string TruncateStrings(string input, int maxLength = 25)
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace CarCareTracker.Logic
|
||||
{
|
||||
public interface ILoginLogic
|
||||
{
|
||||
bool GenerateUserToken(string emailAddress);
|
||||
OperationResponse GenerateUserToken(string emailAddress, bool autoNotify);
|
||||
bool DeleteUserToken(int tokenId);
|
||||
OperationResponse RegisterNewUser(LoginModel credentials);
|
||||
OperationResponse ResetUserPassword(LoginModel credentials);
|
||||
@@ -25,10 +25,12 @@ namespace CarCareTracker.Logic
|
||||
{
|
||||
private readonly IUserRecordDataAccess _userData;
|
||||
private readonly ITokenRecordDataAccess _tokenData;
|
||||
public LoginLogic(IUserRecordDataAccess userData, ITokenRecordDataAccess tokenData)
|
||||
private readonly IMailHelper _mailHelper;
|
||||
public LoginLogic(IUserRecordDataAccess userData, ITokenRecordDataAccess tokenData, IMailHelper mailHelper)
|
||||
{
|
||||
_userData = userData;
|
||||
_tokenData = tokenData;
|
||||
_mailHelper = mailHelper;
|
||||
}
|
||||
public OperationResponse RegisterNewUser(LoginModel credentials)
|
||||
{
|
||||
@@ -113,13 +115,13 @@ namespace CarCareTracker.Logic
|
||||
var result = _tokenData.GetTokens();
|
||||
return result;
|
||||
}
|
||||
public bool GenerateUserToken(string emailAddress)
|
||||
public OperationResponse GenerateUserToken(string emailAddress, bool autoNotify)
|
||||
{
|
||||
//check if email address already has a token attached to it.
|
||||
var existingToken = _tokenData.GetTokenRecordByEmailAddress(emailAddress);
|
||||
if (existingToken.Id != default)
|
||||
{
|
||||
return false;
|
||||
return new OperationResponse { Success = false, Message = "There is an existing token tied to this email address" };
|
||||
}
|
||||
var token = new Token()
|
||||
{
|
||||
@@ -127,7 +129,21 @@ namespace CarCareTracker.Logic
|
||||
EmailAddress = emailAddress
|
||||
};
|
||||
var result = _tokenData.CreateNewToken(token);
|
||||
return result;
|
||||
if (result && autoNotify)
|
||||
{
|
||||
result = _mailHelper.NotifyUserForRegistration(emailAddress, token.Body).Success;
|
||||
if (!result)
|
||||
{
|
||||
return new OperationResponse { Success = false, Message = "Token Generated, but Email failed to send, please check your SMTP settings." };
|
||||
}
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
return new OperationResponse { Success = true, Message = "Token Generated!" };
|
||||
} else
|
||||
{
|
||||
return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage };
|
||||
}
|
||||
}
|
||||
public bool DeleteUserToken(int tokenId)
|
||||
{
|
||||
|
||||
@@ -26,6 +26,7 @@ builder.Services.AddSingleton<IFileHelper, FileHelper>();
|
||||
builder.Services.AddSingleton<IGasHelper, GasHelper>();
|
||||
builder.Services.AddSingleton<IReminderHelper, ReminderHelper>();
|
||||
builder.Services.AddSingleton<IReportHelper, ReportHelper>();
|
||||
builder.Services.AddSingleton<IMailHelper, MailHelper>();
|
||||
|
||||
//configur logic
|
||||
builder.Services.AddSingleton<ILoginLogic, LoginLogic>();
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<div class="row">
|
||||
<div class="col-5">
|
||||
<div class="row">
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input" type="checkbox" role="switch" id="enableAutoNotify" checked>
|
||||
<label class="form-check-label" for="enableAutoNotify">Auto Notify(via Email)</label>
|
||||
</div>
|
||||
<button onclick="generateNewToken()" class="btn btn-primary btn-md mt-1 mb-1"><i class="bi bi-pencil-square me-2"></i>Generate User Token</button>
|
||||
</div>
|
||||
<table class="table table-hover">
|
||||
@@ -88,11 +92,12 @@
|
||||
},
|
||||
}).then(function (result) {
|
||||
if (result.isConfirmed) {
|
||||
$.get('/Admin/GenerateNewToken', {emailAddress: result.value.emailAddress}, function (data) {
|
||||
if (data) {
|
||||
var autoNotify = $("#enableAutoNotify").is(":checked");
|
||||
$.get('/Admin/GenerateNewToken', {emailAddress: result.value.emailAddress, autoNotify: autoNotify}, function (data) {
|
||||
if (data.success) {
|
||||
reloadPage();
|
||||
} else {
|
||||
errorToast("An error occurred, make sure the email address doesn't already have a token generated for it.")
|
||||
errorToast(data.message)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user