diff --git a/eventz-api/Eventz.Api/Controllers/AuthController.cs b/eventz-api/Eventz.Api/Controllers/AuthController.cs index 54b17ec..ec86d34 100644 --- a/eventz-api/Eventz.Api/Controllers/AuthController.cs +++ b/eventz-api/Eventz.Api/Controllers/AuthController.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Http; +using Eventz.Application.Features.Auth.Register; +using MediatR; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Eventz.Api.Controllers @@ -7,5 +9,19 @@ namespace Eventz.Api.Controllers [ApiController] public class AuthController : ControllerBase { + public IMediator _mediator; + public AuthController(IMediator mediator) { + _mediator = mediator; + } + [HttpPost] + public async Task Register (RegistrationCommand command) + { + var result = await _mediator.Send(command); + if (result.Errored) + { + return BadRequest(result); + } + return Ok(result); + } } } diff --git a/eventz-api/Eventz.Api/Program.cs b/eventz-api/Eventz.Api/Program.cs index 8cf15b5..0ced845 100644 --- a/eventz-api/Eventz.Api/Program.cs +++ b/eventz-api/Eventz.Api/Program.cs @@ -1,11 +1,11 @@ -using Microsoft.EntityFrameworkCore; -using MediatR; - -using System; using Eventz.Application.AssemblyMarker; -using Eventz.Infrastructure; using Eventz.Application.Interfaces; +using Eventz.Infrastructure; +using Eventz.Infrastructure.Repositories.Auth; using Eventz.Infrastructure.Repositories.Categories; +using MediatR; +using Microsoft.EntityFrameworkCore; +using System; var builder = WebApplication.CreateBuilder(args); @@ -21,6 +21,7 @@ options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddScoped(); +builder.Services.AddScoped(); //MediatR builder.Services.AddMediatR(cfg => diff --git a/eventz-api/Eventz.Application/Eventz.Application.csproj b/eventz-api/Eventz.Application/Eventz.Application.csproj index 14defb5..bb2b3c6 100644 --- a/eventz-api/Eventz.Application/Eventz.Application.csproj +++ b/eventz-api/Eventz.Application/Eventz.Application.csproj @@ -10,6 +10,7 @@ + diff --git a/eventz-api/Eventz.Application/Features/Auth/Register/RegisterCommand.cs b/eventz-api/Eventz.Application/Features/Auth/Register/RegisterCommand.cs index bad744c..a0c9354 100644 --- a/eventz-api/Eventz.Application/Features/Auth/Register/RegisterCommand.cs +++ b/eventz-api/Eventz.Application/Features/Auth/Register/RegisterCommand.cs @@ -9,8 +9,5 @@ namespace Eventz.Application.Features.Auth.Register { - internal class RegisterCommand - { public record RegistrationCommand (string UserName, string Email, string Password) : IRequest>; - } } diff --git a/eventz-api/Eventz.Application/Features/Auth/Register/RegisterHandler.cs b/eventz-api/Eventz.Application/Features/Auth/Register/RegisterHandler.cs index 9483a1e..4192de3 100644 --- a/eventz-api/Eventz.Application/Features/Auth/Register/RegisterHandler.cs +++ b/eventz-api/Eventz.Application/Features/Auth/Register/RegisterHandler.cs @@ -1,4 +1,9 @@ -using MediatR; +using Eventz.Application.Common; +using Eventz.Application.Dtos; +using Eventz.Application.Interfaces; +using Eventz.Domain.Entitites; +using MediatR; +using Microsoft.AspNetCore.Identity; using System; using System.Collections.Generic; using System.Linq; @@ -7,8 +12,33 @@ namespace Eventz.Application.Features.Auth.Register { - internal class RegisterHandler + internal class RegisterHandler : IRequestHandler> { + private readonly IAuth _auth; + + public RegisterHandler(IAuth auth) + { + _auth = auth; + } + + public async Task> Handle (RegistrationCommand command, CancellationToken cancellationToken) { + var hasher = new PasswordHasher(); + var hashedPassword = hasher.HashPassword(null, command.Password); + var result = hasher.VerifyHashedPassword(null, hashedPassword, command.Password); + + if (result != PasswordVerificationResult.Success) + { + return ApiResponse.Failure("Password Mismatch"); + } + var newUser = new RegisterDto + { + UserName = command.UserName, + Email = command.Email, + Password = hashedPassword + }; + await _auth.RegisterUser (newUser); + return ApiResponse.Success(newUser); + } } } diff --git a/eventz-api/Eventz.Domain/Entitites/User.cs b/eventz-api/Eventz.Domain/Entitites/User.cs index 39f20f1..d15b033 100644 --- a/eventz-api/Eventz.Domain/Entitites/User.cs +++ b/eventz-api/Eventz.Domain/Entitites/User.cs @@ -8,7 +8,7 @@ namespace Eventz.Domain.Entitites { public class User { - public string Id { get; set; } + public int Id { get; set; } public Guid UserToken { get; set; } = new Guid(); public string UserName { get; set; } public string Email { get; set; } diff --git a/eventz-api/Eventz.Infrastructure/AppDbContext.cs b/eventz-api/Eventz.Infrastructure/AppDbContext.cs index 6bac19e..fdc99bf 100644 --- a/eventz-api/Eventz.Infrastructure/AppDbContext.cs +++ b/eventz-api/Eventz.Infrastructure/AppDbContext.cs @@ -14,5 +14,7 @@ public AppDbContext(DbContextOptions options) : base(options) public DbSet Categories => Set(); public DbSet Users => Set(); + + //Add Configs } } diff --git a/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs b/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs index 057ce0e..7565b8c 100644 --- a/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs +++ b/eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs @@ -23,7 +23,7 @@ public async Task RegisterUser (RegisterDto registerDto) var user = new User{ UserName = registerDto.UserName, Email = registerDto.Email, - Password = registerDto.Password, //TODO: Hash password + Password = registerDto.Password, CreatedAt = DateTime.Now }; await _context.Users.AddAsync(user);