registration returns token now
This commit is contained in:
parent
55e82425a9
commit
4bfc10d7de
5 changed files with 14 additions and 6 deletions
|
|
@ -26,7 +26,7 @@ public class AuthController : ControllerBase
|
|||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
return Ok();
|
||||
var token = await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
return Ok(new { token = token });
|
||||
}
|
||||
}
|
||||
|
|
@ -5,5 +5,5 @@ namespace SurveyBackend.Core.Services;
|
|||
public interface IAuthorizationService
|
||||
{
|
||||
public Task<string> LogInUser(string email, string password);
|
||||
public Task RegisterUser(User user);
|
||||
public Task<string> RegisterUser(User user);
|
||||
}
|
||||
|
|
@ -5,5 +5,6 @@ namespace SurveyBackend.Core.Services;
|
|||
public interface IUserService
|
||||
{
|
||||
public Task<User> GetUserByEmail(string email);
|
||||
public Task<bool> IsEmailTaken(string email);
|
||||
public Task CreateUserAsync(User user);
|
||||
}
|
||||
|
|
@ -28,10 +28,10 @@ public class AuthorizationService : IAuthorizationService
|
|||
return token;
|
||||
}
|
||||
|
||||
public async Task RegisterUser(User user)
|
||||
public async Task<string> RegisterUser(User user)
|
||||
{
|
||||
var existingUser = await _userService.GetUserByEmail(user.Email);
|
||||
if (existingUser is not null)
|
||||
var isEmailTaken = await _userService.IsEmailTaken(user.Email);
|
||||
if (isEmailTaken)
|
||||
{
|
||||
throw new ConflictException("Email already exists");
|
||||
}
|
||||
|
|
@ -39,5 +39,7 @@ public class AuthorizationService : IAuthorizationService
|
|||
user.Password = _passwordHasher.HashPassword(user.Password);
|
||||
|
||||
await _userService.CreateUserAsync(user);
|
||||
var token = TokenHelper.GetAuthToken(user);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,11 @@ public class UserService : IUserService
|
|||
return await _userRepository.GetUserByEmail(email) ?? throw new NotFoundException("Email not found");
|
||||
}
|
||||
|
||||
public async Task<bool> IsEmailTaken(string email)
|
||||
{
|
||||
return await _userRepository.GetUserByEmail(email) != null;
|
||||
}
|
||||
|
||||
public async Task CreateUserAsync(User user)
|
||||
{
|
||||
await _userRepository.AddAsync(user);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue