Added login/register function. Implemented authentication. Started authorization

This commit is contained in:
Masu-Baumgartner
2024-10-01 11:29:19 +02:00
parent 73bf27d222
commit ef2e6c9a20
23 changed files with 741 additions and 27 deletions

View File

@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Mvc;
using Moonlight.ApiServer.Services;
using Moonlight.Shared.Http.Requests.Auth;
using Moonlight.Shared.Http.Responses.Auth;
namespace Moonlight.ApiServer.Http.Controllers.Auth;
[ApiController]
[Route("api/auth")]
public class AuthController : Controller
{
private readonly AuthService AuthService;
public AuthController(AuthService authService)
{
AuthService = authService;
}
[HttpPost("login")]
public async Task<LoginResponse> Login([FromBody] LoginRequest request)
{
var user = await AuthService.Login(request.Email, request.Password);
return new LoginResponse()
{
Token = await AuthService.GenerateToken(user)
};
}
[HttpPost("register")]
public async Task<RegisterResponse> Register([FromBody] RegisterRequest request)
{
var user = await AuthService.Register(
request.Username,
request.Email,
request.Password
);
return new RegisterResponse()
{
Token = await AuthService.GenerateToken(user)
};
}
}