From e21862ecb7c7680ede1e85cdcc7c27e743f0341c Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Thu, 24 Jul 2025 21:56:45 -0700 Subject: [PATCH] Create Auth method for external signin --- .../Controllers/AuthenticationController.cs | 56 ++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/Server/Controllers/AuthenticationController.cs b/src/Server/Controllers/AuthenticationController.cs index 2598815..16ce01e 100755 --- a/src/Server/Controllers/AuthenticationController.cs +++ b/src/Server/Controllers/AuthenticationController.cs @@ -46,10 +46,48 @@ namespace Auth.Controllers { test.CurrentPasswordAttempts = 0; await _databaseService.SetAccount(test); - string jwt = AuthJWT.GenereateJWTToken(test.ID, request.StayLoggedIn); + string jwt = AuthJWT.GenereateJWTToken(test, request.StayLoggedIn); AuthJWT.SignIn(Response, request.StayLoggedIn, jwt); return Ok(test); + } + else { + test.CurrentPasswordAttempts += 1; + await _databaseService.SetAccount(test); + return BadRequest("Wrong Password"); + } + } + else { + await SendVerify(new SendVerifyEmailRequest { + UserName = test.UserName + }); + return BadRequest("A new verify email has been sent. \n Note only 1 email send every 5 mintes"); + } + } + return BadRequest("Account Not Found"); + } catch (Exception ex) { + Console.WriteLine("Login Error: " + ex.Message); + return BadRequest("An internal server error has occured"); + } + } + + [Route("authenticate")] + [HttpPost] + public async Task Authenticate([FromBody] LoginRequest request) { + try { + Account? test = await _databaseService.GetAccount(request.UserName.ToLower()); + if (test != null) { + if (test.EmailVerified == true) { + if (test.FailedPasswordLock) { + if (test.CurrentPasswordAttempts >= test.PasswordAttempts) { + return BadRequest("Too many failed password attempts. Please reset your password"); + } + } + if (BCrypt.Net.BCrypt.Verify(request.Password, test.PasswordHash)) { + test.CurrentPasswordAttempts = 0; + await _databaseService.SetAccount(test); + + return Ok(AuthJWT.GenereateJWTToken(test, request.StayLoggedIn)); } else { test.CurrentPasswordAttempts += 1; await _databaseService.SetAccount(test); @@ -100,10 +138,12 @@ namespace Auth.Controllers { return Ok(loadedAccount); } return BadRequest("Unable to create the account"); - } else { + } + else { return BadRequest("Email is already in use"); } - } else { + } + else { return BadRequest("UserName is taken"); } } catch (Exception ex) { @@ -161,7 +201,8 @@ namespace Auth.Controllers { DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key); if (PreviousSentTime.AddMinutes(5) > DateTime.Now) { return BadRequest("Cannot sent another verify email until 5 minutes has elapsed"); - } else { + } + else { _emailContext._SentEmails.Remove(key); } } @@ -203,7 +244,7 @@ namespace Auth.Controllers { } return BadRequest("Your email token has timed out"); } - return BadRequest("Account not found");; + return BadRequest("Account not found"); ; } catch { return BadRequest("An internal server error has occured"); } @@ -219,7 +260,8 @@ namespace Auth.Controllers { DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key); if (PreviousSentTime.AddMinutes(5) > DateTime.Now) { return BadRequest("Cannot sent another reset requests until 5 minutes has elapsed"); - } else { + } + else { _emailContext._SentEmails.Remove(key); } } @@ -264,7 +306,7 @@ namespace Auth.Controllers { } return BadRequest("Your email token has timed out"); } - return BadRequest("Account not found");; + return BadRequest("Account not found"); ; } catch { return BadRequest("An internal server error has occured"); }