Exemple #1
0
        public IActionResult Post([FromBody] UserDataForAuthentication dataForAuthentication)
        {
            IActionResult response = Unauthorized();

            User currentUser = contextDb.Users.FirstOrDefault(u => ((u.Email == dataForAuthentication.Email) && (u.Password == dataForAuthentication.Password)));

            if (currentUser != null)
            {
                var tokenString = BuildToken(currentUser.UserId);
                response = Ok(new { token = tokenString, email = currentUser.Email });
            }
            return(response);
        }
Exemple #2
0
        public IActionResult Put([FromBody] UserDataForAuthentication updatedAuthenticationData)
        {
            try
            {
                Claim  claim           = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
                string idOfCurrentUser = claim.Value;

                User currentUser = contextDb.Users.FirstOrDefault(u => u.UserId.ToString() == idOfCurrentUser);

                User checkedUser = contextDb.Users.FirstOrDefault(u => u.Email == updatedAuthenticationData.Email);

                if ((checkedUser != null) && (checkedUser != currentUser))
                {
                    return(BadRequest("Email exists"));
                }

                if ((currentUser != null) && (updatedAuthenticationData != null))
                {
                    currentUser.Email = updatedAuthenticationData.Email;

                    if ((updatedAuthenticationData.Password != "") && (updatedAuthenticationData.Password != null))
                    {
                        currentUser.Password = updatedAuthenticationData.Password;
                    }

                    contextDb.Users.Update(currentUser);
                    contextDb.SaveChanges();

                    return(Ok(currentUser));
                }
                return(BadRequest());
            }
            catch
            {
                return(StatusCode(500));
            }
        }