mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
AuthController work
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using Govor.Core;
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Core.Services;
|
||||
|
||||
namespace Govor.API.Services.Authentication;
|
||||
@@ -7,27 +10,56 @@ namespace Govor.API.Services.Authentication;
|
||||
public class AuthService : IAccountService
|
||||
{
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
private readonly IJwtService _jwtService;
|
||||
private readonly IUsersRepository _usersRepository;
|
||||
|
||||
public AuthService(IUsersRepository usersRepository, IPasswordHasher passwordHasher)
|
||||
|
||||
public AuthService(IUsersRepository usersRepository, IJwtService jwtService, IPasswordHasher passwordHasher)
|
||||
{
|
||||
_usersRepository = usersRepository;
|
||||
_jwtService = jwtService;
|
||||
_passwordHasher = passwordHasher;
|
||||
}
|
||||
public Task RegistrationAsync(string name, string password)
|
||||
|
||||
public async Task<string> RegistrationAsync(string name, string password)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (await _usersRepository.ExistsUsernameAsync(name))
|
||||
throw new UserAlreadyExistException(name);
|
||||
|
||||
var passwordHash = _passwordHasher.Hash(password);
|
||||
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = name,
|
||||
Description = string.Empty,
|
||||
PasswordHash = passwordHash,
|
||||
CreatedOn = DateOnly.FromDateTime(DateTime.Now),
|
||||
IconId = Guid.NewGuid(),
|
||||
WasOnline = DateTime.UtcNow
|
||||
//Role = role == "Admin" ? "Admin" : "User" // Ограничение ролей
|
||||
};
|
||||
|
||||
//await _usersRepository.AddAsync(user);
|
||||
|
||||
return _jwtService.GenerateJwtToken(user);
|
||||
}
|
||||
|
||||
public async Task<string> LoginAsync(string name, string password)
|
||||
{
|
||||
if (await _usersRepository.ExistsUsernameAsync(name) == false)
|
||||
throw new UserNotRegisteredException(name);
|
||||
|
||||
throw new NotImplementedException();
|
||||
|
||||
var user = await _usersRepository.FindByUsernameAsync(name);
|
||||
|
||||
if (_passwordHasher.Verify(password, user.PasswordHash) == false)
|
||||
throw new LoginUserException();
|
||||
|
||||
return _jwtService.GenerateJwtToken(user);
|
||||
}
|
||||
}
|
||||
|
||||
public class UserAlreadyExistException(string username) : Exception($"{username} is already exists!") { }
|
||||
public class LoginUserException : GovorCoreException { }
|
||||
|
||||
public class UserNotRegisteredException(string username) : Exception($"{username} is not registered!") { }
|
||||
public class UserAlreadyExistException(string username) : GovorCoreException($"{username} is already exists!") { }
|
||||
|
||||
public class UserNotRegisteredException(string username) : GovorCoreException($"{username} is not registered!") { }
|
||||
Reference in New Issue
Block a user