Пример #1
0
        public async Task <IActionResult> ChangePassword(AccountEditPasswordDto passwordDto)
        {
            var result = await _accountService.ChangePassword(passwordDto);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            return(Ok(result.Token));
        }
Пример #2
0
        public async Task <AccountResponse> ChangePassword(AccountEditPasswordDto passwordDto)
        {
            Dictionary <string, string[]> errors = new Dictionary <string, string[]>();

            UserAccount user = _context.UserAccounts.AsNoTracking().FirstOrDefault(u => u.UserName == _userName || u.Email == _userName);

            if (user == null)
            {
                errors.Add("User", new[] { "Podane konto nie istnieje" });
                return(new AccountResponse(errors));
            }

            var signInResult = await _signInManager.CheckPasswordSignInAsync(user, passwordDto.OldPassword, false);

            if (!signInResult.Succeeded)
            {
                errors.Add("Hasło", new[] { "Podałeś zle haslo" });
                return(new AccountResponse(errors));
            }

            var changeResult = await _userManager.ChangePasswordAsync(await _userManager.FindByIdAsync(user.Id),
                                                                      passwordDto.OldPassword, passwordDto.NewPassword);

            if (!changeResult.Succeeded)
            {
                errors.Add("Hasło", new[] { changeResult.ToString() });
                return(new AccountResponse(errors));
            }

            JwtTokenDto response = new JwtTokenDto
            {
                Token = GenerateJwtToken(user.Email, user)
            };

            return(new AccountResponse(response));
        }