Skip to content

Commit be3845c

Browse files
committed
feat: add registration logic
1 parent d9f13f2 commit be3845c

8 files changed

Lines changed: 60 additions & 13 deletions

File tree

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.AspNetCore.Http;
1+
using Eventz.Application.Features.Auth.Register;
2+
using MediatR;
3+
using Microsoft.AspNetCore.Http;
24
using Microsoft.AspNetCore.Mvc;
35

46
namespace Eventz.Api.Controllers
@@ -7,5 +9,19 @@ namespace Eventz.Api.Controllers
79
[ApiController]
810
public class AuthController : ControllerBase
911
{
12+
public IMediator _mediator;
13+
public AuthController(IMediator mediator) {
14+
_mediator = mediator;
15+
}
16+
[HttpPost]
17+
public async Task<IActionResult> Register (RegistrationCommand command)
18+
{
19+
var result = await _mediator.Send(command);
20+
if (result.Errored)
21+
{
22+
return BadRequest(result);
23+
}
24+
return Ok(result);
25+
}
1026
}
1127
}

eventz-api/Eventz.Api/Program.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using MediatR;
3-
4-
using System;
51
using Eventz.Application.AssemblyMarker;
6-
using Eventz.Infrastructure;
72
using Eventz.Application.Interfaces;
3+
using Eventz.Infrastructure;
4+
using Eventz.Infrastructure.Repositories.Auth;
85
using Eventz.Infrastructure.Repositories.Categories;
6+
using MediatR;
7+
using Microsoft.EntityFrameworkCore;
8+
using System;
99

1010
var builder = WebApplication.CreateBuilder(args);
1111

@@ -21,6 +21,7 @@
2121
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
2222

2323
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
24+
builder.Services.AddScoped<IAuth, AuthRepository>();
2425

2526
//MediatR
2627
builder.Services.AddMediatR(cfg =>

eventz-api/Eventz.Application/Eventz.Application.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="AutoMapper" Version="15.0.1" />
1111
<PackageReference Include="FluentValidation" Version="12.0.0" />
1212
<PackageReference Include="MediatR" Version="13.0.0" />
13+
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.3.1" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

eventz-api/Eventz.Application/Features/Auth/Register/RegisterCommand.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99

1010
namespace Eventz.Application.Features.Auth.Register
1111
{
12-
internal class RegisterCommand
13-
{
1412
public record RegistrationCommand (string UserName, string Email, string Password) : IRequest<ApiResponse<RegisterDto>>;
15-
}
1613
}
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using MediatR;
1+
using Eventz.Application.Common;
2+
using Eventz.Application.Dtos;
3+
using Eventz.Application.Interfaces;
4+
using Eventz.Domain.Entitites;
5+
using MediatR;
6+
using Microsoft.AspNetCore.Identity;
27
using System;
38
using System.Collections.Generic;
49
using System.Linq;
@@ -7,8 +12,33 @@
712

813
namespace Eventz.Application.Features.Auth.Register
914
{
10-
internal class RegisterHandler
15+
internal class RegisterHandler : IRequestHandler<RegistrationCommand, ApiResponse<RegisterDto>>
1116
{
17+
private readonly IAuth _auth;
18+
19+
public RegisterHandler(IAuth auth)
20+
{
21+
_auth = auth;
22+
}
23+
24+
public async Task<ApiResponse<RegisterDto>> Handle (RegistrationCommand command, CancellationToken cancellationToken) {
25+
var hasher = new PasswordHasher<object>();
26+
var hashedPassword = hasher.HashPassword(null, command.Password);
27+
var result = hasher.VerifyHashedPassword(null, hashedPassword, command.Password);
28+
29+
if (result != PasswordVerificationResult.Success)
30+
{
31+
return ApiResponse<RegisterDto>.Failure("Password Mismatch");
32+
}
33+
var newUser = new RegisterDto
34+
{
35+
UserName = command.UserName,
36+
Email = command.Email,
37+
Password = hashedPassword
38+
};
39+
await _auth.RegisterUser (newUser);
40+
return ApiResponse<RegisterDto>.Success(newUser);
41+
}
1242

1343
}
1444
}

eventz-api/Eventz.Domain/Entitites/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Eventz.Domain.Entitites
88
{
99
public class User
1010
{
11-
public string Id { get; set; }
11+
public int Id { get; set; }
1212
public Guid UserToken { get; set; } = new Guid();
1313
public string UserName { get; set; }
1414
public string Email { get; set; }

eventz-api/Eventz.Infrastructure/AppDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
1414
public DbSet<Category> Categories => Set<Category>();
1515

1616
public DbSet<User> Users => Set<User>();
17+
18+
//Add Configs
1719
}
1820
}

eventz-api/Eventz.Infrastructure/Repositories/Auth/AuthRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task<RegisterDto> RegisterUser (RegisterDto registerDto)
2323
var user = new User{
2424
UserName = registerDto.UserName,
2525
Email = registerDto.Email,
26-
Password = registerDto.Password, //TODO: Hash password
26+
Password = registerDto.Password,
2727
CreatedAt = DateTime.Now
2828
};
2929
await _context.Users.AddAsync(user);

0 commit comments

Comments
 (0)