Separated app into three layers, cleaned up leftovers
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
using Birdmap.Models;
|
||||
using Birdmap.Services.Interfaces;
|
||||
using AutoMapper;
|
||||
using Birdmap.API.DTOs;
|
||||
using Birdmap.BLL.Interfaces;
|
||||
using Birdmap.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -20,11 +22,13 @@ namespace Birdmap.Controllers
|
||||
{
|
||||
private readonly IAuthService _service;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public AuthController(IAuthService service, IConfiguration configuration)
|
||||
public AuthController(IAuthService service, IConfiguration configuration, IMapper mapper)
|
||||
{
|
||||
_service = service;
|
||||
_configuration = configuration;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
@ -35,12 +39,13 @@ namespace Birdmap.Controllers
|
||||
var user = await _service.AuthenticateUserAsync(model.Username, model.Password);
|
||||
var expiresInSeconds = TimeSpan.FromHours(2).TotalSeconds;
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_configuration["BasicAuth:Secret"]);
|
||||
var key = Encoding.ASCII.GetBytes(_configuration["Secret"]);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new Claim[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, user.Name)
|
||||
new Claim(ClaimTypes.Name, user.Name),
|
||||
new Claim(ClaimTypes.Role, user.Role.ToString()),
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddHours(expiresInSeconds),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
@ -48,14 +53,12 @@ namespace Birdmap.Controllers
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
var tokenString = tokenHandler.WriteToken(token);
|
||||
|
||||
return Ok(
|
||||
new
|
||||
{
|
||||
user_name = user.Name,
|
||||
access_token = tokenString,
|
||||
token_type = "Bearer",
|
||||
expires_in = expiresInSeconds,
|
||||
});
|
||||
var response = _mapper.Map<AuthenticateResponse>(user);
|
||||
response.AccessToken = tokenString;
|
||||
response.TokenType = "Bearer";
|
||||
response.ExpiresIn = expiresInSeconds;
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Birdmap.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
var rng = new Random();
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateTime.Now.AddDays(index),
|
||||
TemperatureC = rng.Next(-20, 55),
|
||||
Summary = Summaries[rng.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user