summaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/Internal/Account/GetRoute.cs
blob: 1aa7ecbfc8c46dd9d6590dd56a466c6d55681129 (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
27
28
29
30
31
namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;

public class GetAccountRoute : RouteBaseAsync.WithoutRequest.WithActionResult<LoggedInUserModel>
{
    private readonly AppDbContext _context;

    public GetAccountRoute(AppDbContext context) {
        _context = context;
    }

    /// <summary>
    /// Get the logged on user's session data.
    /// </summary>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpGet("~/_/account")]
    public override async Task<ActionResult<LoggedInUserModel>> HandleAsync(CancellationToken cancellationToken = default) {
        var user = _context.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();
    }
}