Merge pull request #35 from StewKI/feature-client-with-working-auth
Feature client with working auth
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
using AipsCore.Application.Abstract.Query;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Query.GetMe;
|
||||
|
||||
public record GetMeQuery : IQuery<GetMeQueryDto>;
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace AipsCore.Application.Models.User.Query.GetMe;
|
||||
|
||||
public record GetMeQueryDto(string UserName);
|
||||
@@ -0,0 +1,37 @@
|
||||
using AipsCore.Application.Abstract.Query;
|
||||
using AipsCore.Application.Abstract.UserContext;
|
||||
using AipsCore.Domain.Common.Validation;
|
||||
using AipsCore.Domain.Models.User.Validation;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Query.GetMe;
|
||||
|
||||
public class GetMeQueryHandler : IQueryHandler<GetMeQuery, GetMeQueryDto>
|
||||
{
|
||||
private readonly AipsDbContext _context;
|
||||
private readonly IUserContext _userContext;
|
||||
|
||||
public GetMeQueryHandler(AipsDbContext context, IUserContext userContext)
|
||||
{
|
||||
_context = context;
|
||||
_userContext = userContext;
|
||||
}
|
||||
|
||||
public async Task<GetMeQueryDto> Handle(GetMeQuery query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var userId = _userContext.GetCurrentUserId();
|
||||
|
||||
var result = await _context.Users
|
||||
.Where(u => u.Id.ToString() == userId.IdValue)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (result is null)
|
||||
{
|
||||
throw new ValidationException(UserErrors.NotFound(new UserId(userId.IdValue)));
|
||||
}
|
||||
|
||||
return new GetMeQueryDto(result.UserName!);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
using AipsCore.Application.Abstract;
|
||||
using AipsCore.Application.Common.Authentication.Dtos;
|
||||
using AipsCore.Application.Abstract.MessageBroking;
|
||||
using AipsCore.Application.Common.Authentication;
|
||||
using AipsCore.Application.Common.Message.TestMessage;
|
||||
using AipsCore.Application.Models.User.Command.LogIn;
|
||||
using AipsCore.Application.Models.User.Command.LogOut;
|
||||
using AipsCore.Application.Models.User.Command.LogOutAll;
|
||||
using AipsCore.Application.Models.User.Command.RefreshLogIn;
|
||||
using AipsCore.Application.Models.User.Command.SignUp;
|
||||
using AipsCore.Application.Models.User.Query.GetUser;
|
||||
using AipsCore.Application.Models.User.Query.GetMe;
|
||||
using AipsCore.Infrastructure.Persistence.User;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AipsWebApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[Route("/api/[controller]")]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly IDispatcher _dispatcher;
|
||||
@@ -72,4 +72,12 @@ public class UserController : ControllerBase
|
||||
var test = new TestMessage("ovo je test poruka");
|
||||
await publisher.PublishAsync(test);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("me")]
|
||||
public async Task<ActionResult<GetMeQueryDto>> GetMe(CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await _dispatcher.Execute(new GetMeQuery(), cancellationToken);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,18 @@ builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddAips(builder.Configuration);
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("frontend", policy =>
|
||||
{
|
||||
policy
|
||||
.WithOrigins("http://localhost:5173")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
await app.Services.InitializeInfrastructureAsync();
|
||||
@@ -24,6 +36,8 @@ if (app.Environment.IsDevelopment())
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseCors("frontend");
|
||||
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
app.UseAuthentication();
|
||||
|
||||
Reference in New Issue
Block a user