feat(identity): add /users/@me endpoint

This commit is contained in:
sam 2024-05-22 17:20:00 +02:00
parent 8bd118ea67
commit 2919413118
Signed by: sam
GPG key ID: B4EF20DDE721CAA1

View file

@ -1,12 +1,17 @@
using Foxchat.Core; using Foxchat.Core;
using Foxchat.Core.Models; using Foxchat.Core.Models;
using Foxchat.Identity.Database; using Foxchat.Identity.Database;
using Foxchat.Identity.Database.Models;
using Foxchat.Identity.Middleware;
using Foxchat.Identity.Utils;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace Foxchat.Identity.Controllers; namespace Foxchat.Identity.Controllers;
[ApiController] [ApiController]
[ClientAuthenticate]
[Route("/_fox/ident/users")] [Route("/_fox/ident/users")]
public class UsersController(ILogger logger, InstanceConfig config, IdentityContext db) : ControllerBase public class UsersController(ILogger logger, InstanceConfig config, IdentityContext db) : ControllerBase
{ {
@ -18,4 +23,30 @@ public class UsersController(ILogger logger, InstanceConfig config, IdentityCont
return Ok(new Users.User(user.Id.ToString(), user.Username, config.Domain, null)); return Ok(new Users.User(user.Id.ToString(), user.Username, config.Domain, null));
} }
[HttpGet("@me")]
[Authorize("identify")]
public IActionResult GetMe()
{
var acct = HttpContext.GetAccountOrThrow();
var token = HttpContext.GetToken()!;
var showEmail = token.Scopes.ExpandScopes().Contains("email");
return Ok(new MeUser(
acct.Id,
acct.Username,
acct.Role,
null,
showEmail ? acct.Email : null
));
}
public record MeUser(
Ulid Id,
string Username,
Account.AccountRole Role,
string? AvatarUrl,
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
string? Email
);
} }