mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using BusinessLayer.Services;
|
|
using DataAccessLayer.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace WebAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthController(IAuthService auth) : ControllerBase
|
|
{
|
|
public record RegisterRequest(
|
|
string Username,
|
|
string FirstName,
|
|
string LastName,
|
|
string Email,
|
|
DateTime DateOfBirth,
|
|
string Password
|
|
);
|
|
|
|
public record LoginRequest(string UsernameOrEmail, string Password);
|
|
|
|
[HttpPost("register")]
|
|
public async Task<ActionResult<UserAccount>> Register([FromBody] RegisterRequest req)
|
|
{
|
|
var user = new UserAccount
|
|
{
|
|
UserAccountId = Guid.Empty,
|
|
Username = req.Username,
|
|
FirstName = req.FirstName,
|
|
LastName = req.LastName,
|
|
Email = req.Email,
|
|
DateOfBirth = req.DateOfBirth
|
|
};
|
|
|
|
var created = await auth.RegisterAsync(user, req.Password);
|
|
return CreatedAtAction(nameof(Register), new { id = created.UserAccountId }, created);
|
|
}
|
|
|
|
[HttpPost("login")]
|
|
public async Task<ActionResult> Login([FromBody] LoginRequest req)
|
|
{
|
|
var ok = await auth.LoginAsync(req.UsernameOrEmail, req.Password);
|
|
if (!ok) return Unauthorized();
|
|
return Ok(new { success = true });
|
|
}
|
|
}
|
|
}
|