public async Task <AccountDto> Login(LoginDto loginDto) { try { var user = await _userManager.FindByNameAsync(loginDto.Email); if (user == null) { Status.AddError("Unable to login as this account does not exist."); } if (Status.HasErrors) { return(null); } var userSignedIn = await _userManager.CheckPasswordAsync(user, loginDto.Password); if (userSignedIn) { var accountDto = await _retrieveAuthenticatedUserService.Get(user, _jwtFactory); return(accountDto); } Status.AddError("Username or password was incorrect."); return(null); } catch (Exception e) { Status.AddError(e.Message); await _errorFactory.LogError(e.Message, "Login", _context); return(null); } }
public async Task RegisterAccount(RegisterDto registerDto) { using (var scope = _context.Database.BeginTransaction()) { try { var user = _mapper.Map <RegisterDto, ApplicationUser>(registerDto); var userCreated = await _userManager.CreateAsync(user, registerDto.Password); if (userCreated.Succeeded) { var emailConfirmationToken = await _userManager.GenerateEmailConfirmationTokenAsync(user); emailConfirmationToken = HttpUtility.UrlEncode(emailConfirmationToken); var confirmationEmail = _confimationEmailService.ConfirmationEmail(registerDto.FirstName, user.Id, emailConfirmationToken); await _sendEmailService.SendAsync(user.Email, "Confirm your account", confirmationEmail, registerDto.FirstName); if (_sendEmailService.Status.HasErrors) { Status.CombineErrors(_sendEmailService.Status); } if (Status.HasErrors) { return; } await _context.SaveChangesAsync(); scope.Commit(); } foreach (var error in userCreated.Errors) { Status.AddError(error.Description); } } catch (Exception e) { Status.AddError(e.Message); scope.Rollback(); await _errorFactory.LogError(e.Message, "RegisterAccount", _context); } } }