protected override Task <ConfirmEmailResponseDto> ExecuteAsync(ConfirmEmailRequestDto request, RequestContext context) { // Get secret var secret = _uniwikiContext .EmailConfirmationSecrets .Include(s => s.Profile) .First(s => s.Secret == request.Secret); // If email was already confirmed, then return ok response, but dont issue login token if (secret.Profile.IsConfirmed) { var user = _uniwikiContext.Profiles.Where(p => p.Id == secret.ProfileId).ToAuthorizedUserDto(); return(Task.FromResult(new ConfirmEmailResponseDto(user, null))); } // If the secret was invalidated, then return error if (!secret.IsValid) { throw new RequestException(_textService.Error_EmailConfirmationFailed); } // Confirm the email _emailConfirmationSecretRepository.ConfirmEmail(secret); // Invalidate all secrets _emailConfirmationSecretRepository.InvalidateSecrets(secret.Profile.Id); // Issue login token var token = _loginService.LoginUser(secret.ProfileId); // Get the authorized user var authorizedUser = _uniwikiContext.Profiles.Where(p => p.Id == secret.ProfileId).ToAuthorizedUserDto(); // Create DTO from the token var tokenDto = token.ToLoginTokenDto().Single(); // Create the response var result = new ConfirmEmailResponseDto(authorizedUser, tokenDto); return(Task.FromResult(result)); }
/// <summary> /// Generates, sends and saves the confirmation email for the specified user. /// </summary> public async Task SendConfirmationEmail(Guid profileId, string profileEmail) { // Try to get an existing secret var currentSecret = _emailConfirmationSecretRepository.TryGetValidEmailConfirmationSecret(profileId); // if current secret exists and its not expired if (currentSecret != null && currentSecret.CreationTime.Add(Constants.ResendRegistrationEmailMinTime) > _timeService.Now) { throw new RequestException(_textService.Error_EmailHasBeenAlreadySent); } // Invalidate all old secret(s) _emailConfirmationSecretRepository.InvalidateSecrets(profileId); // Add it to the DB var emailConfirmationSecret = _emailConfirmationSecretRepository.AddEmailConfirmationSecret(profileId, Guid.NewGuid(), _timeService.Now); // Send the message to email await _emailService.SendRegisterEmail(profileEmail, emailConfirmationSecret.Secret); // Save the email secret to the DB. We have to do it after the email was sent - if it will fail it throws exception and the secret will not be saved. _emailConfirmationSecretRepository.SaveEmailConfirmationSecret(emailConfirmationSecret); }