public async Task <IdentityResult> Handle(RegisterUserRequest request, CancellationToken cancellationToken) { try { _logger.LogInformation($"Starting {nameof(RegisterUserHandler)}"); var user = new Models.AppUser { UserName = request.Input.Email, Email = request.Input.Email, Name = request.Input.Name, }; var result = await _userManager.CreateAsync(user, request.Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); await _userManager.AddToRoleAsync(user, Roles.User); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = request.RequestUrlSchema + "://localhost:4200" + "/account/confirmEmail" + $"?code={code}&userId={user.Id}"; var title = "Confirm your email"; var body = new List <string> { "Please confirm your account by clicking the link below." }; var url = new UrlEmailTemplateViewModel("Confirm account", callbackUrl); var template = await _emailTemplateGenerator.RenderActionTemplate(title, body, url); await _emailDispatcher.SaveEmail(new Email { Body = template, Subject = title, To = request.Input.Email }); await _signInManager.SignInAsync(user, isPersistent : false); } _logger.LogInformation($"Finished {nameof(RegisterUserHandler)}"); return(result); } catch (Exception ex) { _logger.LogError(ex, ex.Message); throw; } }
public async Task Handle(ForgotPasswordEvent notification, CancellationToken cancellationToken) { try { _logger.LogInformation($"Starting {nameof(ForgotPasswordHandler)}"); var user = await _userManager.FindByEmailAsync(notification.Input.Email); if (user == null) { throw new NullReferenceException(); } if (!await _userManager.IsEmailConfirmedAsync(user)) { throw new InvalidOperationException("Please, Confirm your email first then try."); } var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = notification.RequestSchema + "://localhost:4200" + "/account/resetPassword" + $"?code={code}"; var title = "Reset Password"; var body = new List <string> { "Please reset your password by clicking the link below." }; var url = new UrlEmailTemplateViewModel("Reset your account", callbackUrl); var template = await _emailTemplateGenerator.RenderActionTemplate(title, body, url); await _emailDispatcher.SaveEmail(new Email { To = notification.Input.Email, Subject = title, Body = template }); _logger.LogInformation($"Finished {nameof(ForgotPasswordHandler)}"); } catch (Exception e) { _logger.LogError(e.Message, e); throw; } }
public async Task Handle(SendVerificationEmailEvent notification, CancellationToken cancellationToken) { try { _logger.LogInformation($"Starting {nameof(SendVerificationEmailHandler)}"); var user = await _userManager.FindByNameAsync(notification.UserName); if (user == null) { throw new NullReferenceException($"Unable to load user with UserName '{notification.UserName}'."); } var email = await _userManager.GetEmailAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = notification.RequestSchema + "://localhost:4200" + "/account/confirmEmail" + $"?code={code}&userId={user.Id}"; var title = "Confirm your email"; var body = new List <string> { "Please confirm your account by clicking the link below." }; var template = await _emailTemplateGenerator.RenderActionTemplate(title, body, new UrlEmailTemplateViewModel("Confirm account", callbackUrl)); await _emailDispatcher.SaveEmail(new Email { To = email, Subject = title, Body = template }); _logger.LogInformation($"Finished {nameof(SendVerificationEmailHandler)}"); } catch (Exception e) { _logger.LogError(e.Message, e); throw; } }