39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using Backend.Contracts;
|
|
using Backend.Domain;
|
|
using Backend.Services;
|
|
|
|
namespace Backend.Endpoints;
|
|
|
|
public static partial class AuthEndpoints
|
|
{
|
|
private static async Task<IResult> GetSession(HttpContext context, IUserSessionService userSessionService)
|
|
{
|
|
var session = await userSessionService.ResolveSessionAsync(context, context.RequestAborted);
|
|
if (session is null)
|
|
{
|
|
return Results.Unauthorized();
|
|
}
|
|
|
|
return Results.Ok(ToAuthSessionDto(session));
|
|
}
|
|
|
|
private static async Task<IResult> Logout(HttpContext context, IUserSessionService userSessionService)
|
|
{
|
|
var session = await userSessionService.ResolveSessionAsync(context, context.RequestAborted);
|
|
if (session is null)
|
|
{
|
|
return Results.Ok(new { loggedOut = true });
|
|
}
|
|
|
|
await userSessionService.LogoutAsync(session, context.RequestAborted);
|
|
return Results.Ok(new { loggedOut = true });
|
|
}
|
|
|
|
private static AuthSessionDto ToAuthSessionDto(UserSession session) =>
|
|
new(
|
|
session.SessionToken,
|
|
session.TwitchUserId,
|
|
session.DisplayName,
|
|
session.Role);
|
|
}
|