summaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/Internal/Account/GetRoute.cs
blob: 34a3c97c84082acc8ae32757951ba65b2df3ac10 (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
namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;

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

	/// <inheritdoc />
	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.SingleOrDefault(c => c.Id == LoggedInUser.Id);
		if (user != default) {
			return Ok(new LoggedInUserModel {
					Id = LoggedInUser.Id,
					Username = LoggedInUser.Username
			});
		}

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