New way to interact with AspNet users

This commit is contained in:
Dominic Villemure
2024-04-09 23:38:26 -04:00
parent 47121b0539
commit 49a10198e4
13 changed files with 476 additions and 77 deletions

View File

@@ -1,21 +1,23 @@
using Hutopy.Domain.Interfaces;
using Hutopy.Domain.Models;
using Hutopy.Infrastructure.Identity;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Identity;
namespace Hutopy.Infrastructure.Services;
public class UserService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) : IUserService
public class UserService(UserManager<ApplicationUser> userManager) : IUserService
{
private readonly UserManager<ApplicationUser> _userManager = userManager;
private readonly SignInManager<ApplicationUser> _signInManager = signInManager;
public async Task CreateUserAsync(string email, string userName, string password)
public async Task CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
{
var applicationUser = new ApplicationUser
{
UserName = userName,
Email = email,
FirstName = firstName,
LastName = lastName
};
//todo: Need to handle errors better for the user.
@@ -26,5 +28,37 @@ public class UserService(UserManager<ApplicationUser> userManager, SignInManager
throw new InvalidOperationException(response.Errors.First().Description);
}
}
public async Task<UserModel?> FindUserByIdAsync(string id)
{
var response = await _userManager.FindByIdAsync(id);
if (response == null) return null;
var userModel = new UserModel()
{
Id = response.Id,
UserName = response.UserName,
Email = response.Email
};
return userModel;
}
public async Task<UserModel?> FindUserByEmailAsync(string email)
{
var response = await _userManager.FindByEmailAsync(email);
if (response == null) return null;
var userModel = new UserModel()
{
Id = response.Id,
UserName = response.UserName,
Email = response.Email
};
return userModel;
}
}