mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
76094af15b
+ tests + new services + InvitationDto and IInvitationReqest
84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using Govor.API.Services.Authentication.Interfaces;
|
|
using Govor.Core;
|
|
using Govor.Core.Infrastructure.Extensions;
|
|
using Govor.Core.Models;
|
|
using Govor.Core.Repositories.Users;
|
|
using Govor.API.Services;
|
|
using Govor.API.Services.AdminsStuff.Interfaces;
|
|
using Govor.Core.Repositories.Admins;
|
|
using Govor.Core.Repositories.Invaites;
|
|
|
|
|
|
namespace Govor.API.Services.Authentication;
|
|
|
|
public class AuthService : IAccountService
|
|
{
|
|
private readonly IPasswordHasher _passwordHasher;
|
|
private readonly IJwtService _jwtService;
|
|
private readonly IUsersRepository _usersRepository;
|
|
private readonly IAdminsRepository _adminsRepository;
|
|
|
|
public AuthService(IUsersRepository usersRepository,
|
|
IJwtService jwtService,
|
|
IPasswordHasher passwordHasher,
|
|
IAdminsRepository adminsRepository
|
|
)
|
|
{
|
|
_usersRepository = usersRepository;
|
|
_jwtService = jwtService;
|
|
_passwordHasher = passwordHasher;
|
|
_adminsRepository = adminsRepository;
|
|
}
|
|
|
|
public async Task<string> RegistrationAsync(string name, string password, Invitation invitation)
|
|
{
|
|
if (await _usersRepository.ExistsUsernameAsync(name))
|
|
throw new UserAlreadyExistException(name);
|
|
|
|
var passwordHash = _passwordHasher.Hash(password);
|
|
|
|
var user = new User
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Username = name,
|
|
PasswordHash = passwordHash,
|
|
Description = string.Empty,
|
|
CreatedOn = DateOnly.FromDateTime(DateTime.UtcNow),
|
|
IconId = Guid.NewGuid(),
|
|
WasOnline = DateTime.UtcNow,
|
|
InviteId = invitation.Id
|
|
};
|
|
|
|
await _usersRepository.AddAsync(user);
|
|
|
|
SetRole(user, invitation);
|
|
|
|
return _jwtService.GenerateJwtToken(user);
|
|
}
|
|
|
|
|
|
public async Task<string> LoginAsync(string name, string password)
|
|
{
|
|
if (await _usersRepository.ExistsUsernameAsync(name) == false)
|
|
throw new UserNotRegisteredException(name);
|
|
|
|
var user = await _usersRepository.FindByUsernameAsync(name);
|
|
|
|
if (_passwordHasher.Verify(password, user.PasswordHash) == false)
|
|
throw new LoginUserException();
|
|
|
|
return _jwtService.GenerateJwtToken(user);
|
|
}
|
|
|
|
private async void SetRole(User user, Invitation invitation)
|
|
{
|
|
if(invitation.IsAdmin)
|
|
await _adminsRepository.AddAsync(new Admin() { UserId = user.Id });
|
|
}
|
|
}
|
|
|
|
public class LoginUserException : GovorCoreException { }
|
|
|
|
public class UserAlreadyExistException(string username) : GovorCoreException($"{username} is already exists!") { }
|
|
|
|
public class UserNotRegisteredException(string username) : GovorCoreException($"{username} is not registered!") { } |