mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: login & registration; enhanced paste view; custom dropdown + search; navbar; global user access via store; auto token refresh; paste creation as user; /api/user/me, returns info on yourself including pastes;
fix: auth check refactor: remove user UID api endpoint
This commit is contained in:
@@ -22,6 +22,16 @@ namespace SharpbinV3.Server.Controllers
|
||||
var existingUser = await _userService.GetByUsername(request.Username);
|
||||
if (existingUser != null)
|
||||
return Conflict(new { message = "Username already exists." });
|
||||
if (!string.IsNullOrEmpty(request.Email))
|
||||
{
|
||||
var existingEmailUser = await _userService.GetByEmail(request.Email);
|
||||
if (existingEmailUser != null)
|
||||
return Conflict(new { message = "Email already in use." });
|
||||
if (!IsValidEmail(request.Email))
|
||||
return BadRequest(new { message = "Invalid email." });
|
||||
}
|
||||
if (!IsValidPassword(request.Password))
|
||||
return BadRequest(new { message = "Password should have at least 8 characters, including uppercase, lowercase, and digits." });
|
||||
|
||||
var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName);
|
||||
return Ok(new
|
||||
@@ -75,5 +85,31 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(new { message = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static bool IsValidPassword(string password)
|
||||
{
|
||||
if (password.Length < 6)
|
||||
return false;
|
||||
if (!password.Any(char.IsUpper))
|
||||
return false;
|
||||
if (!password.Any(char.IsLower))
|
||||
return false;
|
||||
if (!password.Any(char.IsDigit))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
private static bool IsValidEmail(string email)
|
||||
{
|
||||
try
|
||||
{
|
||||
var addr = new System.Net.Mail.MailAddress(email);
|
||||
return addr.Address == email;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,13 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||
var httpUser = HttpContext.User;
|
||||
foreach (var key in httpUser.Claims)
|
||||
{
|
||||
Console.WriteLine($"Claim: {key.Type} = {key.Value}");
|
||||
}
|
||||
var userUUID = httpUser?.FindFirst("UUID")?.Value;
|
||||
User? user = userUUID is not null ? await _userService.GetByUUID(Guid.Parse(userUUID)) : null;
|
||||
Console.WriteLine($"Creating paste for user: {(user != null ? user.Username : "Anonymous")}");
|
||||
Paste paste = await _pasteService.Create(user, content, title, syntax, visibility, expiresAt);
|
||||
return Ok(new
|
||||
{
|
||||
@@ -62,12 +67,13 @@ namespace SharpbinV3.Server.Controllers
|
||||
paste.Syntax,
|
||||
paste.Visibility,
|
||||
paste.ExpiresAt,
|
||||
Author = paste.User != null && paste.User.Visiblity == 0 ? new
|
||||
Author = paste.User != null && paste.User.Visibility == 0 ? new
|
||||
{
|
||||
paste.User.UID,
|
||||
paste.User.UUID,
|
||||
paste.User.Username,
|
||||
paste.User.Visiblity
|
||||
paste.User.DisplayName,
|
||||
paste.User.Visibility
|
||||
} : null
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,15 +10,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
private readonly IUserService _userService = userService;
|
||||
|
||||
[HttpGet("{uid}")]
|
||||
public async Task<IActionResult> GetByUid(int uid)
|
||||
{
|
||||
var user = await _userService.GetByUID(uid);
|
||||
if (user == null) return NotFound();
|
||||
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
}
|
||||
[HttpGet("username/{username}")]
|
||||
[HttpGet("{username}")]
|
||||
public async Task<IActionResult> GetByUsername(string username)
|
||||
{
|
||||
var user = await _userService.GetByUsername(username);
|
||||
@@ -26,12 +18,45 @@ namespace SharpbinV3.Server.Controllers
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
}
|
||||
[HttpGet("uuid/{uuid}")]
|
||||
[Authorize]
|
||||
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 });
|
||||
}
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetMe()
|
||||
{
|
||||
var httpUser = HttpContext.User;
|
||||
var userUUID = httpUser?.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 {
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Syntax,
|
||||
p.Size,
|
||||
p.TrueSize,
|
||||
p.Views,
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt
|
||||
});
|
||||
return Ok(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