Update API to follow REST
This commit is contained in:
@@ -5,6 +5,8 @@ using System.Security.Claims;
|
||||
using BoredCareers.Services;
|
||||
using BoredCareers.Services.DatabaseService;
|
||||
using BoredCareers.Entities;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace BoredCareers.Controllers {
|
||||
[ApiController]
|
||||
@@ -26,7 +28,7 @@ namespace BoredCareers.Controllers {
|
||||
if (test.EmailVerified == true) {
|
||||
if (test.FailedPasswordLock) {
|
||||
if (test.CurrentPasswordAttempts >= test.PasswordAttempts) {
|
||||
return new Account() { ID = -1, UserName = "Too many failed password attempts. Please reset your password" };
|
||||
return NotFound("Too many failed password attempts. Please reset your password");
|
||||
}
|
||||
}
|
||||
if (BCrypt.Net.BCrypt.Verify(PasswordHash, test.PasswordHash)) {
|
||||
@@ -47,21 +49,20 @@ namespace BoredCareers.Controllers {
|
||||
}
|
||||
);
|
||||
return test;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
test.CurrentPasswordAttempts += 1;
|
||||
await _databaseService.SetAccount(test);
|
||||
return new Account() { ID = -1, UserName = "Wrong Password" };
|
||||
return Ok(new Account() { ID = -1, UserName = "Wrong Password" });
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
await SendVerify(test.UserName);
|
||||
return new Account() { ID = -1, UserName = "A new verify email has been sent. \n Note only 1 email send every 5 mintes" };
|
||||
return NotFound("A new verify email has been sent. \n Note only 1 email send every 5 mintes");
|
||||
}
|
||||
}
|
||||
return new Account() { ID = -1, UserName = "User doesn't exist" };
|
||||
return NotFound("Account Not Found");
|
||||
} catch (Exception ex) {
|
||||
return new Account() { ID = -1, UserName = ex.Message };
|
||||
Console.WriteLine("Login Error: " + ex.Message);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,37 +72,35 @@ namespace BoredCareers.Controllers {
|
||||
try {
|
||||
if (await _databaseService.GetAccount(UserName.ToLower()) == null) {
|
||||
if (await _databaseService.GetAccount(Email.ToLower()) == null) {
|
||||
Account? created = new Account() {
|
||||
Account created = new Account() {
|
||||
UserName = UserName.ToLower(),
|
||||
Email = Email.ToLower(),
|
||||
EmailVerified = false,
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword(PasswordHash),
|
||||
};
|
||||
await _databaseService.SetAccount(created);
|
||||
created = await _databaseService.GetAccount(Email.ToLower());
|
||||
if (created != null) {
|
||||
await SendVerify(created.UserName);
|
||||
return created;
|
||||
Account? loadedAccount = await _databaseService.GetAccount(Email.ToLower());
|
||||
if (loadedAccount != null) {
|
||||
await SendVerify(loadedAccount.UserName);
|
||||
return Ok(loadedAccount);
|
||||
}
|
||||
return new Account() { ID = -1, UserName = "Unknown Error" };
|
||||
return NotFound("Unable to create the account");
|
||||
} else {
|
||||
return NotFound("Email is already in use");
|
||||
}
|
||||
else {
|
||||
return new Account() { ID = -1, UserName = "Email is already in use" };
|
||||
}
|
||||
}
|
||||
else {
|
||||
return new Account() { ID = -1, UserName = "UserName is taken" };
|
||||
} else {
|
||||
return NotFound("UserName is taken");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("Error: " + ex.Message);
|
||||
return new Account() { ID = -1, UserName = ex.Message };
|
||||
Console.WriteLine("Register Error: " + ex.Message);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Route("changepassword")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<bool>> ChangePassword([FromForm] string OldPassword, [FromForm] string NewPassword) {
|
||||
public async Task<ActionResult> ChangePassword([FromForm] string OldPassword, [FromForm] string NewPassword) {
|
||||
try {
|
||||
if (isLoggedIn()) {
|
||||
Account user = await getLoggedInUser();
|
||||
@@ -109,12 +108,13 @@ namespace BoredCareers.Controllers {
|
||||
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(NewPassword);
|
||||
user.CurrentPasswordAttempts = 0;
|
||||
await _databaseService.SetAccount(user);
|
||||
return true;
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
return NotFound();
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("ChangePassword Error: " + ex.Message);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,31 +127,37 @@ namespace BoredCareers.Controllers {
|
||||
user.FailedPasswordLock = AccountLock;
|
||||
user.CurrentPasswordAttempts = 0;
|
||||
await _databaseService.SetAccount(user);
|
||||
return "Account Lock Status Updated";
|
||||
return Ok();
|
||||
}
|
||||
return "Unknown Error Occurred";
|
||||
return NotFound();
|
||||
} catch (Exception ex) {
|
||||
return ex.Message;
|
||||
Console.WriteLine("ToggleAccountLock Error: " + ex.Message);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[Route("get")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Account?>> Get() {
|
||||
public async Task<ActionResult<Account>> Get() {
|
||||
try {
|
||||
if (isLoggedIn()) {
|
||||
return await getLoggedInUser();
|
||||
return Ok(await getLoggedInUser());
|
||||
}
|
||||
return Ok();
|
||||
} catch {
|
||||
return Ok();
|
||||
return NotFound();
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("Get Error: " + ex);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[Route("logout")]
|
||||
[HttpPost]
|
||||
public async Task Logout() {
|
||||
await HttpContext.SignOutAsync();
|
||||
public async Task<ActionResult> Logout() {
|
||||
if (isLoggedIn()) {
|
||||
await HttpContext.SignOutAsync();
|
||||
return Ok();
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[Route("sendverifyemail")]
|
||||
@@ -267,18 +273,19 @@ namespace BoredCareers.Controllers {
|
||||
|
||||
[Route("delete")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<bool>> delete([FromForm] string Password) {
|
||||
public async Task<ActionResult> delete([FromForm] string Password) {
|
||||
try {
|
||||
if (isLoggedIn()) {
|
||||
Account user = await getLoggedInUser();
|
||||
if (BCrypt.Net.BCrypt.Verify(Password, user.PasswordHash)) {
|
||||
await _databaseService.DeleteAccount(user.ID);
|
||||
return true;
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
return NotFound();
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine("Delete Error: " + ex.Message);
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user