Compare commits

...

2 Commits

4 changed files with 27 additions and 4 deletions
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using URL_Shortener.DTO; using URL_Shortener.DTO;
using URL_Shortener.Models; using URL_Shortener.Models;
using HashidsNet; using HashidsNet;
using Microsoft.AspNetCore.Authorization;
namespace URL_Shortener.Controllers namespace URL_Shortener.Controllers
{ {
@@ -78,12 +79,15 @@ namespace URL_Shortener.Controllers
} }
[HttpDelete("{id:int}")] [HttpDelete("{id:int}")]
[Authorize]
public async Task<IActionResult> DeleteShortUrlByIdAsync(int id) public async Task<IActionResult> DeleteShortUrlByIdAsync(int id)
{ {
var shortUrl = await _db.ShortUrls.FindAsync(id); var shortUrl = await _db.ShortUrls.FindAsync(id);
if (shortUrl == null) if (shortUrl == null)
return NotFound(); return NotFound();
// TODO: определение пользователя по jwt
_db.ShortUrls.Remove(shortUrl); _db.ShortUrls.Remove(shortUrl);
var clickInfos = _db.ClickInfos.Where(c => c.ShortUrlId == id); var clickInfos = _db.ClickInfos.Where(c => c.ShortUrlId == id);
+2 -2
View File
@@ -13,8 +13,8 @@ namespace URL_Shortener.Controllers
[HttpPost("register")] [HttpPost("register")]
public async Task<IActionResult> RegisterAsync(RegisterUserDTO dto) public async Task<IActionResult> RegisterAsync(RegisterUserDTO dto)
{ {
await _userService.RegisterAsync(dto.Name, dto.Email, dto.Password); var jwt = await _userService.RegisterAsync(dto.Name, dto.Email, dto.Password);
return Ok(); return Ok(jwt);
} }
[HttpPost("login")] [HttpPost("login")]
+18 -1
View File
@@ -1,7 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using URL_Shortener; using URL_Shortener;
using URL_Shortener.Services; using URL_Shortener.Services;
using URL_Shortener.Settings; using URL_Shortener.Settings;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -18,7 +21,20 @@ builder.Services.AddSingleton<HashidsNet.Hashids>();
builder.Services.AddScoped<UserService>(); builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<JwtService>(); builder.Services.AddScoped<JwtService>();
builder.Services.Configure<AuthSettings>(builder.Configuration.GetSection("AuthSettings"));
var authSettings = builder.Configuration.GetSection("AuthSettings");
builder.Services.Configure<AuthSettings>(authSettings);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o => o.TokenValidationParameters = new()
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
authSettings.Get<AuthSettings>().SecretKey))
});
builder.Services.AddDbContext<AppDbContext>(options => builder.Services.AddDbContext<AppDbContext>(options =>
{ {
@@ -47,6 +63,7 @@ using (var scope = app.Services.CreateScope())
//app.UseHttpsRedirection(); //app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
+3 -1
View File
@@ -9,7 +9,7 @@ namespace URL_Shortener.Services
private readonly AppDbContext _db = db; private readonly AppDbContext _db = db;
private readonly JwtService _jwtService = jwtService; private readonly JwtService _jwtService = jwtService;
public async Task RegisterAsync(string userName, string email, string password, CancellationToken cancellationToken = default) public async Task<string> RegisterAsync(string userName, string email, string password, CancellationToken cancellationToken = default)
{ {
if (await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) != null) if (await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) != null)
throw new Exception("This user already exists."); throw new Exception("This user already exists.");
@@ -23,6 +23,8 @@ namespace URL_Shortener.Services
await _db.Users.AddAsync(user, cancellationToken); await _db.Users.AddAsync(user, cancellationToken);
await _db.SaveChangesAsync(cancellationToken); await _db.SaveChangesAsync(cancellationToken);
return _jwtService.GenerateToken(user);
} }
public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default) public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default)