Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion eventz-api/Eventz.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<IActionResult> Register (RegistrationCommand command)
{
var result = await _mediator.Send(command);
if (result.Errored)
{
return BadRequest(result);
}
return Ok(result);
}
}
}
11 changes: 6 additions & 5 deletions eventz-api/Eventz.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -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);

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

builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<IAuth, AuthRepository>();

//MediatR
builder.Services.AddMediatR(cfg =>
Expand Down
1 change: 1 addition & 0 deletions eventz-api/Eventz.Application/Eventz.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="FluentValidation" Version="12.0.0" />
<PackageReference Include="MediatR" Version="13.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@

namespace Eventz.Application.Features.Auth.Register
{
internal class RegisterCommand
{
public record RegistrationCommand (string UserName, string Email, string Password) : IRequest<ApiResponse<RegisterDto>>;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -7,8 +12,33 @@

namespace Eventz.Application.Features.Auth.Register
{
internal class RegisterHandler
internal class RegisterHandler : IRequestHandler<RegistrationCommand, ApiResponse<RegisterDto>>
{
private readonly IAuth _auth;

public RegisterHandler(IAuth auth)
{
_auth = auth;
}

public async Task<ApiResponse<RegisterDto>> Handle (RegistrationCommand command, CancellationToken cancellationToken) {
var hasher = new PasswordHasher<object>();
var hashedPassword = hasher.HashPassword(null, command.Password);
var result = hasher.VerifyHashedPassword(null, hashedPassword, command.Password);

if (result != PasswordVerificationResult.Success)
{
return ApiResponse<RegisterDto>.Failure("Password Mismatch");
}
var newUser = new RegisterDto
{
UserName = command.UserName,
Email = command.Email,
Password = hashedPassword
};
await _auth.RegisterUser (newUser);
return ApiResponse<RegisterDto>.Success(newUser);
}

}
}
2 changes: 1 addition & 1 deletion eventz-api/Eventz.Domain/Entitites/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 2 additions & 0 deletions eventz-api/Eventz.Infrastructure/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
public DbSet<Category> Categories => Set<Category>();

public DbSet<User> Users => Set<User>();

//Add Configs
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task<RegisterDto> 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);
Expand Down