Create Auth method for external signin

This commit is contained in:
2025-07-24 21:56:45 -07:00
parent 3c02bf0bc6
commit e21862ecb7
@@ -46,10 +46,48 @@ namespace Auth.Controllers {
test.CurrentPasswordAttempts = 0; test.CurrentPasswordAttempts = 0;
await _databaseService.SetAccount(test); 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); AuthJWT.SignIn(Response, request.StayLoggedIn, jwt);
return Ok(test); 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<ActionResult> 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 { } else {
test.CurrentPasswordAttempts += 1; test.CurrentPasswordAttempts += 1;
await _databaseService.SetAccount(test); await _databaseService.SetAccount(test);
@@ -100,10 +138,12 @@ namespace Auth.Controllers {
return Ok(loadedAccount); return Ok(loadedAccount);
} }
return BadRequest("Unable to create the account"); return BadRequest("Unable to create the account");
} else { }
else {
return BadRequest("Email is already in use"); return BadRequest("Email is already in use");
} }
} else { }
else {
return BadRequest("UserName is taken"); return BadRequest("UserName is taken");
} }
} catch (Exception ex) { } catch (Exception ex) {
@@ -161,7 +201,8 @@ namespace Auth.Controllers {
DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key); DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key);
if (PreviousSentTime.AddMinutes(5) > DateTime.Now) { if (PreviousSentTime.AddMinutes(5) > DateTime.Now) {
return BadRequest("Cannot sent another verify email until 5 minutes has elapsed"); return BadRequest("Cannot sent another verify email until 5 minutes has elapsed");
} else { }
else {
_emailContext._SentEmails.Remove(key); _emailContext._SentEmails.Remove(key);
} }
} }
@@ -203,7 +244,7 @@ namespace Auth.Controllers {
} }
return BadRequest("Your email token has timed out"); return BadRequest("Your email token has timed out");
} }
return BadRequest("Account not found");; return BadRequest("Account not found"); ;
} catch { } catch {
return BadRequest("An internal server error has occured"); return BadRequest("An internal server error has occured");
} }
@@ -219,7 +260,8 @@ namespace Auth.Controllers {
DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key); DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key);
if (PreviousSentTime.AddMinutes(5) > DateTime.Now) { if (PreviousSentTime.AddMinutes(5) > DateTime.Now) {
return BadRequest("Cannot sent another reset requests until 5 minutes has elapsed"); return BadRequest("Cannot sent another reset requests until 5 minutes has elapsed");
} else { }
else {
_emailContext._SentEmails.Remove(key); _emailContext._SentEmails.Remove(key);
} }
} }
@@ -264,7 +306,7 @@ namespace Auth.Controllers {
} }
return BadRequest("Your email token has timed out"); return BadRequest("Your email token has timed out");
} }
return BadRequest("Account not found");; return BadRequest("Account not found"); ;
} catch { } catch {
return BadRequest("An internal server error has occured"); return BadRequest("An internal server error has occured");
} }