From b24b9d697f1e294cd49fc107e4c8c8bfdc87a2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D0=B0=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=A0=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BC?= Date: Fri, 13 Feb 2026 11:45:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20CORS,=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BF=D1=80=D0=BE=D1=84=D0=B8=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Host/Program.cs | 17 +++++++- .../Queries/GetProfile/GetProfileQuery.cs | 41 +++++++++++++++++++ .../Endpoints/IdentityEndpoints.cs | 23 +++++++---- 3 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/Modules/Identity/Application/Queries/GetProfile/GetProfileQuery.cs diff --git a/src/Host/Program.cs b/src/Host/Program.cs index 56f68e0..0e1486e 100644 --- a/src/Host/Program.cs +++ b/src/Host/Program.cs @@ -4,14 +4,14 @@ using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Nashel.Modules.Catalog.Infrastructure; using Nashel.Modules.Catalog.Presentation.Endpoints; +using Nashel.Modules.Collaboration.Infrastructure; +using Nashel.Modules.Collaboration.Presentation; using Nashel.Modules.Geo.Infrastructure; using Nashel.Modules.Geo.Presentation; using Nashel.Modules.Identity.Infrastructure; using Nashel.Modules.Identity.Presentation.Endpoints; using Nashel.Modules.Order.Infrastructure; using Nashel.Modules.Order.Presentation; -using Nashel.Modules.Collaboration.Infrastructure; -using Nashel.Modules.Collaboration.Presentation; using Nashel.Modules.Reputation.Infrastructure; using Nashel.Modules.Reputation.Presentation; @@ -91,6 +91,17 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) builder.Services.AddAuthorization(); +builder.Services.AddCors(options => +{ + options.AddPolicy("FrontendPolicy", policy => + { + policy.WithOrigins("http://localhost:3000", "http://127.0.0.1:3000") + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials(); + }); +}); + var app = builder.Build(); // Настройка конвейера HTTP-запросов. @@ -103,6 +114,8 @@ if (app.Environment.IsDevelopment()) }); } +app.UseCors("FrontendPolicy"); + // Включение аутентификации и авторизации app.UseAuthentication(); app.UseAuthorization(); diff --git a/src/Modules/Identity/Application/Queries/GetProfile/GetProfileQuery.cs b/src/Modules/Identity/Application/Queries/GetProfile/GetProfileQuery.cs new file mode 100644 index 0000000..ddc6a6e --- /dev/null +++ b/src/Modules/Identity/Application/Queries/GetProfile/GetProfileQuery.cs @@ -0,0 +1,41 @@ +using MediatR; +using Nashel.BuildingBlocks.Application.Abstractions; +using Nashel.Modules.Identity.Domain.Repositories; + +namespace Nashel.Modules.Identity.Application.Queries.GetProfile; + +public record GetProfileQuery : IRequest; + +public record ProfileResponse(Guid Id, string Phone, List Roles); + +public class GetProfileQueryHandler : IRequestHandler +{ + private readonly IAccountRepository _accountRepository; + private readonly ICurrentUserService _currentUserService; + + public GetProfileQueryHandler(IAccountRepository accountRepository, ICurrentUserService currentUserService) + { + _accountRepository = accountRepository; + _currentUserService = currentUserService; + } + + public async Task Handle(GetProfileQuery request, CancellationToken cancellationToken) + { + var userId = _currentUserService.UserId; + if (userId == null) + { + throw new UnauthorizedAccessException(); + } + + var account = await _accountRepository.GetByIdAsync(userId.Value, cancellationToken); + if (account == null) + { + throw new Exception("Account not found"); + } + + return new ProfileResponse( + account.Id, + account.Phone, + account.Roles.Select(r => r.ToString()).ToList()); + } +} diff --git a/src/Modules/Identity/Presentation/Endpoints/IdentityEndpoints.cs b/src/Modules/Identity/Presentation/Endpoints/IdentityEndpoints.cs index 9fdf7b6..d074fa1 100644 --- a/src/Modules/Identity/Presentation/Endpoints/IdentityEndpoints.cs +++ b/src/Modules/Identity/Presentation/Endpoints/IdentityEndpoints.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Nashel.Modules.Identity.Application.Commands; +using Nashel.Modules.Identity.Application.Queries.GetProfile; namespace Nashel.Modules.Identity.Presentation.Endpoints; @@ -18,30 +19,34 @@ public static class IdentityEndpoints return Results.Ok(result); }) .WithName("Register") - .WithOpenApi(operation => new(operation) { Summary = "Регистрация пользователя", Description = "Регистрирует нового пользователя по номеру телефона и паролю." }); + .WithOpenApi(operation => new(operation) { Summary = "Регистрация пользователя" }); authGroup.MapPost("/login", async (LoginCommand command, ISender sender) => { var token = await sender.Send(command); - return Results.Ok(new LoginResponse(token)); + return Results.Ok(new LoginResponse(token, token)); // Пока что возвращаем один токен для обоих полей }) .WithName("Login") - .WithOpenApi(operation => new(operation) { Summary = "Вход в систему", Description = "Аутентификация пользователя и получение JWT токена." }); + .WithOpenApi(operation => new(operation) { Summary = "Вход в систему" }); var profileGroup = app.MapGroup("/api/profile").WithTags("Profile").RequireAuthorization(); + profileGroup.MapGet("/", async (ISender sender) => + { + var profile = await sender.Send(new GetProfileQuery()); + return Results.Ok(profile); + }) + .WithName("GetProfile") + .WithOpenApi(operation => new(operation) { Summary = "Получить профиль" }); + profileGroup.MapPost("/become-performer", async (ISender sender) => { await sender.Send(new BecomePerformerCommand()); return Results.Ok(); }) .WithName("BecomePerformer") - .WithOpenApi(operation => new(operation) { Summary = "Стать исполнителем", Description = "Присваивает текущему пользователю роль исполнителя." }); + .WithOpenApi(operation => new(operation) { Summary = "Стать исполнителем" }); } } -/// -/// Ответ на успешный вход в систему. -/// -/// JWT токен доступа. -public record LoginResponse(string Token); +public record LoginResponse(string AccessToken, string RefreshToken);