mirror of
https://github.com/ryzij/URL-Shortener.git
synced 2026-07-14 03:56:58 +00:00
Добавил регистрацию и вход
This commit is contained in:
@@ -1,24 +1,27 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using URL_Shortener.Models;
|
||||
using URL_Shortener.DTO;
|
||||
using URL_Shortener.Services;
|
||||
|
||||
namespace URL_Shortener.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("user")]
|
||||
public class UserController(AppDbContext db) : ControllerBase
|
||||
public class UserController(UserService userService) : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly UserService _userService = userService;
|
||||
|
||||
[HttpGet("email")]
|
||||
public async Task<IActionResult> GetUserByEmailAsync(string email)
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> RegisterAsync(RegisterUserDTO dto)
|
||||
{
|
||||
var user = await _db.Users.FirstOrDefaultAsync(u => u.Email == email);
|
||||
if (user == null)
|
||||
return NotFound("User not found");
|
||||
await _userService.RegisterAsync(dto.Name, dto.Email, dto.Password);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> LoginAsync(LoginUserDTO dto)
|
||||
{
|
||||
var jwt = await _userService.LoginAsync(dto.Email, dto.Password);
|
||||
return Ok(jwt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace URL_Shortener.DTO
|
||||
{
|
||||
public class LoginUserDTO
|
||||
{
|
||||
[Required, EmailAddress]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace URL_Shortener.DTO
|
||||
{
|
||||
public class RegisterUserDTO
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
[Required, EmailAddress]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using URL_Shortener;
|
||||
using URL_Shortener.Services;
|
||||
using URL_Shortener.Settings;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -12,9 +14,12 @@ builder.Services.AddOpenApi();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// Чтобы короткие коды генерировать
|
||||
builder.Services.AddSingleton<HashidsNet.Hashids>();
|
||||
|
||||
builder.Services.AddScoped<UserService>();
|
||||
builder.Services.AddScoped<JwtService>();
|
||||
builder.Services.Configure<AuthSettings>(builder.Configuration.GetSection("AuthSettings"));
|
||||
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
{
|
||||
var connString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using URL_Shortener.Models;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using URL_Shortener.Settings;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace URL_Shortener.Services
|
||||
{
|
||||
public class JwtService(IOptions<AuthSettings> options)
|
||||
{
|
||||
public string GenerateToken(User user)
|
||||
{
|
||||
var claims = new List<Claim>() {
|
||||
new("userName", user.Name),
|
||||
new("userEmail", user.Email),
|
||||
new("id", user.Id.ToString())
|
||||
};
|
||||
var jwt = new JwtSecurityToken(
|
||||
expires: DateTime.UtcNow.Add(options.Value.Expires),
|
||||
claims: claims,
|
||||
signingCredentials: new SigningCredentials(
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(options.Value.SecretKey)),
|
||||
SecurityAlgorithms.HmacSha256)
|
||||
);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(jwt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,40 @@
|
||||
namespace URL_Shortener.Services
|
||||
using URL_Shortener.Models;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace URL_Shortener.Services
|
||||
{
|
||||
public class UserService
|
||||
public class UserService(AppDbContext db, JwtService jwtService)
|
||||
{
|
||||
public async Task Register(string userName, string email, string hashedPassword)
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly JwtService _jwtService = jwtService;
|
||||
|
||||
public async Task RegisterAsync(string userName, string email, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) != null)
|
||||
throw new Exception("This user already exists.");
|
||||
|
||||
var user = new User()
|
||||
{
|
||||
Name = userName,
|
||||
Email = email
|
||||
};
|
||||
user.HashedPassword = new PasswordHasher<User>().HashPassword(user, password);
|
||||
|
||||
await _db.Users.AddAsync(user, cancellationToken);
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) ??
|
||||
throw new Exception("User not found.");
|
||||
|
||||
var res = new PasswordHasher<User>().VerifyHashedPassword(user, user.HashedPassword, password);
|
||||
if (res == PasswordVerificationResult.Failed)
|
||||
throw new Exception("Incorrect password.");
|
||||
|
||||
return _jwtService.GenerateToken(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace URL_Shortener.Settings
|
||||
{
|
||||
public class AuthSettings
|
||||
{
|
||||
public string SecretKey { get; set; } = string.Empty;
|
||||
public TimeSpan Expires { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,21 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hashids.net" Version="1.7.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7">
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.7">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.1" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.19.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -8,5 +8,9 @@
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Host=localhost;Database=appdb;Username=postgres;Password=app"
|
||||
},
|
||||
"AuthSettings": {
|
||||
"SecretKey": "mysecretkeyqwertyuiopasdfghjklzxcvbnm",
|
||||
"Expires": "00:01:00"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user