MyBookmark/Modules.User.WebApi/Controllers/AccountController.cs
THE_KONDRAT 7b16d72329 ui and login
mongo => postgres
2024-11-03 16:08:39 +03:00

150 lines
4.7 KiB
C#

using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Modules.User.Api;
using Modules.User.Application;
using Modules.User.Application.Commands;
using Modules.User.Application.Models;
using Modules.User.Application.Queries;
using Modules.User.Application.Settings;
using Modules.User.WebApi.Models;
using System.Net;
namespace Modules.User.WebApi.Controllers;
[ApiController]
[Route("[controller]")]
[ProducesResponseType(400, StatusCode = 400, Type = typeof(ProblemDetails))]
[ProducesResponseType(401, StatusCode = 401, Type = typeof(UnauthorizedResult))]
[Authorize]
public class AccountController : ControllerBase
{
private readonly IMediator _mediator;
private readonly UserContext _userContext;
private readonly ILogger<AccountController> _logger;
public AccountController(UserContext userContext, IMediator mediator, ILogger<AccountController> logger)
{
_userContext = userContext;
_mediator = mediator;
_logger = logger;
}
[HttpGet("Sessions")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(List<Session>))]
public async Task<IActionResult> GetSessions()
{
var user = await _userContext.GetUserInfo();
if (user?.AccountId == null) return Ok(new List<Session>());
return Ok(await _mediator.Send(new GetAccountSessionsQuery { AccountId = user.AccountId.Value }));
}
[HttpPost("Sessions/Delete")]
[ProducesResponseType((int)HttpStatusCode.OK)]
public async Task<IActionResult> DeleteSession(DeleteSessionModel model)
{
var user = await _userContext.GetUserInfo();
if (user?.AccountId == null) return Unauthorized();
await _mediator.Send(new DeleteSessionCommand
{
AccountId = user.AccountId.Value,
SessionId = model.SessionId,
});
return Ok();
}
[HttpPost("Sessions/DeleteCurrent")]
public async Task<IActionResult> DeleteCurrentSession()
{
var user = await _userContext.GetUserInfo();
if (user?.AccountId == null) return Ok(new List<Session>());
return Ok(await _mediator.Send(new DeleteCurrentSessionCommand()));
}
[HttpPost("Sessions/DeleteAll")]
public async Task<IActionResult> DeleteAllSessions()
{
var user = await _userContext.GetUserInfo();
if (user?.AccountId == null) return Ok(new List<Session>());
return Ok(await _mediator.Send(new DeleteAllSessionsCommand
{
AccountId = user.AccountId.Value,
}));
}
[HttpGet("User")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(UserInfo))]
public async Task<IActionResult> GetUser()
{
var user = await _userContext.GetUserInfo();
return Ok(user == null ? null : new UserInfo
{
Id = user.Id,
AccountId = user.AccountId,
SessionId = user.SessionId,
NickName = user.NickName,
FirstName = user.FirstName,
Patronymic = user.Patronymic,
LastName = user.LastName,
AvatarId = user.AvatarId,
LanguageId = user.LanguageId,
Email = user.Email,
IsAuthenticated = user.IsAuthenticated,
});
}
[HttpPost("Login")]
[AllowAnonymous]
public async Task<TokensModel> Login(LoginModel model)
{
var tokens = await _mediator.Send(new LoginCommand
{
Email = model.Login,
Password = model.Password,
IsAdmin = false,
Ip = model.Ip,
});
return new TokensModel
{
AccessToken = tokens.AccessToken,
RefreshToken = tokens.RefreshToken,
};
}
[HttpPost("Refresh")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(TokensModel))]
[AllowAnonymous]
public async Task<IActionResult> Refresh(RefreshModel model)
{
var tokens = await _mediator.Send(new RefreshTokensCommand
{
RefreshToken = model.RefreshToken,
Ip = model.Ip,
});
return tokens == null ? Unauthorized() : Ok(new TokensModel
{
AccessToken = tokens.Value.AccessToken,
RefreshToken = tokens.Value.RefreshToken,
});
}
[HttpPost("Register")]
[AllowAnonymous]
public async Task Register(RegisterModel model)
{
var token = await _mediator.Send(new CreateUserCommand
{
Nickname = model.Nickname,
Email = model.Login,
Password = model.Password,
//IsAdmin = false,
});
}
}