feat(backend): add RequestDiscordTokenAsync method

This commit is contained in:
sam 2024-06-12 16:19:49 +02:00
parent 2a7bd746aa
commit 6186eda092
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
12 changed files with 230 additions and 22 deletions

View file

@ -6,18 +6,31 @@ using NodaTime;
namespace Foxnouns.Backend.Controllers.Authentication;
[Route("/api/v2/auth/email")]
public class EmailAuthController(DatabaseContext db, AuthService authSvc, UserRendererService userRendererSvc, IClock clock, ILogger logger) : ApiControllerBase
public class EmailAuthController(
DatabaseContext db,
AuthService authSvc,
UserRendererService userRendererSvc,
IClock clock,
ILogger logger) : ApiControllerBase
{
[HttpPost("login")]
[ProducesResponseType<AuthController.AuthResponse>(StatusCodes.Status200OK)]
public async Task<IActionResult> LoginAsync([FromBody] LoginRequest req)
{
var user = await authSvc.AuthenticateUserAsync(req.Email, req.Password);
var (user, authenticationResult) = await authSvc.AuthenticateUserAsync(req.Email, req.Password);
if (authenticationResult == AuthService.EmailAuthenticationResult.MfaRequired)
throw new NotImplementedException("MFA is not implemented yet");
var frontendApp = await db.GetFrontendApplicationAsync();
logger.Debug("Logging user {Id} in with email and password", user.Id);
var (tokenStr, token) =
authSvc.GenerateToken(user, frontendApp, ["*"], clock.GetCurrentInstant() + Duration.FromDays(365));
db.Add(token);
logger.Debug("Generated token {TokenId} for {UserId}", user.Id, token.Id);
await db.SaveChangesAsync();
return Ok(new AuthController.AuthResponse(