23 lines
736 B
C#
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();
|
|
}
|
|
}
|
|
}
|