public IActionResult SendCodeToStrava([FromBody] StravaAuthCode code) { if (code.auth_code != null) { string url = $"https://www.strava.com/oauth/token?client_id={Credentials.StravaClientId.ToString()}&client_secret={Credentials.StravaClientSecret}&code={code.auth_code}"; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Method = "POST"; System.Net.WebResponse response = request.GetResponse(); try { Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream); string responseString = streamReader.ReadToEnd(); StravaAthlete stravaAthlete = JsonConvert.DeserializeObject <StravaAthlete>(responseString); var user = _context.Users.Find(code.id); user.StravaAccessTokenHashed = PasswordConverter.Encrypt(stravaAthlete.access_token); _context.Users.Update(user); _context.SaveChanges(); return(Ok()); } catch { return(BadRequest()); } } else { return(NoContent()); } }
public async Task <IActionResult> Create([FromBody] UserVM data) { if (data != null) { try { if (_context.Users.FirstOrDefault(e => data.email == e.Email) != null) { return(Conflict()); } else { User user = new User(); user.FirstName = data.first_name.Trim(); user.LastName = data.last_name.Trim(); user.HashedPassword = PasswordConverter.Encrypt(data.password); user.Email = data.email.Trim(); _context.Users.Add(user); await _context.SaveChangesAsync(); LoggedInUserVM viewModel = GetUserInfoFromEmail(data.email); return(Ok(viewModel)); } } catch { throw new System.Web.Http.HttpResponseException(System.Net.HttpStatusCode.InternalServerError); } } else { return(NoContent()); } }
public IActionResult Login([FromBody] LogInAttempt data) { if (data.email != null && data.password != null) { try { var user = _context.Users.FirstOrDefault(a => a.Email == data.email); if (user != null) { string hashedPasswordAttempt = PasswordConverter.Encrypt(data.password); var actualPassword = user.HashedPassword; if (actualPassword == hashedPasswordAttempt) { LoggedInUserVM viewModel = GetUserInfoFromUser(user); return(Ok(viewModel)); } else { return(Unauthorized()); } } else { return(NotFound()); } } catch { throw new System.Web.Http.HttpResponseException(System.Net.HttpStatusCode.InternalServerError); } } else { return(NoContent()); } }