Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
AspNetCoreApiStarter/src/Web.Api/Controllers/AuthController.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (44 sloc)
2.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Options; | |
using Web.Api.Core.Dto.UseCaseRequests; | |
using Web.Api.Core.Interfaces.UseCases; | |
using Web.Api.Models.Settings; | |
using Web.Api.Presenters; | |
namespace Web.Api.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class AuthController : ControllerBase | |
{ | |
private readonly ILoginUseCase _loginUseCase; | |
private readonly LoginPresenter _loginPresenter; | |
private readonly IExchangeRefreshTokenUseCase _exchangeRefreshTokenUseCase; | |
private readonly ExchangeRefreshTokenPresenter _exchangeRefreshTokenPresenter; | |
private readonly AuthSettings _authSettings; | |
public AuthController(ILoginUseCase loginUseCase, LoginPresenter loginPresenter, IExchangeRefreshTokenUseCase exchangeRefreshTokenUseCase, ExchangeRefreshTokenPresenter exchangeRefreshTokenPresenter, IOptions<AuthSettings> authSettings) | |
{ | |
_loginUseCase = loginUseCase; | |
_loginPresenter = loginPresenter; | |
_exchangeRefreshTokenUseCase = exchangeRefreshTokenUseCase; | |
_exchangeRefreshTokenPresenter = exchangeRefreshTokenPresenter; | |
_authSettings = authSettings.Value; | |
} | |
// POST api/auth/login | |
[HttpPost("login")] | |
public async Task<ActionResult> Login([FromBody] Models.Request.LoginRequest request) | |
{ | |
if (!ModelState.IsValid) { return BadRequest(ModelState); } | |
await _loginUseCase.Handle(new LoginRequest(request.UserName, request.Password, Request.HttpContext.Connection.RemoteIpAddress?.ToString()), _loginPresenter); | |
return _loginPresenter.ContentResult; | |
} | |
// POST api/auth/refreshtoken | |
[HttpPost("refreshtoken")] | |
public async Task<ActionResult> RefreshToken([FromBody] Models.Request.ExchangeRefreshTokenRequest request) | |
{ | |
if (!ModelState.IsValid) { return BadRequest(ModelState);} | |
await _exchangeRefreshTokenUseCase.Handle(new ExchangeRefreshTokenRequest(request.AccessToken, request.RefreshToken, _authSettings.SecretKey), _exchangeRefreshTokenPresenter); | |
return _exchangeRefreshTokenPresenter.ContentResult; | |
} | |
} | |
} |