예제 #1
0
        public IActionResult Login([FromBody] LoginDTO login)
        {
            if (login == null)
            {
                return(BadRequest(new { Error = "No login credentials present" }));
            }

            try
            {
                var existingUser = _context.Users
                                   .Where(u =>
                                          u.Username == login.Username ||
                                          u.Email == login.Username
                                          )
                                   .FirstOrDefault();

                if (existingUser == null)
                {
                    return(Unauthorized(new { Error = "User not found" }));
                }

                var hashedBytes = Convert.FromBase64String(existingUser.Password);

                var salt = new byte[16];
                Array.Copy(hashedBytes, 0, salt, 0, 16);

                var pbkdf2 = KeyDerivation.Pbkdf2(login.Password, salt, KeyDerivationPrf.HMACSHA256, 10000, 20);

                for (int i = 0; i < 20; i++)
                {
                    if (hashedBytes[i + 16] != pbkdf2[i])
                    {
                        return(Unauthorized(new { Error = "Username or password is incorrect" }));
                    }
                }

                var token = GenerateToken(existingUser);

                return(Ok(new { Data = token }));
            }
            catch (Exception ex)
            {
                var message = $"Error logging in user {login.Username}";
                var data    = new
                {
                    Source         = ex.Source,
                    Message        = ex.Message,
                    InnerException = ex.InnerException,
                };

                var dataString = JsonConvert.SerializeObject(data);

                _context.Logs.Add(new KravWodLog {
                    Message = message, Data = dataString, TimeStamp = DateTimeOffset.Now
                });
                _context.SaveChanges();

                return(StatusCode(500, new { Error = message }));
            }
        }
예제 #2
0
        public async Task <IActionResult> GetFitnessLogs(int userId)
        {
            var tokenInfo = TokenUtility.GetTokenInfo(HttpContext);

            if (userId != tokenInfo.Id)
            {
                return(Unauthorized(new { Error = "Invalid UserId" }));
            }

            try
            {
                var results = await _context.FitnessLogs
                              .AsNoTracking()
                              .Include("WorkoutType")
                              .Include("ExerciseMaps.Exercise.ExerciseType")
                              .Include("ExerciseMaps.Exercise.QuantityType")
                              .Where(log =>
                                     log.UserId == userId
                                     )
                              .ToListAsync();

                return(Ok(new { Data = results }));
            }
            catch (Exception ex)
            {
                var message = $"Error retrieving fitness logs for user {userId}";
                var data    = new
                {
                    Source         = ex.Source,
                    Message        = ex.Message,
                    InnerException = ex.InnerException,
                };

                var dataString = JsonConvert.SerializeObject(data);

                _context.Logs.Add(new KravWodLog {
                    Message = message, Data = dataString, TimeStamp = DateTimeOffset.Now
                });
                _context.SaveChanges();

                return(StatusCode(500, new { Error = message }));
            }
        }
예제 #3
0
        public async Task <IActionResult> CreateEnums([FromBody] DataModel.Enum[] enums)
        {
            var results = new List <DataModel.Enum>();

            foreach (var item in enums)
            {
                var newEnum = await _context.Enums.AddAsync(item);

                results.Add(newEnum.Entity);
            }

            _context.SaveChanges();

            return(Ok(new { Data = results }));
        }