classlib/ExpenseTracker.Application/Common/Authorization/MustBeAuthenticatedRequirement.cs
2024-08-07 21:12:02 +03:00

23 lines
736 B
C#

using MediatR.Behaviors.Authorization;
using ExpenseTracker.Application.Common.Exceptions;
namespace ExpenseTracker.Application.Common.Authorization;
public class MustBeAuthenticatedRequirement : IAuthorizationRequirement
{
public required bool IsAuthenticated { get; init; } = default!;
class MustBeAuthenticatedRequirementHandler : IAuthorizationHandler<MustBeAuthenticatedRequirement>
{
public async Task<AuthorizationResult> Handle(MustBeAuthenticatedRequirement request, CancellationToken cancellationToken)
{
if (!request.IsAuthenticated)
{
throw new UnAuthorizedException();
}
return AuthorizationResult.Succeed();
}
}
}