Add in verify company email
This commit is contained in:
@@ -2,13 +2,18 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using BoredCareers.Services.DatabaseService;
|
||||
using BoredCareers.Entities;
|
||||
using System.Web.Http;
|
||||
using BoredCareers.Services;
|
||||
|
||||
namespace BoredCareers.Controllers {
|
||||
[ApiController]
|
||||
[Route("api/company")]
|
||||
public class CompanyController : MistoxControllerBase {
|
||||
|
||||
public CompanyController(DatabaseService db) : base(db) {}
|
||||
EmailService _emailContext;
|
||||
|
||||
public CompanyController(DatabaseService db, EmailService emailContext) : base(db) {
|
||||
_emailContext = emailContext;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetCompany(int CompanyID) {
|
||||
@@ -60,6 +65,57 @@ namespace BoredCareers.Controllers {
|
||||
return NotFound("Not logged in");
|
||||
}
|
||||
|
||||
}
|
||||
[HttpGet("sendverifyemail")]
|
||||
public async Task<ActionResult<string>> SendVerify([FromQuery] int CompanyID) {
|
||||
try {
|
||||
string key = "v" + CompanyID;
|
||||
// Stop from sending multiple emails quickly
|
||||
if (_emailContext._SentEmails.ContainsKey(key)) {
|
||||
DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key);
|
||||
if (PreviousSentTime.AddMinutes(5) > DateTime.Now) {
|
||||
return NotFound("Cannot sent another verify email until 5 minutes has elapsed");
|
||||
} else {
|
||||
_emailContext._SentEmails.Remove(key);
|
||||
}
|
||||
}
|
||||
Company? test = await _databaseService.GetCompany(CompanyID);
|
||||
if (test != null) {
|
||||
test.EmailToken = Guid.NewGuid().ToString();
|
||||
await _databaseService.SetCompany(test);
|
||||
|
||||
string EmailContents = EmailService.CompanyVerifyEmailSubject;
|
||||
EmailContents = Substitue(EmailContents, "@CompanyName", test.Name);
|
||||
EmailContents = Substitue(EmailContents, "@ID", CompanyID.ToString());
|
||||
EmailContents = Substitue(EmailContents, "@VerifyPassword", test.EmailToken);
|
||||
|
||||
string result = _emailContext.Send(test.Email, EmailService.CompanyVerifyEmailSubject, EmailContents);
|
||||
_emailContext._SentEmails.Add(key, DateTime.Now);
|
||||
return Redirect("/");
|
||||
}
|
||||
return NotFound("Account not found");
|
||||
} catch (Exception) {
|
||||
return NotFound("An internal server error has occured");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("verifyemail")]
|
||||
public async Task<ActionResult<bool>> VerifyEmail([FromQuery] int CompanyID, [FromQuery] string EmailToken) {
|
||||
try {
|
||||
Company? test = await _databaseService.GetCompany(CompanyID);
|
||||
if (test != null) {
|
||||
if (test.EmailToken == EmailToken) {
|
||||
test.EmailToken = "";
|
||||
test.EmailVerified = true;
|
||||
await _databaseService.SetCompany(test);
|
||||
return Redirect("/");
|
||||
}
|
||||
return BadRequest("The token isn't valid");
|
||||
}
|
||||
return BadRequest("Account not found"); ;
|
||||
} catch {
|
||||
return BadRequest("An internal server error has occured");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user