mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Registration rework
+ InvitesRepository + AdminsRepository + rework jwt
This commit is contained in:
@@ -4,6 +4,8 @@ using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.API.Services;
|
||||
using Govor.Core.Repositories.Admins;
|
||||
using Govor.Core.Repositories.Invaites;
|
||||
|
||||
|
||||
namespace Govor.API.Services.Authentication;
|
||||
@@ -13,38 +15,64 @@ public class AuthService : IAccountService
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IUsersRepository _usersRepository;
|
||||
private readonly IInvitesRepository _invitesRepository;
|
||||
private readonly IAdminsRepository _adminsRepository;
|
||||
|
||||
public AuthService(IUsersRepository usersRepository, IJwtService jwtService, IPasswordHasher passwordHasher)
|
||||
public AuthService(IUsersRepository usersRepository,
|
||||
IJwtService jwtService,
|
||||
IPasswordHasher passwordHasher,
|
||||
IInvitesRepository invitesRepository,
|
||||
IAdminsRepository adminsRepository)
|
||||
{
|
||||
_usersRepository = usersRepository;
|
||||
_jwtService = jwtService;
|
||||
_passwordHasher = passwordHasher;
|
||||
_invitesRepository = invitesRepository;
|
||||
_adminsRepository = adminsRepository;
|
||||
}
|
||||
|
||||
public async Task<string> RegistrationAsync(string name, string password)
|
||||
public async Task<string> RegistrationAsync(string name, string password, string inviteCode)
|
||||
{
|
||||
// 1. Проверка существования имени
|
||||
if (await _usersRepository.ExistsUsernameAsync(name))
|
||||
throw new UserAlreadyExistException(name);
|
||||
|
||||
|
||||
// 2. Проверка валидности инвайта
|
||||
var invite = await _invitesRepository.GetByCodeAsync(inviteCode);
|
||||
|
||||
// 3. Генерация пароля
|
||||
var passwordHash = _passwordHasher.Hash(password);
|
||||
|
||||
|
||||
// 4. Создание пользователя
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = name,
|
||||
Description = string.Empty,
|
||||
PasswordHash = passwordHash,
|
||||
CreatedOn = DateOnly.FromDateTime(DateTime.Now),
|
||||
Description = string.Empty,
|
||||
CreatedOn = DateOnly.FromDateTime(DateTime.UtcNow),
|
||||
IconId = Guid.NewGuid(),
|
||||
WasOnline = DateTime.UtcNow
|
||||
//Role = role == "Admin" ? "Admin" : "User" // Ограничение ролей
|
||||
WasOnline = DateTime.UtcNow,
|
||||
InviteId = invite.Id
|
||||
};
|
||||
|
||||
|
||||
// 5. Добавление пользователя
|
||||
await _usersRepository.AddAsync(user);
|
||||
|
||||
|
||||
// 6. Назначение роли, если инвайт — админский
|
||||
if (invite.IsAdmin)
|
||||
{
|
||||
await _adminsRepository.AddAsync(new Admin
|
||||
{
|
||||
UserId = user.Id
|
||||
});
|
||||
}
|
||||
|
||||
// 7. Генерация токена
|
||||
return _jwtService.GenerateJwtToken(user);
|
||||
}
|
||||
|
||||
|
||||
public async Task<string> LoginAsync(string name, string password)
|
||||
{
|
||||
if (await _usersRepository.ExistsUsernameAsync(name) == false)
|
||||
|
||||
Reference in New Issue
Block a user