public async Task <IActionResult> RateBugReportAsync([FromBody] UserRateDTO userRateDTO) { var handler = new JwtSecurityTokenHandler(); var token = handler.ReadToken(userRateDTO.Token) as JwtSecurityToken; var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; var bugReport = (await bugReportFinder.GetBugReportsAsync(x => x.Id == userRateDTO.Id)).FirstOrDefault(); if (bugReport is null) { return(NotFound("No bug report found")); } var user = (await userFinder.GetUsersAsync(x => x.Id == userId)).FirstOrDefault(); bool hasAgreed = userRateDTO.Rate == "agree"; if (hasAgreed) { bugReport.Rating++; } else { bugReport.Rating--; } await bugReportRater.RateBugReportAsync(bugReport, user, hasAgreed); return(Ok()); }
public async Task <UserAuthenticateResult> Authenticate(string username, string password, string appSettings) { try { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { throw new WrongCredentialsException("Wrong username or password!"); } var result = await _userFinder.GetUsersAsync(x => x.UserName == username); var user = result.FirstOrDefault(); if (user == null) { throw new UserNotFoundException("User not found!"); } if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt)) { throw new WrongCredentialsException("You entered wrong password!"); } var authenticatedUser = _mapper.Map <UserDto>(user); authenticatedUser.Token = _jwtCreator.CreateJwt(appSettings, user.Id); return(new UserAuthenticateResult { User = authenticatedUser }); } catch (UserNotFoundException e) { return(new UserAuthenticateResult { ErrorOccured = true, ErrorMessage = e.Message }); } catch (WrongCredentialsException e) { return(new UserAuthenticateResult { ErrorOccured = true, ErrorMessage = e.Message }); } catch (Exception e) { return(new UserAuthenticateResult { ErrorOccured = true, ErrorMessage = e.Message }); } }
public async Task <IActionResult> GetUserCredentials() { var userId = GetIdFromClaims(); if (userId is null) { userId = "fe640abd-37af-4aa7-8b65-04d060200361"; } var foundUser = await _userFinder.GetUsersAsync(x => x.Id == userId); var result = _mapper.Map <UserDto>(foundUser?.FirstOrDefault()); return(Ok(new { userName = result.UserName, email = result.Email, })); }