aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/Internal/Account/GetAccountRoute.cs
blob: 121b40f01cc27f05397ee9bbe65f3e3e51a0b20d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;

public class GetAccountRoute : RouteBaseAsync.WithoutRequest.WithActionResult<LoggedInUserModel>
{
    private readonly MainAppDatabase _database;

    public GetAccountRoute(MainAppDatabase database) {
        _database = database;
    }

    [HttpGet("~/_/account")]
    public override async Task<ActionResult<LoggedInUserModel>> HandleAsync(CancellationToken cancellationToken = default) {
        var user = _database.Users
            .Select(x => new {x.Username, x.Id})
            .SingleOrDefault(c => c.Id == LoggedInUser.Id);
        if (user != default) {
            return Ok(new LoggedInUserModel {
                Id = LoggedInUser.Id,
                Username = LoggedInUser.Username
            });
        }

        await HttpContext.SignOutAsync();
        return Unauthorized();
    }
}