public async Task <IActionResult> Confirm( CancellationToken cancellationToken, [FromBody] RegistrationConfirmationBinding binding, [FromServices] IUserRepository userRepository, [FromServices] IConfirmationCodeProvider confirmationCodeProvider) { if (!confirmationCodeProvider.Validate(binding.ConfirmationCode, out var email)) { return(UnprocessableEntity()); } var user = await userRepository.FindByEmail(email, cancellationToken); if (user == null) { return(NotFound()); } try { user.Confirm(); await userRepository.Save(user); } catch (UserEmailAlreadyConfirmedException) { return(NoContent()); } return(NoContent()); }
public async Task <User> Confirm(String confirmationCode, CancellationToken cancellationToken) { if (!_confirmationCodeProvider.Validate(confirmationCode, out var email, out var userId)) { throw new InvalidTokenException(); } var user = await _repository.Get(userId, cancellationToken); if (email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase)) { user.Confirm(); return(user); } throw new InvalidTokenException(); }