🐛 Cors, and bug fixes
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@ -6,7 +7,7 @@ namespace DysonNetwork.Sphere.Account;
|
||||
|
||||
[ApiController]
|
||||
[Route("/accounts")]
|
||||
public class AccountController(AppDatabase db)
|
||||
public class AccountController(AppDatabase db, IHttpContextAccessor httpContext)
|
||||
{
|
||||
[HttpGet("{name}")]
|
||||
[ProducesResponseType<Account>(StatusCodes.Status200OK)]
|
||||
@ -22,13 +23,22 @@ public class AccountController(AppDatabase db)
|
||||
[Required] [MaxLength(256)] public string Name { get; set; } = string.Empty;
|
||||
[Required] [MaxLength(256)] public string Nick { get; set; } = string.Empty;
|
||||
[Required] [MaxLength(1024)] public string Email { get; set; } = string.Empty;
|
||||
[Required] [MinLength(4)] [MaxLength(128)] public string Password { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MinLength(4)]
|
||||
[MaxLength(128)]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType<Account>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<Account>> CreateAccount([FromBody] AccountCreateRequest request)
|
||||
{
|
||||
var dupeNameCount = await db.Accounts.Where(a => a.Name == request.Name).CountAsync();
|
||||
if (dupeNameCount > 0)
|
||||
return new BadRequestObjectResult("The name is already taken.");
|
||||
|
||||
var account = new Account
|
||||
{
|
||||
Name = request.Name,
|
||||
@ -50,9 +60,23 @@ public class AccountController(AppDatabase db)
|
||||
}.HashSecret()
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
await db.Accounts.AddAsync(account);
|
||||
await db.SaveChangesAsync();
|
||||
return account;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("me")]
|
||||
[ProducesResponseType<Account>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<Account>> GetMe()
|
||||
{
|
||||
var userIdClaim = httpContext.HttpContext?.User.FindFirst("user_id")?.Value;
|
||||
long? userId = long.TryParse(userIdClaim, out var id) ? id : null;
|
||||
if (userId is null) return new BadRequestObjectResult("Invalid or missing user_id claim.");
|
||||
|
||||
var account = await db.Accounts.FindAsync(userId);
|
||||
|
||||
return new OkObjectResult(account);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user