mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: implement server-side user data fetching; enhance user controller for detailed responses; update token handling to use cookies
This commit is contained in:
@@ -1,41 +1,53 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.Services;
|
||||
|
||||
namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(IUserService userService) : ControllerBase
|
||||
public class UserController(IUserService userService, AppDbContext db) : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService = userService;
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
[HttpGet("{username}")]
|
||||
public async Task<IActionResult> GetByUsername(string username)
|
||||
{
|
||||
var user = await _userService.GetByUsername(username);
|
||||
if (user == null) return NotFound();
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
var user = await _userService.GetByUsername(username, withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
}
|
||||
|
||||
[HttpGet("uuid/{uuid}")]
|
||||
public async Task<IActionResult> GetByUUID(Guid uuid)
|
||||
{
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null) return NotFound();
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
var user = await _userService.GetByUUID(uuid, withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetMe()
|
||||
{
|
||||
var httpUser = HttpContext.User;
|
||||
var userUUID = httpUser?.FindFirst("UUID")?.Value;
|
||||
var userUUID = HttpContext.User?.FindFirst("UUID")?.Value;
|
||||
if (userUUID is null)
|
||||
return Unauthorized(new { message = "Invalid token." });
|
||||
var user = await _userService.GetByUUID(Guid.Parse(userUUID), true);
|
||||
if (user == null)
|
||||
return NotFound();
|
||||
var pastes = user.Pastes?.Select(p => new {
|
||||
|
||||
var user = await _userService.GetByUUID(Guid.Parse(userUUID), withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
}
|
||||
|
||||
private async Task<object> BuildUserResponse(User user)
|
||||
{
|
||||
var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString();
|
||||
var pasteQuery = _db.Pastes.AsNoTracking().Where(p => p.AuthorUUID == user.UUID);
|
||||
if (!isAuthenticatedUser)
|
||||
pasteQuery = pasteQuery.Where(p => p.Visibility == 0);
|
||||
|
||||
var pastes = await pasteQuery.Select(p => new {
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Syntax,
|
||||
@@ -45,18 +57,30 @@ namespace SharpbinV3.Server.Controllers
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt
|
||||
});
|
||||
return Ok(new {
|
||||
}).ToListAsync();
|
||||
|
||||
if (isAuthenticatedUser)
|
||||
{
|
||||
return new {
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
user.DisplayName,
|
||||
user.Email,
|
||||
user.LastLogin,
|
||||
user.Roles,
|
||||
user.Visibility,
|
||||
Pastes = pastes
|
||||
};
|
||||
}
|
||||
|
||||
return new {
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
user.DisplayName,
|
||||
user.Email,
|
||||
user.LastLogin,
|
||||
user.Roles,
|
||||
user.Visibility,
|
||||
Pastes = pastes
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user