registration and authorization
This commit is contained in:
parent
2b5f468b84
commit
4423dc360f
12 changed files with 136 additions and 6 deletions
|
|
@ -1,5 +1,7 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.Infrastructure.Services;
|
||||
using SurveyBackend.Mappers.UserDTOs;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
|
|
@ -7,9 +9,32 @@ namespace SurveyBackend.Controllers;
|
|||
[Route("auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> GetToken([FromBody] UserLoginDto loginData)
|
||||
private readonly AuthorizationService _authorizationService;
|
||||
|
||||
public AuthController(AuthorizationService authorizationService)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
||||
{
|
||||
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
|
||||
return token is null ? Unauthorized() : Ok(new { token = token });
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.Infrastructure.Services;
|
||||
|
||||
namespace SurveyBackend.Mappers.UserDTOs;
|
||||
|
||||
public static class UserRegistrationMapper
|
||||
{
|
||||
public static User UserRegistrationToModel(UserRegistrationDto dto) => new User
|
||||
{
|
||||
Email = dto.Email,
|
||||
FirstName = dto.FirstName,
|
||||
LastName = dto.LastName,
|
||||
Password = dto.Password,
|
||||
};
|
||||
}
|
||||
|
|
@ -2,8 +2,12 @@ using System.Text;
|
|||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SurveyBackend.Core.Repositories;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.Infrastructure;
|
||||
using SurveyBackend.Infrastructure.Data;
|
||||
using SurveyBackend.Infrastructure.Repositories;
|
||||
using SurveyBackend.Infrastructure.Services;
|
||||
using SurveyLib.Core.Repositories;
|
||||
using SurveyLib.Core.Services;
|
||||
using SurveyLib.Infrastructure.EFCore.Data;
|
||||
|
|
@ -27,6 +31,13 @@ public class Program
|
|||
|
||||
builder.Services.AddScoped<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
|
||||
|
||||
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
|
||||
builder.Services.AddScoped<IPasswordHasher, Sha256PasswordHasher>();
|
||||
|
||||
builder.Services.AddScoped<AuthorizationService>();
|
||||
|
||||
builder.Services.AddScoped<ISurveyRepository, SurveyRepository>();
|
||||
builder.Services.AddScoped<ISurveyService, SurveyService>();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace SurveyBackend.Core.Models;
|
|||
|
||||
public class User
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public int Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
|
|
|
|||
|
|
@ -4,4 +4,5 @@ namespace SurveyBackend.Core.Repositories;
|
|||
|
||||
public interface IUserRepository : IGenericRepository<User>
|
||||
{
|
||||
public Task<User?> GetUserByEmail(string email);
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
namespace SurveyBackend.Infrastructure.Services;
|
||||
namespace SurveyBackend.Core.Services;
|
||||
|
||||
public interface IPasswordHasher
|
||||
{
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
|
||||
namespace SurveyBackend.Core.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
|
||||
public Task<User?> GetUserByEmail(string email);
|
||||
public Task CreateUserAsync(User user);
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ public class ApplicationDbContext : SurveyDbContext
|
|||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
|
|
|
|||
|
|
@ -41,4 +41,9 @@ public class UserRepository : IUserRepository
|
|||
_context.Users.Remove(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByEmail(string email)
|
||||
{
|
||||
return await _context.Users.FirstOrDefaultAsync(u => u.Email == email);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.Infrastructure.Helpers;
|
||||
|
||||
namespace SurveyBackend.Infrastructure.Services;
|
||||
|
||||
public class AuthorizationService
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
|
||||
public AuthorizationService(IUserService userService, IPasswordHasher passwordHasher)
|
||||
{
|
||||
_userService = userService;
|
||||
_passwordHasher = passwordHasher;
|
||||
}
|
||||
|
||||
public async Task<string?> LogInUser(string email, string password)
|
||||
{
|
||||
var user = await _userService.GetUserByEmail(email);
|
||||
if (user is null || !_passwordHasher.Verify(password, user.Password))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var token = TokenHelper.GetAuthToken(user);
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task RegisterUser(User user)
|
||||
{
|
||||
var existingUser = await _userService.GetUserByEmail(user.Email);
|
||||
if (existingUser is not null)
|
||||
{
|
||||
throw new Exception("Email already exists");
|
||||
}
|
||||
|
||||
user.Password = _passwordHasher.HashPassword(user.Password);
|
||||
|
||||
await _userService.CreateUserAsync(user);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Security.Cryptography;
|
||||
using SurveyBackend.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Infrastructure.Services;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
using SurveyBackend.Core.Repositories;
|
||||
using SurveyBackend.Core.Services;
|
||||
|
||||
namespace SurveyBackend.Infrastructure.Services;
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public UserService(IUserRepository userRepository)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByEmail(string email)
|
||||
{
|
||||
return await _userRepository.GetUserByEmail(email);
|
||||
}
|
||||
|
||||
public async Task CreateUserAsync(User user)
|
||||
{
|
||||
await _userRepository.AddAsync(user);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue